class Money::Bank::RussianCentralBankFetcher

Constants

DAILY_RATES_URL

Public Instance Methods

perform(date = Date.today) click to toggle source
# File lib/money/bank/russian_central_bank_fetcher.rb, line 17
def perform(date = Date.today)
  response = HTTParty.get(rates_url(date))
  unless response.success?
    raise_fetch_error("cbr.ru respond with #{response.code}", response)
  end
  extract_rates(response.parsed_response)
rescue HTTParty::Error => e
  raise_fetch_error(e.message)
end

Private Instance Methods

extract_rates(parsed_response) click to toggle source
# File lib/money/bank/russian_central_bank_fetcher.rb, line 37
def extract_rates(parsed_response)
  rates_arr = parsed_response['ValCurs']['Valute']
  rates_arr.map do |rate|
    {
      code: rate['CharCode'],
      nominal: rate['Nominal'].to_i,
      value: rate['Value'].tr(',', '.').to_f
    }
  end
end
raise_fetch_error(message, response = nil) click to toggle source
# File lib/money/bank/russian_central_bank_fetcher.rb, line 29
def raise_fetch_error(message, response = nil)
  raise FetchError.new(message, response)
end
rates_url(date) click to toggle source
# File lib/money/bank/russian_central_bank_fetcher.rb, line 33
def rates_url(date)
  "#{DAILY_RATES_URL}?date_req=#{date.strftime('%d/%m/%Y')}"
end