Permalink
Browse files

Updated to run under Ruby 1.8 and 1.9

  • Loading branch information...
1 parent b87a487 commit 2e83f39337515a2368065b1ff4e02e9003be78c2 @scottjbarr committed Mar 7, 2009
View
@@ -1,8 +1,8 @@
-=== YahooCurrency
+= YahooCurrency
Retrieve currency exchange rates from Yahoo! Finance.
-==== The Yahoo! Finance Service
+== The Yahoo! Finance Service
URL to fetch the AUD to USD exchange rate
@@ -18,26 +18,27 @@ this class does not yet support this functionality yet.
"USD to JPY",108.085,"6/18/2008","2:45am"
"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.
+* Tested with Ruby 1.8.7 and 1.9.1
-==== Example
+== Example
exchange_rate = YahooCurrency.get_rate!("JPY", "USD")
exchange_rate.from #=> "JPY"
exchange_rate.to #=> "USD"
exchange_rate.rate #=> 0.0111
exchange_rate.timestamp #=> Wed Feb 11 22:20:00 +0800 2009
-==== References
+== References
* http://gummy-stuff.org/forex.htm
* http://www.thinkruby.org/tags/currency-converter
* http://finance.yahoo.com/currency
* http://gummy-stuff.org/Yahoo-data.htm
-==== License
+== License
(The MIT License)
@@ -17,7 +17,7 @@ def self.get_rate!(from, to)
target = "#{PATH}?s=#{from}#{to}=X&f=nl1d1t1"
# hit the url
- resp, data = http.get(target, nil)
+ resp, data = http.get(target)
# check the response code
if resp.code.to_i != 200
@@ -35,16 +35,23 @@ def self.get_rate!(from, to)
def self.parse_rate(data)
data.split(',')[1].to_f
end
-
+
#
# 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)
- # the timestamp will be in M/D/YYYY HH:mm:ampm format
- ts = (data.split(',')[2] + ' ' + data.split(',')[3]).gsub('"', '').chop
- return nil if ts == "N/A N/A"
-
- Time.parse(ts)
+ data = data.gsub('"', '')
+ d = data.chop.split(',')[2]
+ t = data.split(',')[3].gsub('"', '')
+
+ 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
@@ -3,7 +3,7 @@
class YahooCurrencyTest < Test::Unit::TestCase
def setup
- @timestamp = Time.parse("6/18/2008 2:45am")
+ @timestamp = Time.parse("2008-06-18 02:45:00 +0800")
end
def test_get_rate_usd_aud
@@ -4,7 +4,7 @@
class YahooCurrencyTest < Test::Unit::TestCase
def setup
- @timestamp = Time.parse("6/18/2008 2:45am")
+ @timestamp = Time.parse("2008-06-18 02:45:00 +0800")
end
def test_get_rate_jpy_usd
View
@@ -2,12 +2,12 @@
Gem::Specification.new do |s|
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.authors = ["Scott Barr"]
- s.date = %q{2009-02-11}
- s.description = %q{Retrieve currency exchange rates from Yahoo! Finance}
+ s.date = %q{2009-03-07}
+ s.description = %q{Fetch currency exchange rates from Yahoo! Finance}
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.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"]

0 comments on commit 2e83f39

Please sign in to comment.