class OpenexchangeConvert::ConvertCurrency

Public Class Methods

new(api_key) click to toggle source
# File lib/openexchange_convert.rb, line 5
def initialize(api_key)
  @usd = 1
  @results = 0
  @api_key = api_key
end

Public Instance Methods

convert_to_foreign(currency, amount) click to toggle source
# File lib/openexchange_convert.rb, line 25
def convert_to_foreign(currency, amount)
 foreign = get_exchange_rate(currency)
 @results = foreign * (amount.to_i * @usd)
end
convert_to_usd(currency, amount) click to toggle source
# File lib/openexchange_convert.rb, line 21
def convert_to_usd(currency, amount)
 foreign = get_exchange_rate(currency)
 @results = (amount.to_i * (@usd / foreign))
end
get_all_exchange_rates() click to toggle source
# File lib/openexchange_convert.rb, line 15
def get_all_exchange_rates
 @file = open("http://openexchangerates.org/api/latest.json?app_id=#{@api_key}")
 @j = JSON.load(@file.read)
 @current_currency = @j['rates']
end
get_exchange_rate(currency_type) click to toggle source
# File lib/openexchange_convert.rb, line 10
def get_exchange_rate(currency_type)
  all_rates = get_all_exchange_rates
  all_rates[currency_type.upcase]
end