SergheiScepanovschi / ruby_on_rails_rep

0 stars 2 forks source link

Update your code #2

Closed Turcanuse closed 3 years ago

Turcanuse commented 4 years ago

Update code structure

Example code structure:

class ExampleBank

  def execute
    connect
    fetch_accounts
    fetch_transactions
    save_result # in JSON file
  end

  def connect
    # here you log in to the bank
  end

  def fetch_accounts
    # fetch html data using nokogiri, take only fragment of html.
    html = Nokogiri::HTML.fragment(browser.div(class: "example").html)
    parse_accounts(html)
  end

  def fetch_transactions
    # go to transactions
    # set date for 2 month
    parse_transactions(account, html)
  end

  def parse_accounts(html)
    # parse accounts here
  end

  def parse_transactions(account, html)
    # parse transactions here
  end
end

Update data structure

{
  "accounts": [
    {
      "name": "account1",
      "currency": "MDL",
      "balance": 300.22,
      "nature": "account",
      "transactions": [
        {
            "date": "2015-01-15",
            "description": "bought food",
            "amount": -20.31,
            "currency": "MDL",
            "account_name": "account1"
        }
      ]
    }
  ]
}

date must be it json format example: "2020-08-20", balance and amount must be float not string.

Write tests

You should write a test that checks the data from demo bank.

  1. Read about Rspec. It is a framework for test your code.

  2. Write specs for parse_accounts and parse_transactions.

  3. Take HTML from demo bank and save it in a file (check screenshot). html_example = Nokogiri::HTML(File.read('accounts.html'))

Снимок

  1. Call method for parse accounts and send HTML data. parse_accounts(html_example)

  2. Check the number of accounts and show an example account in a hash format.

Example:

it 'check number of accounts and show an example account' do
  html_example = Nokogiri::HTML(File.read('accounts.html'))
  accounts = parse_accounts(html_example) # NOTE: It is my example, not a real code.

  expect(accounts.count).to eq(5)
  expect(accounts.first.to_hash).to eq(
    {
      "name"         => "Demo Everyday Account",
      "currency"     => "USD",
      "balance"      => 1959.90,
      "nature"       => "account",
      "transactions" => []
    }
  )
end
  1. Do the same for transactions.

Good luck!

Turcanuse commented 3 years ago

A few more things to cover:

Good luck

SergheiScepanovschi commented 3 years ago

i don't have any global variables, I have only static fields in class(ok i can create object with dinamic fields, but i don't understend what is wrong). in test i tried to verify accouts data and transaction data in same time (ok i understood i have to separate them)

SergheiScepanovschi commented 3 years ago

I have updated files and created pull request for your comments.