Class: OpenexchangeConvert::ConvertCurrency
- Inherits:
-
Object
- Object
- OpenexchangeConvert::ConvertCurrency
- Defined in:
- lib/openexchange_convert.rb
Instance Method Summary collapse
- #convert_to_foreign(currency, amount) ⇒ Object
- #convert_to_usd(currency, amount) ⇒ Object
- #get_all_exchange_rates ⇒ Object
- #get_exchange_rate(currency_type) ⇒ Object
-
#initialize(api_key) ⇒ ConvertCurrency
constructor
A new instance of ConvertCurrency.
Constructor Details
#initialize(api_key) ⇒ ConvertCurrency
Returns a new instance of ConvertCurrency.
5 6 7 8 9 |
# File 'lib/openexchange_convert.rb', line 5 def initialize(api_key) @usd = 1 @results = 0 @api_key = api_key end |
Instance Method Details
#convert_to_foreign(currency, amount) ⇒ Object
25 26 27 28 |
# 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) ⇒ Object
21 22 23 24 |
# 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 ⇒ Object
15 16 17 18 19 |
# 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) ⇒ Object
10 11 12 13 |
# 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 |