Class: CurrencyConverter::Yahoo
- Inherits:
-
Object
- Object
- CurrencyConverter::Yahoo
- Defined in:
- lib/currency_converter/yahoo.rb
Constant Summary collapse
- API_URL =
'https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote;currency=true?view=basic'
Instance Attribute Summary collapse
-
#from_currency ⇒ Object
readonly
Returns the Symbol of ‘base’ currency.
-
#rates ⇒ Object
readonly
Returns the array of currencies rates.
-
#to_currency ⇒ Object
readonly
Returns the Symbol of ‘quot’ currency.
Instance Method Summary collapse
-
#exchange(base, quot, amount) ⇒ amount
Receive the amount of you desire currency.
-
#initialize ⇒ Yahoo
constructor
A new instance of Yahoo.
Constructor Details
#initialize ⇒ Yahoo
Returns a new instance of Yahoo.
19 20 21 22 23 |
# File 'lib/currency_converter/yahoo.rb', line 19 def initialize response = fetch_data parse_rates(response) end |
Instance Attribute Details
#from_currency ⇒ Object (readonly)
Returns the Symbol of ‘base’ currency
9 10 11 |
# File 'lib/currency_converter/yahoo.rb', line 9 def from_currency @from_currency end |
#rates ⇒ Object (readonly)
Returns the array of currencies rates
15 16 17 |
# File 'lib/currency_converter/yahoo.rb', line 15 def rates @rates end |
#to_currency ⇒ Object (readonly)
Returns the Symbol of ‘quot’ currency
12 13 14 |
# File 'lib/currency_converter/yahoo.rb', line 12 def to_currency @to_currency end |
Instance Method Details
#exchange(base, quot, amount) ⇒ amount
Receive the amount of you desire currency.
currency_converter = CurrencyConverter::Yahoo.new currency_converter.exchange(‘USD’, ‘EUR’, 100) currency_converter.exchange(‘USD’, ‘INR’, 100)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/currency_converter/yahoo.rb', line 35 def exchange(base, quot, amount) @from_currency = base.upcase.to_sym @to_currency = quot.upcase.to_sym validate_currency base_rate = rates[from_currency].to_f quot_rate = rates[to_currency].to_f rate = base_rate.zero? ? 0 : (quot_rate / base_rate) validate_rate(rate) amount * rate end |