sergey-raevsky / parse_account_info

0 stars 0 forks source link

Update your code #1

Closed Stepandanci closed 4 years ago

Stepandanci commented 4 years ago

Make your changes in dev branch. Open a new pull request from dev branch to master and do not merge it.

  1. Update code

Separate code logic in fetch and parsing methods. Unite methods to one class.

Example code structure:

class ExampleBank

  def execute
    connect
    fetch_accounts
    fetch_transactions
    save_result
  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
  1. Update data structure

Save all JSON data in a file, I need to check result.

Read again your test task, and check example.

{
  "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.
  1. Create example_spec.rb file.

  2. Write specs for parse_accounts and parse_transactions.

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

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

  5. 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"         => "40817840400000055321",
      "currency"     => "USD",
      "balance"      => 100000.00,
      "nature"       => "account",
      "transactions" => []
    }
  )
end
  1. Do the same for transactions.

Good luck!

Stepandanci commented 4 years ago

The task was done very well! Open a new pull request from develop to master for compare. I will ask other devs to review.

Stepandanci commented 4 years ago

Looks good! Do not merge your pull request.