Sha256: 16c212c10e309a5ceca011124c94ff70e39c54332ce07994e60d51db20ddb97e

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

require 'httparty'
require 'open-uri'

module Momm
  module Feeds
    class ECB

      FETCHING_URL = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml".freeze

      #Hard coded for good
      CURRENCIES = %w{USD JPY BGN CZK DKK GBP HUF LTL PLN RON SEK CHF
        NOK HRK RUB TRY AUD BRL CAD CNY HKD IDR ILS INR KRW
        MXN MYR NZD PHP SGD THB ZAR}.freeze

      class << self

        # Eager loading the instance of ECB
        #
        # == Returns
        # self
        #
        def instance
          @instance ||= self.send :new
        end
      end

      # should be a singleton class
      private_class_method :new

      # Parse the XML data by Nokogiri
      # == Returns
      # Nokogiri Object
      #
      def parsed_content
        # @TODO Refactoring Bad patterns
        HTTParty.get(fetching_url, format: :xml)['Envelope']['Cube']['Cube']
      end

      # convert the nokogiri parsed data to array
      #
      # == Returns
      # looks like [{date: Date.now, currency: :CNY, rate: 1.23} ...]
      #
      def currency_rates
        parsed_content.map do |content|
          date = Date.parse(content["time"])
          cubes = content["Cube"]
          cubes.map do |cube|
            {
              date: date,
              currency: cube["currency"].to_sym,
              rate: cube["rate"].to_f
            }
          end
        end.flatten
      end

      def fetching_url
        FETCHING_URL
      end

      def currencies
        CURRENCIES
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
momm-1.0.1 lib/momm/feeds/ecb.rb
momm-1.0.0 lib/momm/feeds/ecb.rb
momm-0.0.7 lib/momm/feeds/ecb.rb
momm-0.0.6 lib/momm/feeds/ecb.rb
momm-0.0.5 lib/momm/feeds/ecb.rb
momm-0.0.4 lib/momm/feeds/ecb.rb
momm-0.0.3 lib/momm/feeds/ecb.rb
momm-0.0.2 lib/momm/feeds/ecb.rb
momm-0.0.1 lib/momm/feeds/ecb.rb