Class: Money::Bank::YahooFinanceCurrency

Inherits:
VariableExchange
  • Object
show all
Defined in:
lib/money/bank/yahoo_finance_currency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ratesHash (readonly)

Returns Stores the currently known rates.

Returns:

  • (Hash)

    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.

Examples:

@bank = YahooFinanceCurrency.new #=> <Money::Bank::YahooFinanceCurrency...>
@bank.get_rate(:USD, :EUR) #=> 0.776337241
@bank.flush_rate(:USD, :EUR) #=> 0.776337241

Parameters:

  • from (String, Symbol, Currency)

    Currency to convert from (used for key into @rates).

  • to (String, Symbol, Currency)

    Currency to convert to (used for key into @rates).

Returns:

  • (Float)

    The flushed rate.



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_ratesHash

Clears all rates stored in @rates

Examples:

@bank = YahooFinanceCurrency.new  #=> <Money::Bank::YahooFinanceCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241
@bank.flush_rates           #=> {}

Returns:

  • (Hash)

    The empty @rates Hash.



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.

Examples:

@bank = YahooFinanceCurrency.new #=> <Money::Bank::YahooFinanceCurrency...>
@bank.get_rate(:USD, :EUR) #=> 0.776337241

Parameters:

  • from (String, Symbol, Currency)

    Currency to convert from

  • to (String, Symbol, Currency)

    Currency to convert to

Returns:

  • (Float)

    The requested 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.

Examples:

@bank = YahooFinanceCurrency.new #=> <Money::Bank::YahooFinanceCurrency...>
@bank.get_yahoo_rate(:USD, :EUR) #=> 0.776337241

Parameters:

  • from (String, Symbol, Currency)

    Currency to convert from

  • to (String, Symbol, Currency)

    Currency to convert to

Returns:

  • (Float)

    The requested rate.



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