class SevenBankFxRate::Parser

Parses xml response content and convert to ruby objects

Public Class Methods

new(response) click to toggle source

@param response the body of Net::HTTPResponse

# File lib/seven_bank_fx_rate/parser.rb, line 9
def initialize(response)
  @root = REXML::Document.new(response).root
end

Public Instance Methods

countries() click to toggle source

Parses the <countries> section in original xml response @return an array of SevenBankFxRate::Elements::Country

# File lib/seven_bank_fx_rate/parser.rb, line 29
def countries
  countries = []
  return countries unless @root

  @root.elements.each('countries/country') do |country_tag|
    country = Elements::Country.new
    %w[country_code country_name].each do |attr|
      country_tag.elements.each(attr.split('_').join) do |e|
        country.send("#{attr}=".to_sym, e.text.strip)
      end
    end
    country.currencies = currencies country_tag
    countries << country
  end
  countries
end
meta() click to toggle source

Parses the <header> section in original xml response @return instance of SevenBankFxRate::Elements::Meta

# File lib/seven_bank_fx_rate/parser.rb, line 15
def meta
  meta = Elements::Meta.new
  return meta unless @root

  %w[create_date apply_date apply_time data_count].each do |attr|
    @root.elements.each("header/#{attr.split('_').join}") do |e|
      meta.send("#{attr}=".to_sym, e.text.strip)
    end
  end
  meta
end

Private Instance Methods

currencies(country_tag) click to toggle source

Parses the <currencies> section in original xml response @return an array of SevenBankFxRate::Elements::Currency

# File lib/seven_bank_fx_rate/parser.rb, line 50
def currencies(country_tag)
  currencies = []
  country_tag.elements.each('currency') do |currency_tag|
    currency = Elements::Currency.new
    %w[currency_code currency_name fx_rate].each do |attr|
      currency_tag.elements.each(attr.split('_').join) do |e|
        currency.send("#{attr}=".to_sym, e.text.strip)
      end
    end
    currencies << currency
  end
  currencies
end