eu_central_bank 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Wong Liang Zan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,56 @@
1
+ = eu_central_bank
2
+
3
+ == Introduction
4
+
5
+ This gem downloads the exchange rates from the European Central Bank. You can calculate exchange rates with it. It is compatible with the money gem.
6
+
7
+ == Installation
8
+
9
+ $ gem install eu_central_bank
10
+
11
+ == Dependencies
12
+
13
+ * nokogiri
14
+ * money
15
+
16
+ == Usage
17
+
18
+ With the gem, you do not need to manually add exchange rates. Calling update_rates will download the rates from the European Central Bank. The API is the same as the money gem. Feel free to use Money objects with the bank.
19
+
20
+ eu_bank = EuCentralBank.new
21
+ Money.default_bank = eu_bank
22
+ money1 = Money.new(10)
23
+ money1.bank # eu_bank
24
+
25
+ # call this before calculating exchange rates
26
+ # this will download the rates from ECB
27
+ eu_bank.update_rates
28
+
29
+ # exchange 100 CAD to USD
30
+ # API is the same as the money gem
31
+ eu_bank.exchange(100, "CAD", "USD") # 124
32
+ Money.us_dollar(100).exchange_to("CAD") # Money.ca_dollar(124)
33
+
34
+ For performance reasons, you may prefer to read from a file instead. Furthermore, ECB publishes their rates daily. It makes sense to save the rates in a file to read from.
35
+
36
+ # saves the rates in a specified location
37
+ eu_bank.save_rates("/some/file/location/exchange_rates.xml")
38
+
39
+ # reads the rates from the specified location
40
+ eu_bank.update_rates("/some/file/location/exchange_rates.xml")
41
+
42
+ # exchange 100 CAD to USD as usual
43
+ eu_bank.exchange(100, "CAD", "USD") # 124
44
+
45
+ == Note on Patches/Pull Requests
46
+
47
+ * Fork the project.
48
+ * Make your feature addition or bug fix.
49
+ * Add tests for it. This is important so I don't break it in a
50
+ future version unintentionally.
51
+ * Commit, do not mess with rakefile, version, or history.
52
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
53
+
54
+ == Copyright
55
+
56
+ Copyright (c) 2010 Wong Liang Zan. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "eu_central_bank"
8
+ gem.summary = %Q{Calculates exchange rates based on rates from european central bank. Money gem compatible. }
9
+ gem.description = %Q{This gem reads exchange rates from the european central bank website. It uses it to calculates exchange rates. It is compatible with the money gem}
10
+ gem.email = "zan@liangzan.net"
11
+ gem.homepage = "http://github.com/liangzan/eu_central_bank"
12
+ gem.authors = ["Wong Liang Zan"]
13
+ gem.add_development_dependency "rspec", ">= 1.3.0"
14
+ gem.add_development_dependency "rr", ">= 0.10.11"
15
+ gem.add_development_dependency "shoulda", ">= 2.10.3"
16
+ gem.add_dependency "nokogiri", ">= 1.4.1"
17
+ gem.add_dependency "money", ">= 2.3.0"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "eu_central_bank #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{eu_central_bank}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Wong Liang Zan"]
12
+ s.date = %q{2010-04-21}
13
+ s.description = %q{This gem reads exchange rates from the european central bank website. It uses it to calculates exchange rates. It is compatible with the money gem}
14
+ s.email = %q{zan@liangzan.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "eu_central_bank.gemspec",
27
+ "lib/eu_central_bank.rb",
28
+ "spec/eu_central_bank_spec.rb",
29
+ "spec/exchange_rates.xml",
30
+ "spec/exchange_rates.yml",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/liangzan/eu_central_bank}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{Calculates exchange rates based on rates from european central bank. Money gem compatible.}
39
+ s.test_files = [
40
+ "spec/eu_central_bank_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
50
+ s.add_development_dependency(%q<rr>, [">= 0.10.11"])
51
+ s.add_development_dependency(%q<shoulda>, [">= 2.10.3"])
52
+ s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.1"])
53
+ s.add_runtime_dependency(%q<money>, [">= 2.3.0"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
56
+ s.add_dependency(%q<rr>, [">= 0.10.11"])
57
+ s.add_dependency(%q<shoulda>, [">= 2.10.3"])
58
+ s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
59
+ s.add_dependency(%q<money>, [">= 2.3.0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
63
+ s.add_dependency(%q<rr>, [">= 0.10.11"])
64
+ s.add_dependency(%q<shoulda>, [">= 2.10.3"])
65
+ s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
66
+ s.add_dependency(%q<money>, [">= 2.3.0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'money'
5
+
6
+ class InvalidCache < StandardError ; end
7
+
8
+ class EuCentralBank < Money::VariableExchangeBank
9
+
10
+ ECB_RATES_URL = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml'
11
+ CURRENCIES = %w(USD JPY BGN CZK DKK EEK GBP HUF LTL LVL PLN RON SEK CHF NOK HRK RUB TRY AUD BRL CAD CNY HKD IDR INR KRW MXN MYR NZD PHP SGD THB ZAR)
12
+
13
+ def update_rates(cache=nil)
14
+ exchange_rates(cache).each do |exchange_rate|
15
+ rate = exchange_rate.attribute("rate").value.to_f
16
+ currency = exchange_rate.attribute("currency").value
17
+ add_rate("EUR", currency, rate)
18
+ end
19
+ add_rate("EUR", "EUR", 1)
20
+ end
21
+
22
+ def save_rates(cache)
23
+ raise InvalidCache if !cache
24
+ File.open(cache, "w") do |file|
25
+ io = open(ECB_RATES_URL) ;
26
+ io.each_line {|line| file.puts line}
27
+ end
28
+ end
29
+
30
+ def exchange(cents, from_currency, to_currency)
31
+ rate = get_rate(from_currency, to_currency)
32
+ if !rate
33
+ from_base_rate = get_rate("EUR", from_currency)
34
+ to_base_rate = get_rate("EUR", to_currency)
35
+ rate = to_base_rate / from_base_rate
36
+ end
37
+ (cents * rate).floor
38
+ end
39
+
40
+ protected
41
+
42
+ def exchange_rates(cache=nil)
43
+ rates_source = !!cache ? cache : ECB_RATES_URL
44
+ doc = Nokogiri::XML(open(rates_source))
45
+ doc.xpath('gesmes:Envelope/xmlns:Cube/xmlns:Cube//xmlns:Cube')
46
+ end
47
+
48
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "EuCentralBank" do
4
+ before(:each) do
5
+ @bank = EuCentralBank.new
6
+ @cache_path = File.expand_path(File.dirname(__FILE__) + '/exchange_rates.xml')
7
+ @yml_cache_path = File.expand_path(File.dirname(__FILE__) + '/exchange_rates.yml')
8
+ @tmp_cache_path = File.expand_path(File.dirname(__FILE__) + '/tmp/exchange_rates.xml')
9
+ end
10
+
11
+ after(:each) do
12
+ if File.exists? @tmp_cache_path
13
+ File.delete @tmp_cache_path
14
+ end
15
+ end
16
+
17
+ it "should save the xml file from ecb given a file path" do
18
+ @bank.save_rates(@tmp_cache_path)
19
+ File.exists?(@tmp_cache_path).should == true
20
+ end
21
+
22
+ it "should raise an error if an invalid path is given to save_rates" do
23
+ lambda { @bank.save_rates(nil) }.should raise_exception
24
+ end
25
+
26
+ it "should update itself with exchange rates from ecb website" do
27
+ stub(OpenURI::OpenRead).open(EuCentralBank::ECB_RATES_URL) {@cache_path}
28
+ @bank.update_rates
29
+ EuCentralBank::CURRENCIES.each do |currency|
30
+ @bank.get_rate("EUR", currency).should > 0
31
+ end
32
+ end
33
+
34
+ it "should update itself with exchange rates from cache" do
35
+ @bank.update_rates(@cache_path)
36
+ EuCentralBank::CURRENCIES.each do |currency|
37
+ @bank.get_rate("EUR", currency).should > 0
38
+ end
39
+ end
40
+
41
+ it "should return the correct exchange rates" do
42
+ EXCHANGE_RATES = YAML.load_file(@yml_cache_path)
43
+ @bank.update_rates(@cache_path)
44
+ EuCentralBank::CURRENCIES.each do |currency|
45
+ @bank.exchange(100, "EUR", currency).should == (EXCHANGE_RATES["currencies"][currency].to_f * 100).floor
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,44 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
3
+ <gesmes:subject>Reference rates</gesmes:subject>
4
+ <gesmes:Sender>
5
+ <gesmes:name>European Central Bank</gesmes:name>
6
+ </gesmes:Sender>
7
+ <Cube>
8
+ <Cube time='2010-04-20'>
9
+ <Cube currency='USD' rate='1.3486'/>
10
+ <Cube currency='JPY' rate='125.32'/>
11
+ <Cube currency='BGN' rate='1.9558'/>
12
+ <Cube currency='CZK' rate='25.309'/>
13
+ <Cube currency='DKK' rate='7.4423'/>
14
+ <Cube currency='EEK' rate='15.6466'/>
15
+ <Cube currency='GBP' rate='0.87650'/>
16
+ <Cube currency='HUF' rate='264.93'/>
17
+ <Cube currency='LTL' rate='3.4528'/>
18
+ <Cube currency='LVL' rate='0.7074'/>
19
+ <Cube currency='PLN' rate='3.8861'/>
20
+ <Cube currency='RON' rate='4.1470'/>
21
+ <Cube currency='SEK' rate='9.6450'/>
22
+ <Cube currency='CHF' rate='1.4334'/>
23
+ <Cube currency='NOK' rate='7.9475'/>
24
+ <Cube currency='HRK' rate='7.2542'/>
25
+ <Cube currency='RUB' rate='39.2535'/>
26
+ <Cube currency='TRY' rate='2.0064'/>
27
+ <Cube currency='AUD' rate='1.4478'/>
28
+ <Cube currency='BRL' rate='2.3576'/>
29
+ <Cube currency='CAD' rate='1.3665'/>
30
+ <Cube currency='CNY' rate='9.2047'/>
31
+ <Cube currency='HKD' rate='10.4688'/>
32
+ <Cube currency='IDR' rate='12141.23'/>
33
+ <Cube currency='INR' rate='60.0060'/>
34
+ <Cube currency='KRW' rate='1507.69'/>
35
+ <Cube currency='MXN' rate='16.4647'/>
36
+ <Cube currency='MYR' rate='4.3209'/>
37
+ <Cube currency='NZD' rate='1.8961'/>
38
+ <Cube currency='PHP' rate='60.094'/>
39
+ <Cube currency='SGD' rate='1.8551'/>
40
+ <Cube currency='THB' rate='43.374'/>
41
+ <Cube currency='ZAR' rate='10.0235'/>
42
+ </Cube>
43
+ </Cube>
44
+ </gesmes:Envelope>
@@ -0,0 +1,34 @@
1
+ currencies:
2
+ USD: 1.3486
3
+ JPY: 125.32
4
+ BGN: 1.9558
5
+ CZK: 25.309
6
+ DKK: 7.4423
7
+ EEK: 15.6466
8
+ GBP: 0.87650
9
+ HUF: 264.93
10
+ LTL: 3.4528
11
+ LVL: 0.7074
12
+ PLN: 3.8861
13
+ RON: 4.1470
14
+ SEK: 9.6450
15
+ CHF: 1.4334
16
+ NOK: 7.9475
17
+ HRK: 7.2542
18
+ RUB: 39.2535
19
+ TRY: 2.0064
20
+ AUD: 1.4478
21
+ BRL: 2.3576
22
+ CAD: 1.3665
23
+ CNY: 9.2047
24
+ HKD: 10.4688
25
+ IDR: 12141.23
26
+ INR: 60.0060
27
+ KRW: 1507.69
28
+ MXN: 16.4647
29
+ MYR: 4.3209
30
+ NZD: 1.8961
31
+ PHP: 60.094
32
+ SGD: 1.8551
33
+ THB: 43.374
34
+ ZAR: 10.0235
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'eu_central_bank'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'shoulda'
7
+ require 'rr'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :rr
11
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eu_central_bank
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
+ - Wong Liang Zan
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-21 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rr
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 10
44
+ - 11
45
+ version: 0.10.11
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: shoulda
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 2
57
+ - 10
58
+ - 3
59
+ version: 2.10.3
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: nokogiri
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 1
71
+ - 4
72
+ - 1
73
+ version: 1.4.1
74
+ type: :runtime
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: money
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 2
85
+ - 3
86
+ - 0
87
+ version: 2.3.0
88
+ type: :runtime
89
+ version_requirements: *id005
90
+ description: This gem reads exchange rates from the european central bank website. It uses it to calculates exchange rates. It is compatible with the money gem
91
+ email: zan@liangzan.net
92
+ executables: []
93
+
94
+ extensions: []
95
+
96
+ extra_rdoc_files:
97
+ - LICENSE
98
+ - README.rdoc
99
+ files:
100
+ - .document
101
+ - .gitignore
102
+ - LICENSE
103
+ - README.rdoc
104
+ - Rakefile
105
+ - VERSION
106
+ - eu_central_bank.gemspec
107
+ - lib/eu_central_bank.rb
108
+ - spec/eu_central_bank_spec.rb
109
+ - spec/exchange_rates.xml
110
+ - spec/exchange_rates.yml
111
+ - spec/spec.opts
112
+ - spec/spec_helper.rb
113
+ has_rdoc: true
114
+ homepage: http://github.com/liangzan/eu_central_bank
115
+ licenses: []
116
+
117
+ post_install_message:
118
+ rdoc_options:
119
+ - --charset=UTF-8
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ requirements: []
137
+
138
+ rubyforge_project:
139
+ rubygems_version: 1.3.6
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Calculates exchange rates based on rates from european central bank. Money gem compatible.
143
+ test_files:
144
+ - spec/eu_central_bank_spec.rb
145
+ - spec/spec_helper.rb