Class: Forex::TabularRates

Inherits:
Object
  • Object
show all
Defined in:
lib/forex/tabular_rates.rb

Constant Summary collapse

NoSuchColumn =
Class.new(StandardError)
COLUMN_LABELS =
[
  :buy_cash,
  :buy_draft,
  :sell_cash,
  :sell_draft
]
DEFAULT_COLUMNS =
{
  currency_code: 0,
}.merge(Hash[(COLUMN_LABELS).zip(1..COLUMN_LABELS.size)])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, columns = DEFAULT_COLUMNS) ⇒ TabularRates

Returns a new instance of TabularRates.



18
19
20
21
# File 'lib/forex/tabular_rates.rb', line 18

def initialize(table, columns = DEFAULT_COLUMNS)
  @table = table
  @columns = columns.symbolize_keys
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/forex/tabular_rates.rb', line 5

def columns
  @columns
end

#tableObject

Returns the value of attribute table.



5
6
7
# File 'lib/forex/tabular_rates.rb', line 5

def table
  @table
end

Instance Method Details

#parse_rates(translations = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/forex/tabular_rates.rb', line 23

def parse_rates(translations = {})
  table.css('tr').each_with_object(cached_currencies) do |tr, currencies|
    table_row = tr.css('td')

    next unless (currency_code = parse_currency_code(table_row, translations))

    currencies[currency_code] = COLUMN_LABELS.each_with_object({}) do |column_label, rates|
      rates.merge! parse_currency_rates(table_row, column_label)
    end
  end
end