Class: Money::Bank::YahooFinanceCurrency
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::YahooFinanceCurrency
- Defined in:
- lib/money/bank/yahoo_finance_currency.rb
Instance Attribute Summary collapse
-
#rates ⇒ Hash
readonly
Stores the currently known rates.
Instance Method Summary collapse
-
#flush_rate(from, to) ⇒ Float
Clears the specified rate stored in @rates.
-
#flush_rates ⇒ Hash
Clears all rates stored in @rates.
-
#get_rate(from, to) ⇒ Float
Returns the requested rate from @rates if it exists, otherwise calls
#get_yahoo_rate. -
#get_yahoo_rate(from, to) ⇒ Float
Returns the requested rate after querying Yahoo! Finance.
Instance Attribute Details
#rates ⇒ Hash (readonly)
Returns Stores the currently known rates.
8 9 10 |
# File 'lib/money/bank/yahoo_finance_currency.rb', line 8 def rates @rates end |
Instance Method Details
#flush_rate(from, to) ⇒ Float
Clears the specified rate stored in @rates.
39 40 41 42 43 44 |
# File 'lib/money/bank/yahoo_finance_currency.rb', line 39 def flush_rate(from, to) key = rate_key_for(from, to) @mutex.synchronize{ @rates.delete(key) } end |
#flush_rates ⇒ Hash
Clears all rates stored in @rates
19 20 21 22 23 |
# File 'lib/money/bank/yahoo_finance_currency.rb', line 19 def flush_rates @mutex.synchronize{ @rates = {} } end |
#get_rate(from, to) ⇒ Float
Returns the requested rate from @rates if it exists, otherwise calls #get_yahoo_rate.
58 59 60 61 62 |
# File 'lib/money/bank/yahoo_finance_currency.rb', line 58 def get_rate(from, to) @mutex.synchronize{ @rates[rate_key_for(from, to)] ||= get_yahoo_rate(from, to) } end |
#get_yahoo_rate(from, to) ⇒ Float
Returns the requested rate after querying Yahoo! Finance.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/money/bank/yahoo_finance_currency.rb', line 76 def get_yahoo_rate(from, to) from, to = Currency.wrap(from), Currency.wrap(to) if from.iso_code == to.iso_code return 1.0 else url = "http://download.finance.yahoo.com/d/quotes.csv?s=#{from.iso_code}#{to.iso_code}=X&f=l1" data = URI.parse(url).read.to_f if data.zero? or to.iso_code == "ALL" raise UnknownRate else return data end end end |