Class: YahooCurrency

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_currency/yahoo_currency.rb

Constant Summary collapse

HOST =

set the host and path we access the service from

'download.finance.yahoo.com'
PATH =
'/d/quotes.csv'

Class Method Summary collapse

Class Method Details

.get_rate!(from, to) ⇒ Object

Retrieve the exchange rate for the from and to currency codes.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yahoo_currency/yahoo_currency.rb', line 13

def self.get_rate!(from, to)

  # create the url
  http = Net::HTTP.new(HOST, 80)
  target = "#{PATH}?s=#{from}#{to}=X&f=nl1d1t1"

  # hit the url
  resp, data = http.get(target)

  # check the response code
  if resp.code.to_i != 200
    raise "#{resp.code} #{resp.message}"
  end

  ExchangeRate.new(from, to, parse_rate(data), parse_timestamp(data))
end