national_bank_of_romania 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in national_bank_of_romania.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vlad Suciu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # NationalBankOfRomania
2
+
3
+
4
+ This gem provides the ability to download the exchange rates from the National Bank of Romania and it is compatible with the [money gem](https://github.com/RubyMoney/money/).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'national_bank_of_romania'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install national_bank_of_romania
19
+
20
+ ## Dependencies
21
+
22
+ * nokogiri
23
+ * money
24
+
25
+ ## Usage
26
+
27
+ You can use it together with the money gem. The money gem accepts an exchange bank object:
28
+
29
+ ro_bank = NationalBankOfRomania::Bank.new
30
+
31
+ # scrapes bnr.ro for the current rates
32
+ ro_bank.update_rates
33
+
34
+ # change the default bank
35
+ Money.default_bank = ro_bank
36
+
37
+ # exchange API examples
38
+ Money.us_dollar(100).exchange_to("CAD")
39
+ # or
40
+ money1 = Money.new(100, "RON")
41
+ money1.exchange_to("RON")
42
+
43
+ Calling ```update_rates``` will automatically populate the object with the rates from the National Bank of Romania's official website.
44
+
45
+ The gem is a wrapper around the money library therefore you can simply use it like so:
46
+
47
+ ro_bank = NationalBankOfRomania::Bank.new
48
+
49
+ # scrapes bnr.ro for the current rates
50
+ ro_bank.update_rates
51
+
52
+ # exchange 100 RON to USD
53
+ ro_bank.exchange(100, "RON", "USD")
54
+
55
+ ### Performance Considerations
56
+
57
+ The currency rates are updated once a day hence that it makes sense to save them in a file.
58
+
59
+ # saves the rates in a specified location
60
+ ro_bank = NationalBankOfRomania::Bank.new
61
+ ro_bank.save_rates("/path/to/rates.xml")
62
+
63
+ # calling update_rates with a path to load the rates from
64
+ ro_bank.update_rates("/path/to/rates.xml")
65
+
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
74
+
75
+ ## Special Thanks
76
+
77
+ NationalBankOfRomania was inspired by [eu_central_bank](https://github.com/RubyMoney/eu_central_bank) and they would both not be possible without the popular [money gem](https://github.com/RubyMoney/money/).
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,45 @@
1
+ require "money"
2
+ module NationalBankOfRomania
3
+
4
+ class Bank < ::Money::Bank::VariableExchange
5
+ attr_accessor :last_updated
6
+
7
+ RATES_URL = "http://www.bnro.ro/nbrfxrates.xml"
8
+ CURRENCIES = %w(AED AUD BGN BRL CAD CHF CNY CZK DKK EGP EUR GBP HUF INR JPY
9
+ KRW MDL MXN NOK NZD PLN RSD RUB SEK TRY UAH USD ZAR)
10
+
11
+ def save_rates(path_to_file)
12
+ cache.save(path_to_file)
13
+ end
14
+
15
+ def update_rates(path_to_file = nil)
16
+ exchange_rates(path_to_file).each do |rate|
17
+ next unless CURRENCIES.include?(rate[:currency])
18
+ add_rate("RON", rate[:currency], rate[:value])
19
+ end
20
+
21
+ add_rate("RON", "RON", 1)
22
+ @last_updated = Time.now
23
+ end
24
+
25
+ def exchange(cents, from_currency, other_currency)
26
+ from_currency = Money.new(cents, from_currency)
27
+ other_currency = Money::Currency.wrap(other_currency)
28
+ exchange_with(from_currency, other_currency)
29
+ end
30
+
31
+ def parser
32
+ @parser ||= Parser.new(RATES_URL)
33
+ end
34
+
35
+ def cache
36
+ @cache ||= Cache.new(RATES_URL)
37
+ end
38
+
39
+ private
40
+ def exchange_rates(path_to_file = nil)
41
+ parser.records(path_to_file)
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,21 @@
1
+ require "open-uri"
2
+
3
+ module NationalBankOfRomania
4
+ class InvalidCache < StandardError ; end
5
+ class Cache
6
+ attr_reader :url
7
+
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def save(path_to_file)
13
+ raise InvalidCache unless path_to_file
14
+
15
+ File.open(path_to_file, "w") do |file|
16
+ io = open(url) ;
17
+ io.each_line {|line| file.puts line}
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ require "national_bank_of_romania/version"
2
+
3
+ require "bank"
4
+ require "parser"
5
+ require "cache"
@@ -0,0 +1,3 @@
1
+ module NationalBankOfRomania
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ require "nokogiri"
2
+
3
+ module NationalBankOfRomania
4
+ class Parser
5
+ attr_reader :url
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ @records = nil
10
+ end
11
+
12
+ def records(cache)
13
+ source = !!cache ? cache : url
14
+ doc = Nokogiri::XML(open(source))
15
+ @records = doc.root.elements[1].elements[2].elements
16
+ formatted_records
17
+ end
18
+
19
+ private
20
+ def formatted_records
21
+ @records.map do |row|
22
+ { currency: row.attribute("currency").value, value: row.text.to_f }
23
+ end
24
+ end
25
+ end
26
+ end
data/mvim ADDED
@@ -0,0 +1 @@
1
+ /Users/vladsuciu/.rbenv/versions/1.9.3-p362/lib/ruby/gems/1.9.1/gems/money-5.1.0
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'national_bank_of_romania/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "national_bank_of_romania"
8
+ gem.version = NationalBankOfRomania::VERSION
9
+ gem.authors = ["Vlad Suciu"]
10
+ gem.email = ["suciu.vlad@gmail.com"]
11
+ gem.description = %q{This gem provides the ability to download the exchange rates from the National Bank of Romania and it is compatible with the money gem.}
12
+ gem.summary = %q{This gem provides the ability to download the exchange rates from the National Bank of Romania and it is compatible with the money gem.}
13
+ gem.homepage = ""
14
+
15
+ gem.add_dependency "nokogiri", "~> 1.5.6"
16
+ gem.add_dependency "money", "~> 5.1.0"
17
+
18
+ gem.add_development_dependency "rspec", ">= 2.12.0"
19
+ gem.add_development_dependency "rake"
20
+
21
+ gem.files = `git ls-files`.split($/)
22
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
23
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
24
+ gem.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+ require "national_bank_of_romania"
3
+
4
+ describe "Bank" do
5
+
6
+ before(:each) do
7
+ @bank = NationalBankOfRomania::Bank.new
8
+ @feed = File.expand_path(File.dirname(__FILE__) + '/fixtures/nbrfxrates.xml')
9
+ @tmp = File.expand_path(File.dirname(__FILE__) + '/fixtures/tmp_nbrfxrates.xml')
10
+
11
+ @yml = File.expand_path(File.dirname(__FILE__) + '/fixtures/exchange_rates.yml')
12
+ @exchange_rates = YAML.load_file(@yml)
13
+
14
+ @bank.stub(:open).and_return(File.read(@feed))
15
+ end
16
+
17
+ it "calls 'save' on the Cache" do
18
+ @bank.cache.should_receive(:save).with(@tmp)
19
+ @bank.save_rates(@tmp)
20
+ end
21
+
22
+ context "#update_rates" do
23
+ it "from BNR unless a local feed is given" do
24
+ @bank.update_rates
25
+
26
+ NationalBankOfRomania::Bank::CURRENCIES.each do |currency|
27
+ @bank.get_rate("RON", currency).should > 0
28
+ end
29
+ end
30
+
31
+ it "from cache if a local feed is given" do
32
+ @bank.update_rates(@feed)
33
+
34
+ NationalBankOfRomania::Bank::CURRENCIES.each do |currency|
35
+ @bank.get_rate("RON", currency).should > 0
36
+ end
37
+ end
38
+ end
39
+
40
+ it "should set last_updated when the rates are updated" do
41
+ bank1 = @bank.last_updated
42
+ @bank.update_rates(@feed)
43
+
44
+ bank2 = @bank.last_updated
45
+ @bank.update_rates(@feed)
46
+
47
+ bank3 = @bank.last_updated
48
+
49
+ bank1.should_not eq(bank2)
50
+ bank2.should_not eq(bank3)
51
+ end
52
+
53
+ it "should return the correct exchange rates using exchange" do
54
+ @bank.update_rates(@feed)
55
+
56
+ NationalBankOfRomania::Bank::CURRENCIES.each do |currency|
57
+ subunit = Money::Currency.wrap(currency).subunit_to_unit.to_s.scan(/0/).count
58
+ value = BigDecimal.new(@exchange_rates["currencies"][currency].to_s).truncate(subunit.to_i).to_f
59
+ @bank.exchange(100, "RON", currency).to_f.should == value
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+ require "cache"
3
+
4
+ describe "cache" do
5
+
6
+ before(:each) do
7
+ @bank = NationalBankOfRomania::Bank.new
8
+ @feed = File.expand_path(File.dirname(__FILE__) + '/fixtures/nbrfxrates.xml')
9
+ @tmp = File.expand_path(File.dirname(__FILE__) + '/fixtures/tmp_nbrfxrates.xml')
10
+
11
+ @bank.stub(:open).and_return(File.read(@feed))
12
+ end
13
+
14
+ after(:each) do
15
+ if File.exists? @tmp
16
+ File.delete @tmp
17
+ end
18
+ end
19
+
20
+ context "#save" do
21
+ it "the xml file given a file path" do
22
+ @bank.save_rates(@tmp)
23
+ File.exists?(@tmp).should === true
24
+ end
25
+
26
+ it "raise an error if an invalid path is given" do
27
+ lambda { @bank.save_rates(nil) }.should raise_exception
28
+ end
29
+ end
30
+
31
+
32
+ end
@@ -0,0 +1,31 @@
1
+ currencies:
2
+ AED: 0.8830
3
+ AUD: 3.4109
4
+ BGN: 2.2165
5
+ BRL: 1.5876
6
+ CAD: 3.2824
7
+ CHF: 3.4827
8
+ CNY: 0.5217
9
+ CZK: 0.1696
10
+ DKK: 0.5809
11
+ EGP: 0.4912
12
+ EUR: 4.3351
13
+ GBP: 5.1961
14
+ HUF: 1.4812
15
+ INR: 0.0597
16
+ JPY: 3.6283
17
+ KRW: 0.3065
18
+ MDL: 0.2672
19
+ MXN: 0.2575
20
+ NOK: 0.5843
21
+ NZD: 2.7223
22
+ PLN: 1.0517
23
+ RSD: 0.0386
24
+ RUB: 0.1073
25
+ SEK: 0.5007
26
+ TRY: 1.8461
27
+ UAH: 0.3991
28
+ USD: 3.2431
29
+ XAU: 175.6294
30
+ XDR: 4.9900
31
+ ZAR: 0.3697
@@ -0,0 +1,45 @@
1
+
2
+ <?xml version="1.0" encoding="utf-8"?>
3
+ <DataSet xmlns="http://www.bnr.ro/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd">
4
+ <Header>
5
+ <Publisher>National Bank of Romania</Publisher>
6
+ <PublishingDate>2013-01-17</PublishingDate>
7
+ <MessageType>DR</MessageType>
8
+ </Header>
9
+ <Body>
10
+ <Subject>Reference rates</Subject>
11
+ <OrigCurrency>RON</OrigCurrency>
12
+ <Cube date="2013-01-17">
13
+ <Rate currency="AED">0.8830</Rate>
14
+ <Rate currency="AUD">3.4109</Rate>
15
+ <Rate currency="BGN">2.2165</Rate>
16
+ <Rate currency="BRL">1.5876</Rate>
17
+ <Rate currency="CAD">3.2824</Rate>
18
+ <Rate currency="CHF">3.4827</Rate>
19
+ <Rate currency="CNY">0.5217</Rate>
20
+ <Rate currency="CZK">0.1696</Rate>
21
+ <Rate currency="DKK">0.5809</Rate>
22
+ <Rate currency="EGP">0.4912</Rate>
23
+ <Rate currency="EUR">4.3351</Rate>
24
+ <Rate currency="GBP">5.1961</Rate>
25
+ <Rate currency="HUF" multiplier="100">1.4812</Rate>
26
+ <Rate currency="INR">0.0597</Rate>
27
+ <Rate currency="JPY" multiplier="100">3.6283</Rate>
28
+ <Rate currency="KRW" multiplier="100">0.3065</Rate>
29
+ <Rate currency="MDL">0.2672</Rate>
30
+ <Rate currency="MXN">0.2575</Rate>
31
+ <Rate currency="NOK">0.5843</Rate>
32
+ <Rate currency="NZD">2.7223</Rate>
33
+ <Rate currency="PLN">1.0517</Rate>
34
+ <Rate currency="RSD">0.0386</Rate>
35
+ <Rate currency="RUB">0.1073</Rate>
36
+ <Rate currency="SEK">0.5007</Rate>
37
+ <Rate currency="TRY">1.8461</Rate>
38
+ <Rate currency="UAH">0.3991</Rate>
39
+ <Rate currency="USD">3.2431</Rate>
40
+ <Rate currency="XAU">175.6294</Rate>
41
+ <Rate currency="XDR">4.9900</Rate>
42
+ <Rate currency="ZAR">0.3697</Rate>
43
+ </Cube>
44
+ </Body>
45
+ </DataSet>
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: national_bank_of_romania
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vlad Suciu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.6
30
+ - !ruby/object:Gem::Dependency
31
+ name: money
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 5.1.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 5.1.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.12.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.12.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: This gem provides the ability to download the exchange rates from the
79
+ National Bank of Romania and it is compatible with the money gem.
80
+ email:
81
+ - suciu.vlad@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - CHANGELOG.md
89
+ - Gemfile
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/bank.rb
94
+ - lib/cache.rb
95
+ - lib/national_bank_of_romania.rb
96
+ - lib/national_bank_of_romania/version.rb
97
+ - lib/parser.rb
98
+ - mvim
99
+ - national_bank_of_romania.gemspec
100
+ - spec/bank_spec.rb
101
+ - spec/cache_spec.rb
102
+ - spec/fixtures/exchange_rates.yml
103
+ - spec/fixtures/nbrfxrates.xml
104
+ - spec/spec_helper.rb
105
+ homepage: ''
106
+ licenses: []
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.23
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: This gem provides the ability to download the exchange rates from the National
129
+ Bank of Romania and it is compatible with the money gem.
130
+ test_files:
131
+ - spec/bank_spec.rb
132
+ - spec/cache_spec.rb
133
+ - spec/fixtures/exchange_rates.yml
134
+ - spec/fixtures/nbrfxrates.xml
135
+ - spec/spec_helper.rb