Class: CurrencyConverter::Yahoo

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeYahoo

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_currencyObject (readonly)

Returns the Symbol of ‘base’ currency



9
10
11
# File 'lib/currency_converter/yahoo.rb', line 9

def from_currency
  @from_currency
end

#ratesObject (readonly)

Returns the array of currencies rates



15
16
17
# File 'lib/currency_converter/yahoo.rb', line 15

def rates
  @rates
end

#to_currencyObject (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)

Parameters:

  • other (String, String, Numeric)

    currency to exchange to.

Returns:

  • (amount)


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