Sha256: 81fd9e26665d4f6e115c353ff86a2fb9c31c661e5a76b97ce9c7fb7aa233ba11

Contents?: true

Size: 796 Bytes

Versions: 2

Compression:

Stored size: 796 Bytes

Contents

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

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

      def call
        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.3.1 lib/convertator/providers/cbr_provider.rb
convertator-0.3.0 lib/convertator/providers/cbr_provider.rb