class CurrencyApi

require 'http'

Constants

API_TOKEN
Quotes
SOURCES

Reference Options: currency-api.appspot.com

Public Class Methods

convert(options = {}) click to toggle source
# File lib/chid/currency_api.rb, line 50
def self.convert(options = {})
  amount = options.fetch(:amount, 1).to_f
  from   = options.fetch(:from, :USD).to_sym
  to     = options.fetch(:to, :BRL).to_sym

  request = HTTP.get("http://www.apilayer.net/api/live?access_key=#{API_TOKEN}")
  json    = JSON.parse request

  to_amount   = json['quotes']["USD#{to}"]
  from_amount = json['quotes']["USD#{from}"]

  if to == :USD
    amount / from_amount
  else
    to_amount * amount
  end
end