amoscaliuc / ruby-test-task

extract information from the bank: accounts and transactions (for the last two months).
0 stars 0 forks source link

Update code structure #1

Closed Turcanuse closed 3 years ago

Turcanuse commented 3 years ago

Example code structure:

class ExampleBank

  def connect
    # here you log in to the bank
  end

  def fetch_accounts
    # navigate to 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
    # navigate to transactions
    # set date for 2 month
    parse_transactions(account, html)
  end

  def parse_accounts(html)
    # parse accounts 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"
        }
      ]
    }
  ]
}

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'))

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

Good luck!

amoscaliuc commented 3 years ago

Pull request: https://github.com/amoscaliuc/ruby-test-task/pull/2 attached to this issue.

All main remarks applied.

One todo remained: set date for 2 month: Done

Stepandanci commented 3 years ago

Hello,

You've updated your code well. We still have a couple of questions.

Update your Account and Transaction classes. You need to write a complete constructor (method: initialize) for objects of these classes.
You must use these classes in fetch method , this is an important part of the task.

In spec provide an account and transaction example in hash format, not in separate entities. Create a method to convert the account object to a hash.

Example:

expect(accounts.first.to_hash).to eq(
  {
    "name"         => "Demo Everyday Account",
    "currency"     => "USD",
    "balance"      => 1959.90,
    "nature"       => "account",
    "transactions" => []
  }
)
amoscaliuc commented 3 years ago

Now I see how the task should be implemented!!! :) Now it looks MUUUUCH better and cleaner! Thanks!

All comments applied in the current dev branch.

Stepandanci commented 3 years ago

It looks better.

Show an example of output.json file from method accounts_into_file. You used it in your code, but you haven't saved it to the repository. We need to check the result of the code execution.

In spec/bank_spec.rb transaction's currency should have the same value as account's currency. (RUB not symbol ).

As a recommendation you can use each_with_index method instead of increasing count value each time.

amoscaliuc commented 3 years ago

Show an example of output.json - DONE Transaction's currency should have the same value as account's currency - DONE Use each_with_index method instead of increasing count - DONE

Stepandanci commented 3 years ago

Read again your test task and check all steps.

Do not rush to complete task.

In step 4 described required format.

4. Add to your script the possibility to printout stored data in JSON format.

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

Use method JSON.pretty_generate(some_data).

amoscaliuc commented 3 years ago

I saw my problem, why I could not properly use JSON.pretty_generate for the requested output: