nbrb_currency 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
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,31 @@
1
+ = nbrb_currency
2
+
3
+ == Introduction
4
+
5
+ This gem downloads the exchange rates from the National Bank of the Republic of Belarus. You can calculate exchange rates with it. It is compatible with the money gem.
6
+
7
+ == Installation
8
+
9
+ $ gem install nbrb_currency
10
+
11
+ == Dependencies
12
+
13
+ * nokogiri
14
+ * money
15
+
16
+ == Usage
17
+
18
+ Check documentation for https://github.com/RubyMoney/eu_central_bank, it's almost the same
19
+
20
+ == Note on Patches/Pull Requests
21
+
22
+ * Fork the project.
23
+ * Make your feature addition or bug fix.
24
+ * Add tests for it. This is important so I don't break it in a
25
+ future version unintentionally.
26
+ * Commit, do not mess with rakefile, version, or history.
27
+ (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)
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2010 RubyMoney. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+ rescue LoadError
7
+ task(:spec){abort "`gem install rspec` to run specs"}
8
+ end
9
+
10
+ task :default => :spec
11
+
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'money'
5
+
6
+ class InvalidCache < StandardError ; end
7
+
8
+ class NbrbCurrency < Money::Bank::VariableExchange
9
+
10
+ NBRB_RATES_URL = 'http://nbrb.by/Services/XmlExRates.aspx'
11
+ CURRENCIES = %w(AUD BGN UAH DKK USD EUR PLN JPY IRR ISK CAD CNY KWD LVL LTL MDL NOK RUB SGD KGS KZT TRY GBP CZK SEK CHF)
12
+
13
+ def update_rates(cache=nil)
14
+ exchange_rates(cache).each do |exchange_rate|
15
+ rate = exchange_rate.xpath("Rate").text
16
+ currency = exchange_rate.xpath("CharCode").text
17
+ scale = exchange_rate.xpath("Scale").text
18
+ next if currency == "XDR"
19
+ add_rate(currency, "BYR", (BigDecimal.new(rate) / BigDecimal.new(scale)).to_f)
20
+ end
21
+ add_rate("BYR", "BYR", 1)
22
+ end
23
+
24
+ def save_rates(cache)
25
+ raise InvalidCache if !cache
26
+ File.open(cache, "w") do |file|
27
+ io = open(NBRB_RATES_URL) ;
28
+ io.each_line {|line| file.puts line}
29
+ end
30
+ end
31
+
32
+ def exchange(cents, from_currency, to_currency)
33
+ exchange_with(Money.new(cents, from_currency), to_currency)
34
+ end
35
+
36
+ def exchange_with(from, to_currency)
37
+ rate = get_rate(from.currency, to_currency)
38
+ unless rate
39
+ from_base_rate = get_rate(from.currency, "BYR")
40
+ to_base_rate = get_rate(to_currency, "BYR")
41
+ rate = (BigDecimal.new(from_base_rate, 8) / BigDecimal.new(to_base_rate, 8)).to_f
42
+ raise "Rate #{from.currency} - #{to_currency} unknown!" unless rate
43
+ end
44
+ Money.new(((Money::Currency.wrap(to_currency).subunit_to_unit.to_f / from.currency.subunit_to_unit.to_f) * from.cents * rate).round, to_currency)
45
+ end
46
+
47
+ protected
48
+
49
+ def exchange_rates(cache=nil)
50
+ rates_source = !!cache ? cache : NBRB_RATES_URL
51
+ doc = Nokogiri::XML(open(rates_source))
52
+ doc.xpath('DailyExRates//Currency')
53
+ end
54
+
55
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "nbrb_currency"
6
+ s.version = "1.0.0"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Aleks Grebennik"]
9
+ s.email = ["aleks.grebennik@gmail.com"]
10
+ s.homepage = "http://github.com/slbug/nbrb_currency"
11
+ s.summary = %q{Calculates exchange rates based on rates from National Bank of the Republic of Belarus. Money gem compatible.}
12
+ s.description = %q{This gem reads exchange rates from the National Bank of the Republic of Belarus website. It uses it to calculates exchange rates. It is compatible with the money gem}
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+
16
+ s.add_dependency "nokogiri"
17
+ s.add_dependency "money", "~> 3.7.1"
18
+
19
+ s.add_development_dependency "rspec", ">= 2.0.0"
20
+ s.add_development_dependency "rr"
21
+ s.add_development_dependency "shoulda"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,192 @@
1
+ <?xml version="1.0" encoding="windows-1251"?>
2
+ <DailyExRates Date="10/26/2011">
3
+ <Currency Id="170">
4
+ <NumCode>036</NumCode>
5
+ <CharCode>AUD</CharCode>
6
+ <Scale>1</Scale>
7
+ <Name>������������� ������</Name>
8
+ <Rate>8920.87</Rate>
9
+ </Currency>
10
+ <Currency Id="191">
11
+ <NumCode>975</NumCode>
12
+ <CharCode>BGN</CharCode>
13
+ <Scale>1</Scale>
14
+ <Name>������c��� ���</Name>
15
+ <Rate>6059.74</Rate>
16
+ </Currency>
17
+ <Currency Id="224">
18
+ <NumCode>980</NumCode>
19
+ <CharCode>UAH</CharCode>
20
+ <Scale>1</Scale>
21
+ <Name>������</Name>
22
+ <Rate>1064.87</Rate>
23
+ </Currency>
24
+ <Currency Id="36">
25
+ <NumCode>208</NumCode>
26
+ <CharCode>DKK</CharCode>
27
+ <Scale>1</Scale>
28
+ <Name>������� �����</Name>
29
+ <Rate>1592.06</Rate>
30
+ </Currency>
31
+ <Currency Id="145">
32
+ <NumCode>840</NumCode>
33
+ <CharCode>USD</CharCode>
34
+ <Scale>1</Scale>
35
+ <Name>������ ���</Name>
36
+ <Rate>8520</Rate>
37
+ </Currency>
38
+ <Currency Id="19">
39
+ <NumCode>978</NumCode>
40
+ <CharCode>EUR</CharCode>
41
+ <Scale>1</Scale>
42
+ <Name>����</Name>
43
+ <Rate>11860</Rate>
44
+ </Currency>
45
+ <Currency Id="166">
46
+ <NumCode>960</NumCode>
47
+ <CharCode>XDR</CharCode>
48
+ <Scale>1</Scale>
49
+ <Name>������� ��� (SDR) �� ���</Name>
50
+ <Rate>13467.99</Rate>
51
+ </Currency>
52
+ <Currency Id="219">
53
+ <NumCode>985</NumCode>
54
+ <CharCode>PLN</CharCode>
55
+ <Scale>1</Scale>
56
+ <Name>������</Name>
57
+ <Rate>2714.33</Rate>
58
+ </Currency>
59
+ <Currency Id="67">
60
+ <NumCode>392</NumCode>
61
+ <CharCode>JPY</CharCode>
62
+ <Scale>10</Scale>
63
+ <Name>����</Name>
64
+ <Rate>1119.43</Rate>
65
+ </Currency>
66
+ <Currency Id="264">
67
+ <NumCode>364</NumCode>
68
+ <CharCode>IRR</CharCode>
69
+ <Scale>1000</Scale>
70
+ <Name>�������� ����</Name>
71
+ <Rate>794.63</Rate>
72
+ </Currency>
73
+ <Currency Id="58">
74
+ <NumCode>352</NumCode>
75
+ <CharCode>ISK</CharCode>
76
+ <Scale>1</Scale>
77
+ <Name>���������� �����</Name>
78
+ <Rate>74.54</Rate>
79
+ </Currency>
80
+ <Currency Id="23">
81
+ <NumCode>124</NumCode>
82
+ <CharCode>CAD</CharCode>
83
+ <Scale>1</Scale>
84
+ <Name>��������� ������</Name>
85
+ <Rate>8494.94</Rate>
86
+ </Currency>
87
+ <Currency Id="254">
88
+ <NumCode>156</NumCode>
89
+ <CharCode>CNY</CharCode>
90
+ <Scale>1</Scale>
91
+ <Name>��������� ����</Name>
92
+ <Rate>1339.46</Rate>
93
+ </Currency>
94
+ <Currency Id="72">
95
+ <NumCode>414</NumCode>
96
+ <CharCode>KWD</CharCode>
97
+ <Scale>1</Scale>
98
+ <Name>���������� �����</Name>
99
+ <Rate>30996.47</Rate>
100
+ </Currency>
101
+ <Currency Id="176">
102
+ <NumCode>428</NumCode>
103
+ <CharCode>LVL</CharCode>
104
+ <Scale>1</Scale>
105
+ <Name>���������� ���</Name>
106
+ <Rate>16836.28</Rate>
107
+ </Currency>
108
+ <Currency Id="177">
109
+ <NumCode>440</NumCode>
110
+ <CharCode>LTL</CharCode>
111
+ <Scale>1</Scale>
112
+ <Name>��������� ���</Name>
113
+ <Rate>3432.51</Rate>
114
+ </Currency>
115
+ <Currency Id="178">
116
+ <NumCode>498</NumCode>
117
+ <CharCode>MDL</CharCode>
118
+ <Scale>1</Scale>
119
+ <Name>���������� ���</Name>
120
+ <Rate>730.39</Rate>
121
+ </Currency>
122
+ <Currency Id="101">
123
+ <NumCode>578</NumCode>
124
+ <CharCode>NOK</CharCode>
125
+ <Scale>1</Scale>
126
+ <Name>���������� �����</Name>
127
+ <Rate>1544.71</Rate>
128
+ </Currency>
129
+ <Currency Id="190">
130
+ <NumCode>643</NumCode>
131
+ <CharCode>RUB</CharCode>
132
+ <Scale>1</Scale>
133
+ <Name>���������� �����</Name>
134
+ <Rate>279</Rate>
135
+ </Currency>
136
+ <Currency Id="119">
137
+ <NumCode>702</NumCode>
138
+ <CharCode>SGD</CharCode>
139
+ <Scale>1</Scale>
140
+ <Name>��������c��� ������</Name>
141
+ <Rate>6743.71</Rate>
142
+ </Currency>
143
+ <Currency Id="223">
144
+ <NumCode>417</NumCode>
145
+ <CharCode>KGS</CharCode>
146
+ <Scale>1</Scale>
147
+ <Name>���</Name>
148
+ <Rate>187.97</Rate>
149
+ </Currency>
150
+ <Currency Id="222">
151
+ <NumCode>398</NumCode>
152
+ <CharCode>KZT</CharCode>
153
+ <Scale>1</Scale>
154
+ <Name>�����</Name>
155
+ <Rate>57.58</Rate>
156
+ </Currency>
157
+ <Currency Id="256">
158
+ <NumCode>949</NumCode>
159
+ <CharCode>TRY</CharCode>
160
+ <Scale>1</Scale>
161
+ <Name>�������� ����</Name>
162
+ <Rate>4714.74</Rate>
163
+ </Currency>
164
+ <Currency Id="143">
165
+ <NumCode>826</NumCode>
166
+ <CharCode>GBP</CharCode>
167
+ <Scale>1</Scale>
168
+ <Name>���� ����������</Name>
169
+ <Rate>13618.37</Rate>
170
+ </Currency>
171
+ <Currency Id="171">
172
+ <NumCode>203</NumCode>
173
+ <CharCode>CZK</CharCode>
174
+ <Scale>1</Scale>
175
+ <Name>������� �����</Name>
176
+ <Rate>475.9</Rate>
177
+ </Currency>
178
+ <Currency Id="129">
179
+ <NumCode>752</NumCode>
180
+ <CharCode>SEK</CharCode>
181
+ <Scale>1</Scale>
182
+ <Name>�������� �����</Name>
183
+ <Rate>1300.6</Rate>
184
+ </Currency>
185
+ <Currency Id="130">
186
+ <NumCode>756</NumCode>
187
+ <CharCode>CHF</CharCode>
188
+ <Scale>1</Scale>
189
+ <Name>����������� �����</Name>
190
+ <Rate>9684.57</Rate>
191
+ </Currency>
192
+ </DailyExRates>
@@ -0,0 +1,28 @@
1
+ currencies:
2
+ AUD: 8920.87
3
+ BGN: 6059.74
4
+ UAH: 1064.87
5
+ DKK: 1592.06
6
+ USD: 8520
7
+ EUR: 11860
8
+ PLN: 2714.33
9
+ JPY: 111.943
10
+ IRR: 0.79463
11
+ ISK: 74.54
12
+ CAD: 8494.94
13
+ CNY: 1339.46
14
+ KWD: 30996.47
15
+ LVL: 16836.28
16
+ LTL: 3432.51
17
+ MDL: 730.39
18
+ NOK: 1544.71
19
+ RUB: 279
20
+ SGD: 6743.71
21
+ KGS: 187.97
22
+ KZT: 57.58
23
+ TRY: 4714.74
24
+ GBP: 13618.37
25
+ CZK: 475.9
26
+ SEK: 1300.6
27
+ CHF: 9684.57
28
+
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'yaml'
3
+
4
+ describe "NbrbCurrency" do
5
+ before(:each) do
6
+ @bank = NbrbCurrency.new
7
+ @cache_path = File.expand_path(File.dirname(__FILE__) + '/exchange_rates.xml')
8
+ @yml_cache_path = File.expand_path(File.dirname(__FILE__) + '/exchange_rates.yml')
9
+ @tmp_cache_path = File.expand_path(File.dirname(__FILE__) + '/tmp/exchange_rates.xml')
10
+ @exchange_rates = YAML.load_file(@yml_cache_path)
11
+ end
12
+
13
+ after(:each) do
14
+ if File.exists? @tmp_cache_path
15
+ File.delete @tmp_cache_path
16
+ end
17
+ end
18
+
19
+ it "should save the xml file from nbrb given a file path" do
20
+ @bank.save_rates(@tmp_cache_path)
21
+ File.exists?(@tmp_cache_path).should == true
22
+ end
23
+
24
+ it "should raise an error if an invalid path is given to save_rates" do
25
+ lambda { @bank.save_rates(nil) }.should raise_exception
26
+ end
27
+
28
+ it "should update itself with exchange rates from nbrb website" do
29
+ stub(OpenURI::OpenRead).open(NbrbCurrency::NBRB_RATES_URL) {@cache_path}
30
+ @bank.update_rates
31
+ NbrbCurrency::CURRENCIES.each do |currency|
32
+ @bank.get_rate(currency, "BYR").should > 0
33
+ end
34
+ end
35
+
36
+ it "should update itself with exchange rates from cache" do
37
+ @bank.update_rates(@cache_path)
38
+ NbrbCurrency::CURRENCIES.each do |currency|
39
+ @bank.get_rate(currency, "BYR").should > 0
40
+ end
41
+ end
42
+
43
+ it "should return the correct exchange rates using exchange" do
44
+ @bank.update_rates(@cache_path)
45
+ NbrbCurrency::CURRENCIES.reject{|c| %w{JPY KWD}.include?(c) }.each do |currency|
46
+ @bank.exchange(100, currency, "BYR").cents.should == (@exchange_rates["currencies"][currency].to_f * 100).round
47
+ end
48
+ subunit = Money::Currency.wrap("KWD").subunit_to_unit.to_f
49
+ @bank.exchange(1000, "KWD", "BYR").cents.should == ((subunit / 1000) * @exchange_rates["currencies"]['KWD'].to_f * 100).round
50
+ subunit = Money::Currency.wrap("JPY").subunit_to_unit.to_f
51
+ @bank.exchange(100, "JPY", "BYR").cents.should == ((subunit / 100) * @exchange_rates["currencies"]['JPY'].to_f * 100).round
52
+ end
53
+
54
+ it "should return the correct exchange rates using exchange_with" do
55
+ @bank.update_rates(@cache_path)
56
+ NbrbCurrency::CURRENCIES.reject{|c| %w{JPY KWD}.include?(c) }.each do |currency|
57
+ @bank.exchange_with(Money.new(100, currency), "BYR").cents.should == (@exchange_rates["currencies"][currency].to_f * 100).round
58
+ @bank.exchange_with(1.to_money(currency), "BYR").cents.should == (@exchange_rates["currencies"][currency].to_f * 100).round
59
+ end
60
+ @bank.exchange_with(5000.to_money('JPY'), 'BYR').cents.should == 55971500 # 559715 BYR
61
+ end
62
+
63
+ # in response to #4
64
+ it "should exchange btc" do
65
+ Money::Currency::TABLE[:btc] = {
66
+ :priority => 1,
67
+ :iso_code => "BTC",
68
+ :name => "Bitcoin",
69
+ :symbol => "BTC",
70
+ :subunit => "Cent",
71
+ :subunit_to_unit => 1000,
72
+ :separator => ".",
73
+ :delimiter => ","
74
+ }
75
+ @bank.add_rate("USD", "BTC", 1 / 13.7603)
76
+ @bank.add_rate("BTC", "USD", 13.7603)
77
+ @bank.exchange(100, "BTC", "USD").cents.should == 138
78
+ end
79
+ end
@@ -0,0 +1,7 @@
1
+ require 'nbrb_currency'
2
+ require 'shoulda'
3
+ require 'rr'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rr
7
+ end
data/spec/tmp/.gitkeep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nbrb_currency
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aleks Grebennik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &9840700 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *9840700
25
+ - !ruby/object:Gem::Dependency
26
+ name: money
27
+ requirement: &9840100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.7.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *9840100
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &9839060 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 2.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *9839060
47
+ - !ruby/object:Gem::Dependency
48
+ name: rr
49
+ requirement: &9838220 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *9838220
58
+ - !ruby/object:Gem::Dependency
59
+ name: shoulda
60
+ requirement: &9836840 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *9836840
69
+ description: This gem reads exchange rates from the National Bank of the Republic
70
+ of Belarus website. It uses it to calculates exchange rates. It is compatible with
71
+ the money gem
72
+ email:
73
+ - aleks.grebennik@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.rdoc
82
+ - Rakefile
83
+ - lib/nbrb_currency.rb
84
+ - nbrb_currency.gemspec
85
+ - spec/exchange_rates.xml
86
+ - spec/exchange_rates.yml
87
+ - spec/nbrb_currency_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/tmp/.gitkeep
90
+ homepage: http://github.com/slbug/nbrb_currency
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: 1.3.6
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.11
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Calculates exchange rates based on rates from National Bank of the Republic
114
+ of Belarus. Money gem compatible.
115
+ test_files: []