class Momm::Feeds::ECB

Constants

CURRENCIES

Hard coded for good

ConnectionError
FETCHING_URL

Public Class Methods

instance() click to toggle source

Eager loading the instance of ECB

Returns

self

# File lib/momm/feeds/ecb.rb, line 23
def instance
  @instance ||= self.send :new
end

Public Instance Methods

currencies() click to toggle source
# File lib/momm/feeds/ecb.rb, line 78
def currencies
  CURRENCIES
end
currency_rates() click to toggle source

turn the xml data to array of currencies

Returns

looks like [{date: Date.now, currency: :CNY, rate: 1.23} …]

# File lib/momm/feeds/ecb.rb, line 60
def currency_rates
  daily_currencies = xml.elements["//Cube"].select{ |c| c.is_a? REXML::Element }
  daily_currencies.map do |daily_currency|
    date = Date.parse daily_currency.attributes["time"]
    daily_currency.map do |cube|
      {
        date: date,
        currency: cube.attributes["currency"].to_sym,
        rate: cube.attributes["rate"].to_f
      }
    end
  end.flatten
end
fetching_url() click to toggle source
# File lib/momm/feeds/ecb.rb, line 74
def fetching_url
  URI(FETCHING_URL)
end
response() click to toggle source

Request the feed and get the response

Returns

A Net::HTTPResponse response

# File lib/momm/feeds/ecb.rb, line 37
def response
  @response ||= Net::HTTP.start(fetching_url.host, fetching_url.port,
    :use_ssl => fetching_url.scheme == "https",
    :open_timeout => 5,
    :read_timeout => 5) do |http|

    http.request Net::HTTP::Get.new(fetching_url)
  end
end
xml() click to toggle source

Response body in xml format

# File lib/momm/feeds/ecb.rb, line 48
def xml
  @xml ||= begin
    raise ConnectionError unless response.is_a? Net::HTTPOK
    REXML::Document.new response.body
  end
end