Sha256: 55b92cfc88dea53d6608ca9cbb0742c428e31b39dc4060c234a9911d4d0cdf97

Contents?: true

Size: 784 Bytes

Versions: 2

Compression:

Stored size: 784 Bytes

Contents

require 'net/http'
require 'rexml/document'

module Convertator
  module Providers
    class CbrProvider
      SERVICE_URI = 'http://www.cbr.ru/scripts/xml_daily.asp'

      def new_rates 
        default_rates.merge parse(fetch)
      end

      private

      def default_rates
        {
          RUB: "1",
        }
      end

      def parse(data)
        doc = REXML::Document.new(data)
        rates = {}
        doc.elements.each('ValCurs/Valute') do |valute|
          code = REXML::XPath.first(valute, 'CharCode/text()').to_s
          val = REXML::XPath.first(valute, 'Value/text()').to_s
          rates[code.upcase.to_sym] = val
        end
        rates
      end

      def fetch
        uri = URI(SERVICE_URI)
        ::Net::HTTP.get(uri) 
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
convertator-0.1.1 lib/convertator/providers/cbr_provider.rb
convertator-0.1.0 lib/convertator/providers/cbr_provider.rb