Skip to content

Commit

Permalink
Updated to run under Ruby 1.8 and 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
scottjbarr committed Mar 7, 2009
1 parent b87a487 commit 2e83f39
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
13 changes: 7 additions & 6 deletions README.rdoc
@@ -1,8 +1,8 @@
=== YahooCurrency = YahooCurrency


Retrieve currency exchange rates from Yahoo! Finance. Retrieve currency exchange rates from Yahoo! Finance.


==== The Yahoo! Finance Service == The Yahoo! Finance Service


URL to fetch the AUD to USD exchange rate URL to fetch the AUD to USD exchange rate


Expand All @@ -18,26 +18,27 @@ this class does not yet support this functionality yet.
"USD to JPY",108.085,"6/18/2008","2:45am" "USD to JPY",108.085,"6/18/2008","2:45am"
"EUR to USD",1.5493,"6/18/2008","2:44am" "EUR to USD",1.5493,"6/18/2008","2:44am"


==== Features == Features


* So easy to use, even a baby could find out what his currency is trading at. * So easy to use, even a baby could find out what his currency is trading at.
* Tested with Ruby 1.8.7 and 1.9.1


==== Example == Example


exchange_rate = YahooCurrency.get_rate!("JPY", "USD") exchange_rate = YahooCurrency.get_rate!("JPY", "USD")
exchange_rate.from #=> "JPY" exchange_rate.from #=> "JPY"
exchange_rate.to #=> "USD" exchange_rate.to #=> "USD"
exchange_rate.rate #=> 0.0111 exchange_rate.rate #=> 0.0111
exchange_rate.timestamp #=> Wed Feb 11 22:20:00 +0800 2009 exchange_rate.timestamp #=> Wed Feb 11 22:20:00 +0800 2009


==== References == References


* http://gummy-stuff.org/forex.htm * http://gummy-stuff.org/forex.htm
* http://www.thinkruby.org/tags/currency-converter * http://www.thinkruby.org/tags/currency-converter
* http://finance.yahoo.com/currency * http://finance.yahoo.com/currency
* http://gummy-stuff.org/Yahoo-data.htm * http://gummy-stuff.org/Yahoo-data.htm


==== License == License


(The MIT License) (The MIT License)


Expand Down
23 changes: 15 additions & 8 deletions lib/yahoo_currency/yahoo_currency.rb
Expand Up @@ -17,7 +17,7 @@ def self.get_rate!(from, to)
target = "#{PATH}?s=#{from}#{to}=X&f=nl1d1t1" target = "#{PATH}?s=#{from}#{to}=X&f=nl1d1t1"


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


# check the response code # check the response code
if resp.code.to_i != 200 if resp.code.to_i != 200
Expand All @@ -35,16 +35,23 @@ def self.get_rate!(from, to)
def self.parse_rate(data) def self.parse_rate(data)
data.split(',')[1].to_f data.split(',')[1].to_f
end end

# #
# The timestamp is in the 2 and 3rd fields of the CSV # The timestamp is in the 2 and 3rd fields of the CSV
# #
# The timestamp in the data from Yahoo will in the format
# "M/D/YYYY HH:mm:ampm" Eg. 6/18/2008 2:45am
#
def self.parse_timestamp(data) def self.parse_timestamp(data)
# the timestamp will be in M/D/YYYY HH:mm:ampm format data = data.gsub('"', '')
ts = (data.split(',')[2] + ' ' + data.split(',')[3]).gsub('"', '').chop d = data.chop.split(',')[2]
return nil if ts == "N/A N/A" t = data.split(',')[3].gsub('"', '')


Time.parse(ts) return nil if d == "N/A"

dp = d.split("/")

Time.parse("#{dp[2].to_i}/#{dp[0]}/#{dp[1].to_i} #{t}".chop)
end end

end end
2 changes: 1 addition & 1 deletion test/integration/yahoo_currency_test.rb
Expand Up @@ -3,7 +3,7 @@
class YahooCurrencyTest < Test::Unit::TestCase class YahooCurrencyTest < Test::Unit::TestCase


def setup def setup
@timestamp = Time.parse("6/18/2008 2:45am") @timestamp = Time.parse("2008-06-18 02:45:00 +0800")
end end


def test_get_rate_usd_aud def test_get_rate_usd_aud
Expand Down
2 changes: 1 addition & 1 deletion test/unit/yahoo_currency_test.rb
Expand Up @@ -4,7 +4,7 @@
class YahooCurrencyTest < Test::Unit::TestCase class YahooCurrencyTest < Test::Unit::TestCase


def setup def setup
@timestamp = Time.parse("6/18/2008 2:45am") @timestamp = Time.parse("2008-06-18 02:45:00 +0800")
end end


def test_get_rate_jpy_usd def test_get_rate_jpy_usd
Expand Down
6 changes: 3 additions & 3 deletions yahoo_currency.gemspec
Expand Up @@ -2,12 +2,12 @@


Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{yahoo_currency} s.name = %q{yahoo_currency}
s.version = "0.1.0" s.version = "0.1.2"


s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Scott Barr"] s.authors = ["Scott Barr"]
s.date = %q{2009-02-11} s.date = %q{2009-03-07}
s.description = %q{Retrieve currency exchange rates from Yahoo! Finance} s.description = %q{Fetch currency exchange rates from Yahoo! Finance}
s.email = %q{scottb@globalitcreations.com} s.email = %q{scottb@globalitcreations.com}
s.extra_rdoc_files = ["lib/yahoo_currency/exchange_rate.rb", "lib/yahoo_currency/yahoo_currency.rb", "lib/yahoo_currency.rb", "README.rdoc"] s.extra_rdoc_files = ["lib/yahoo_currency/exchange_rate.rb", "lib/yahoo_currency/yahoo_currency.rb", "lib/yahoo_currency.rb", "README.rdoc"]
s.files = ["config.yml", "init.rb", "lib/yahoo_currency/exchange_rate.rb", "lib/yahoo_currency/yahoo_currency.rb", "lib/yahoo_currency.rb", "Manifest", "Rakefile", "README.rdoc", "test/docs/FAKE-FAKE.csv", "test/docs/JPY-USD.csv", "test/docs/USD-CAD.csv", "test/integration/yahoo_currency_test.rb", "test/mocks/yahoo_currency_mock.rb", "test/test_helper.rb", "test/unit/yahoo_currency_test.rb", "yahoo_currency.gemspec"] s.files = ["config.yml", "init.rb", "lib/yahoo_currency/exchange_rate.rb", "lib/yahoo_currency/yahoo_currency.rb", "lib/yahoo_currency.rb", "Manifest", "Rakefile", "README.rdoc", "test/docs/FAKE-FAKE.csv", "test/docs/JPY-USD.csv", "test/docs/USD-CAD.csv", "test/integration/yahoo_currency_test.rb", "test/mocks/yahoo_currency_mock.rb", "test/test_helper.rb", "test/unit/yahoo_currency_test.rb", "yahoo_currency.gemspec"]
Expand Down

0 comments on commit 2e83f39

Please sign in to comment.