class ExchangeRatesNBP::Clients::CurrencyDataSet

Constants

FIELD_AVERAGE_EXCHANGE_RATE
FIELD_CONVERSION_FACTOR
FIELD_CURRENCY_CODE
FIELD_PUBLISH_DATE
FIELD_TABLE_NAME
SELECTOR_CURRENCY_ELEMENT

Public Class Methods

new(table_id) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 14
def initialize(table_id)
  @table_id = table_id
end

Public Instance Methods

exchange_rate(currency_code) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 18
def exchange_rate(currency_code)
  xml = Oga.parse_xml(data)

  doc = selected_currency_element(xml, currency_code)

  currency_element_to_hash(xml, doc)
end
exchange_table() click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 26
def exchange_table
  xml = Oga.parse_xml(data)

  table_element_to_hash(xml)
end

Private Instance Methods

currency_element_to_hash(xml, doc) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 34
def currency_element_to_hash(xml, doc)
  {
    exchange_rate: to_exchange_rate(doc),
    publish_date: to_publish_date(xml),
    conversion_factor: to_conversion_factor(doc)
  }
end
data() click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 75
def data
  HTTP.get(BASE_URL + "#{@table_id}.xml").to_s
end
selected_currency_element(xml, currency_code) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 65
def selected_currency_element(xml, currency_code)
  doc = xml.css(SELECTOR_CURRENCY_ELEMENT).detect do |p|
    p.css(FIELD_CURRENCY_CODE).text == currency_code
  end

  raise "Invalid currency code: #{currency_code}" if doc.nil?

  doc
end
table_element_to_hash(xml) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 42
def table_element_to_hash(xml)
  {
    publish_date: to_publish_date(xml),
    exchange_table_name: to_exchange_table(xml)
  }
end
to_conversion_factor(doc) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 61
def to_conversion_factor(doc)
  doc.css(FIELD_CONVERSION_FACTOR).text.to_i
end
to_exchange_rate(doc) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 57
def to_exchange_rate(doc)
  doc.css(FIELD_AVERAGE_EXCHANGE_RATE).text.tr(',', '.').to_f
end
to_exchange_table(xml) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 53
def to_exchange_table(xml)
  xml.css(FIELD_TABLE_NAME).text
end
to_publish_date(xml) click to toggle source
# File lib/exchange_rates_nbp/clients/currency_data_set.rb, line 49
def to_publish_date(xml)
  Date.parse(xml.css(FIELD_PUBLISH_DATE).text)
end