Sha256: a4e47d688f1c46df4dfd38dbce5ea763ab2cf302c910a7999719ca478a271b69

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'rexml/document'

module SevenBankFxRate
  # Parses xml response content and convert to ruby objects
  class Parser
    # @param response the body of Net::HTTPResponse
    def initialize(response)
      @root = REXML::Document.new(response).root
    end

    # Parses the <header> section in original xml response
    # @return instance of SevenBankFxRate::Elements::Meta
    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

    # Parses the <countries> section in original xml response
    # @return an array of SevenBankFxRate::Elements::Country
    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

    private

    # Parses the <currencies> section in original xml response
    # @return an array of SevenBankFxRate::Elements::Currency
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
seven_bank_fx_rate-1.1.1 lib/seven_bank_fx_rate/parser.rb
seven_bank_fx_rate-1.1.0 lib/seven_bank_fx_rate/parser.rb