Class: ExchangeRate

Inherits:
Object
  • Object
show all
Defined in:
lib/exchangerate.rb

Constant Summary collapse

HOST =
"www.exchangerate-api.com"
SUPPORTED_CODES =
['EUR','USD','JPY','GBP','BGN','CZK','DKK','EEK','HUF','LTL','LVL','PLN','RON','SEK','CHF','NOK','HRK','RUB','TRY','AUD','BRL','CAD','CNY','HKD','IDR','INR','KRW','MXN','MYR','NZD','PHP','SGD','THB','ZAR']

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ ExchangeRate

Returns a new instance of ExchangeRate.



10
11
12
# File 'lib/exchangerate.rb', line 10

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#convert(from_curr, to_curr, amount) ⇒ Object

converts



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/exchangerate.rb', line 15

def convert(from_curr,to_curr,amount)
  begin
    raise UnsupportedCurrencyException if !SUPPORTED_CODES.include?(from_curr) || !SUPPORTED_CODES.include?(to_curr)
    http = Net::HTTP.new(HOST, 80)
    url = "/"+from_curr+"/"+to_curr+"/"+amount.to_s+"?k=#{@api_key}"
    response = http.get(url)
    return response.body
  rescue UnsupportedCurrencyException => e
    raise UnsupportedCurrencyException
  rescue => err_msg
    puts #{err_msg}
  end
end