Class: SevenBankFxRate::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/seven_bank_fx_rate/parser.rb

Overview

Parses xml response content and convert to ruby objects

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • response

    the body of Net::HTTPResponse



9
10
11
# File 'lib/seven_bank_fx_rate/parser.rb', line 9

def initialize(response)
  @root = REXML::Document.new(response).root
end

Instance Method Details

#countriesObject

Parses the <countries> section in original xml response

Returns:

  • an array of SevenBankFxRate::Elements::Country



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

#metaObject

Parses the <header> section in original xml response

Returns:

  • instance of SevenBankFxRate::Elements::Meta



15
16
17
18
19
20
21
22
23
24
25
# 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