yahoo_currency 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,16 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ config.yml
5
+ init.rb
6
+ lib/yahoo_currency.rb
7
+ lib/yahoo_currency/exchange_rate.rb
8
+ lib/yahoo_currency/yahoo_currency.rb
9
+ test/docs/FAKE-FAKE.csv
10
+ test/docs/JPY-USD.csv
11
+ test/docs/USD-CAD.csv
12
+ test/integration/yahoo_currency_test.rb
13
+ test/mocks/yahoo_currency_mock.rb
14
+ test/test_helper.rb
15
+ test/unit/yahoo_currency_test.rb
16
+ yahoo_currency.gemspec
data/README.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ = YahooCurrency
2
+
3
+ Retrieve currency exchange rates from Yahoo! Finance.
4
+
5
+ == The Yahoo! Finance Service
6
+
7
+ URL to fetch the AUD to USD exchange rate
8
+
9
+ http://download.finance.yahoo.com/d/quotes.csv?s=AUDUSD=X&f=nl1d1t1
10
+
11
+ Although the Yahoo! service supports multiple rates values from one request,
12
+ this class does not yet support this functionality yet.
13
+
14
+ $ curl "http://download.finance.yahoo.com/d/quotes.csv?s=USDCAD=X+JPYUSD=X+GBPUSD=X+USDJPY=X+EURUSD=X&f=nl1d1t1"
15
+ "USD to CAD",1.0191,"6/18/2008","2:45am"
16
+ "JPY to USD",0.0093,"6/18/2008","2:45am"
17
+ "GBP to USD",1.9514,"6/18/2008","2:44am"
18
+ "USD to JPY",108.085,"6/18/2008","2:45am"
19
+ "EUR to USD",1.5493,"6/18/2008","2:44am"
20
+
21
+ == Features
22
+
23
+ * So easy to use, even a baby could find out what his currency is trading at.
24
+ * Tested with Ruby 1.8.7 and 1.9.1
25
+
26
+ == Example
27
+
28
+ exchange_rate = YahooCurrency.get_rate!("JPY", "USD")
29
+ exchange_rate.from #=> "JPY"
30
+ exchange_rate.to #=> "USD"
31
+ exchange_rate.rate #=> 0.0111
32
+ exchange_rate.timestamp #=> Wed Feb 11 22:20:00 +0800 2009
33
+
34
+ == References
35
+
36
+ * http://gummy-stuff.org/forex.htm
37
+ * http://www.thinkruby.org/tags/currency-converter
38
+ * http://finance.yahoo.com/currency
39
+ * http://gummy-stuff.org/Yahoo-data.htm
40
+
41
+ == License
42
+
43
+ (The MIT License)
44
+
45
+ Copyright (c) 2009 Global IT Creations Pte Ltd http://www.globalitcreations.com
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining a copy
48
+ of this software and associated documentation files (the "Software"), to deal
49
+ in the Software without restriction, including without limitation the rights
50
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51
+ copies of the Software, and to permit persons to whom the Software is
52
+ furnished to do so, subject to the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be included in
55
+ all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
63
+ THE SOFTWARE.
64
+
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'echoe'
5
+ require 'yaml'
6
+
7
+ APP_NAME = "yahoo_currency"
8
+ APP_URL = "http://github.com/scottbarr/yahoo_currency/tree/master"
9
+ APP_DESCRIPTION = "Fetch currency exchange rates from Yahoo! Finance"
10
+ AUTHOR_NAME = "Scott Barr"
11
+ AUTHOR_EMAIL = "scottb@globalitcreations.com"
12
+
13
+ #
14
+ # Create a version for this build from the config.yml and the BUILD_NUMBER
15
+ # environment variable.
16
+ #
17
+ def get_version
18
+ # read the config file
19
+ config = open("config.yml") {|f| YAML.load(f)}
20
+
21
+ # build the version number
22
+ version_major = config["version"]["major"]
23
+ version_minor = config["version"]["minor"]
24
+ version_build = ENV["BUILD_NUMBER"] ||= "0"
25
+ "#{version_major}.#{version_minor}.#{version_build}"
26
+ end
27
+
28
+ Rake::TestTask.new("test:units") { |t|
29
+ t.pattern = 'test/unit/*_test.rb'
30
+ t.verbose = true
31
+ t.warning = false
32
+ }
33
+
34
+ Rake::TestTask.new("test:integration") { |t|
35
+ t.pattern = 'test/integration/*_test.rb'
36
+ t.verbose = true
37
+ t.warning = false
38
+ }
39
+
40
+ # setup the gem
41
+ Echoe.new(APP_NAME, get_version) do |p|
42
+ p.description = APP_DESCRIPTION
43
+ p.url = APP_URL
44
+ p.author = AUTHOR_NAME
45
+ p.email = AUTHOR_EMAIL
46
+ p.ignore_pattern = ["tmp/*", "script/*"]
47
+ p.development_dependencies = []
48
+ end
49
+
50
+ # define a remove task method so the "test" task isn't breaking my stuff
51
+ Rake::TaskManager.class_eval do
52
+ def remove_task(task_name)
53
+ @tasks.delete(task_name.to_s)
54
+ end
55
+ end
56
+
57
+ # helper method to remove a task
58
+ def remove_task(task_name)
59
+ Rake.application.remove_task(task_name)
60
+ end
61
+
62
+ # remove the original test task
63
+ remove_task :test
64
+
65
+ task :test => ["test:units", "test:integration"]
data/config.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ version:
3
+ major: 0
4
+ minor: 1
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # require 'lib/yahoo_currency'
2
+ # require 'lib/exchange_rate'
3
+ require 'yahoo_currency'
@@ -0,0 +1,2 @@
1
+ require 'yahoo_currency/yahoo_currency'
2
+ require 'yahoo_currency/exchange_rate'
@@ -0,0 +1,12 @@
1
+ class ExchangeRate
2
+
3
+ attr_accessor :from, :to, :rate, :timestamp
4
+
5
+ def initialize(from, to, rate, timestamp)
6
+ @from = from
7
+ @to = to
8
+ @rate = rate
9
+ @timestamp = timestamp
10
+ end
11
+
12
+ end
@@ -0,0 +1,57 @@
1
+ require 'net/http'
2
+ require 'time'
3
+
4
+ class YahooCurrency
5
+
6
+ # set the host and path we access the service from
7
+ HOST = 'download.finance.yahoo.com'
8
+ PATH = '/d/quotes.csv'
9
+
10
+ #
11
+ # Retrieve the exchange rate for the from and to currency codes.
12
+ #
13
+ def self.get_rate!(from, to)
14
+
15
+ # create the url
16
+ http = Net::HTTP.new(HOST, 80)
17
+ target = "#{PATH}?s=#{from}#{to}=X&f=nl1d1t1"
18
+
19
+ # hit the url
20
+ resp, data = http.get(target)
21
+
22
+ # check the response code
23
+ if resp.code.to_i != 200
24
+ raise "#{resp.code} #{resp.message}"
25
+ end
26
+
27
+ ExchangeRate.new(from, to, parse_rate(data), parse_timestamp(data))
28
+ end
29
+
30
+ private
31
+
32
+ #
33
+ # The rate is the first field in the CSV
34
+ #
35
+ def self.parse_rate(data)
36
+ data.split(',')[1].to_f
37
+ end
38
+
39
+ #
40
+ # The timestamp is in the 2 and 3rd fields of the CSV
41
+ #
42
+ # The timestamp in the data from Yahoo will in the format
43
+ # "M/D/YYYY HH:mm:ampm" Eg. 6/18/2008 2:45am
44
+ #
45
+ def self.parse_timestamp(data)
46
+ data = data.gsub('"', '')
47
+ d = data.chop.split(',')[2]
48
+ t = data.split(',')[3].gsub('"', '')
49
+
50
+ return nil if d == "N/A"
51
+
52
+ dp = d.split("/")
53
+
54
+ Time.parse("#{dp[2].to_i}/#{dp[0]}/#{dp[1].to_i} #{t}".chop)
55
+ end
56
+
57
+ end
@@ -0,0 +1 @@
1
+ "FAKEFAKE=X",0.00,"N/A","N/A"
@@ -0,0 +1 @@
1
+ "JPY to USD",0.0093,"6/18/2008","2:45am"
@@ -0,0 +1 @@
1
+ "USD to CAD",1.0191,"6/18/2008","2:45am"
@@ -0,0 +1,28 @@
1
+ require 'test/test_helper'
2
+
3
+ class YahooCurrencyTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @timestamp = Time.parse("2008-06-18 02:45:00 +0800")
7
+ end
8
+
9
+ def test_get_rate_usd_aud
10
+ exchange_rate = YahooCurrency.get_rate!("AUD", "USD")
11
+ assert_equal "AUD", exchange_rate.from
12
+ assert_equal "USD", exchange_rate.to
13
+ assert_not_nil exchange_rate.rate
14
+ assert_not_nil exchange_rate.timestamp
15
+ end
16
+
17
+ #
18
+ # Test one or more unknown currency codes
19
+ #
20
+ def test_get_rate_fake_fake
21
+ exchange_rate = YahooCurrency.get_rate!("FAKE", "FAKE")
22
+ assert_equal "FAKE", exchange_rate.from
23
+ assert_equal "FAKE", exchange_rate.to
24
+ assert_equal 0.00, exchange_rate.rate
25
+ assert_nil exchange_rate.timestamp
26
+ end
27
+
28
+ end
@@ -0,0 +1,20 @@
1
+ #
2
+ # A mock of the YahooCurrency class
3
+ #
4
+ require 'lib/yahoo_currency'
5
+
6
+ # Reopen and override the class
7
+ class YahooCurrency
8
+
9
+ def self.get_rate!(from, to)
10
+
11
+ data = ""
12
+ # try and open a test csv files
13
+ File.open("test/docs/#{from}-#{to}.csv", "r") do |infile|
14
+ data = infile.gets
15
+ end
16
+
17
+ ExchangeRate.new(from, to, parse_rate(data), parse_timestamp(data))
18
+ end
19
+
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'yahoo_currency'
@@ -0,0 +1,37 @@
1
+ require 'test/test_helper'
2
+ require 'test/mocks/yahoo_currency_mock'
3
+
4
+ class YahooCurrencyTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @timestamp = Time.parse("2008-06-18 02:45:00 +0800")
8
+ end
9
+
10
+ def test_get_rate_jpy_usd
11
+ exchange_rate = YahooCurrency.get_rate!("JPY", "USD")
12
+ assert_equal "JPY", exchange_rate.from
13
+ assert_equal "USD", exchange_rate.to
14
+ assert_equal 0.0093, exchange_rate.rate
15
+ assert_equal @timestamp, exchange_rate.timestamp
16
+ end
17
+
18
+ def test_get_rate_usd_cad
19
+ exchange_rate = YahooCurrency.get_rate!("USD", "CAD")
20
+ assert_equal "USD", exchange_rate.from
21
+ assert_equal "CAD", exchange_rate.to
22
+ assert_equal 1.0191, exchange_rate.rate
23
+ assert_equal @timestamp, exchange_rate.timestamp
24
+ end
25
+
26
+ #
27
+ # Test one or more unknown currency codes
28
+ #
29
+ def test_get_rate_fake_fake
30
+ exchange_rate = YahooCurrency.get_rate!("FAKE", "FAKE")
31
+ assert_equal "FAKE", exchange_rate.from
32
+ assert_equal "FAKE", exchange_rate.to
33
+ assert_equal 0.00, exchange_rate.rate
34
+ assert_nil exchange_rate.timestamp
35
+ end
36
+
37
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{yahoo_currency}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Scott Barr"]
9
+ s.date = %q{2010-07-20}
10
+ s.description = %q{Fetch currency exchange rates from Yahoo! Finance}
11
+ s.email = %q{scottb@globalitcreations.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/yahoo_currency.rb", "lib/yahoo_currency/exchange_rate.rb", "lib/yahoo_currency/yahoo_currency.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "config.yml", "init.rb", "lib/yahoo_currency.rb", "lib/yahoo_currency/exchange_rate.rb", "lib/yahoo_currency/yahoo_currency.rb", "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"]
14
+ s.homepage = %q{http://github.com/scottbarr/yahoo_currency/tree/master}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Yahoo_currency", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{yahoo_currency}
18
+ s.rubygems_version = %q{1.3.6}
19
+ s.summary = %q{Fetch currency exchange rates from Yahoo! Finance}
20
+ s.test_files = ["test/integration/yahoo_currency_test.rb", "test/test_helper.rb", "test/unit/yahoo_currency_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yahoo_currency
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Scott Barr
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-20 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Fetch currency exchange rates from Yahoo! Finance
22
+ email: scottb@globalitcreations.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ - lib/yahoo_currency.rb
30
+ - lib/yahoo_currency/exchange_rate.rb
31
+ - lib/yahoo_currency/yahoo_currency.rb
32
+ files:
33
+ - Manifest
34
+ - README.rdoc
35
+ - Rakefile
36
+ - config.yml
37
+ - init.rb
38
+ - lib/yahoo_currency.rb
39
+ - lib/yahoo_currency/exchange_rate.rb
40
+ - lib/yahoo_currency/yahoo_currency.rb
41
+ - test/docs/FAKE-FAKE.csv
42
+ - test/docs/JPY-USD.csv
43
+ - test/docs/USD-CAD.csv
44
+ - test/integration/yahoo_currency_test.rb
45
+ - test/mocks/yahoo_currency_mock.rb
46
+ - test/test_helper.rb
47
+ - test/unit/yahoo_currency_test.rb
48
+ - yahoo_currency.gemspec
49
+ has_rdoc: true
50
+ homepage: http://github.com/scottbarr/yahoo_currency/tree/master
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --line-numbers
56
+ - --inline-source
57
+ - --title
58
+ - Yahoo_currency
59
+ - --main
60
+ - README.rdoc
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 1
76
+ - 2
77
+ version: "1.2"
78
+ requirements: []
79
+
80
+ rubyforge_project: yahoo_currency
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Fetch currency exchange rates from Yahoo! Finance
85
+ test_files:
86
+ - test/integration/yahoo_currency_test.rb
87
+ - test/test_helper.rb
88
+ - test/unit/yahoo_currency_test.rb