class Mint::FixerCurrency::Request

Fetch exchange rates from fixer.io

Public Class Methods

new(currency, date = nil) click to toggle source
# File lib/mint/fixer_currency/request.rb, line 9
def initialize(currency, date = nil)
  @currency = currency
  @date = date
end

Public Instance Methods

perform!() click to toggle source

@return Hash

# File lib/mint/fixer_currency/request.rb, line 15
def perform!
  url = @date ? "http://api.fixer.io/#{@date}?base=#{@currency}" : "http://api.fixer.io/latest?base=#{@currency}"
  parse(fetch(url))
end

Private Instance Methods

fetch(url) click to toggle source

@return [String]

# File lib/mint/fixer_currency/request.rb, line 23
def fetch(url)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, 80)
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  response.body
end
parse(body) click to toggle source

@return [Hash]

# File lib/mint/fixer_currency/request.rb, line 32
def parse(body)
  json = JSON.parse(body)
  error = json.fetch('error', nil)
  raise FixerArgumentError, error if error
  Hash[json.fetch('rates', nil).map { |k, v| [k.to_sym, v] }]
end