russian_central_bank 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in russian_central_bank.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ramil Mustafin
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # RussianCentralBank
2
+
3
+ This gem extends Money::Bank::VariableExchange with Money::Bank::RussianCentralBank and gives acceess to the Central Bank of Russia currency exchange.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'russian_central_bank'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install russian_central_bank
18
+
19
+ ##Dependencies
20
+
21
+ * savon
22
+ * money
23
+
24
+ ## Usage
25
+
26
+ bank = Money::Bank::RussianCentralBank
27
+
28
+ # Load rates
29
+ bank.update_rates
30
+
31
+ Money.default_bank = bank
32
+
33
+ # Exchange 100 USD to RUB
34
+ 100.to_money('USD').exchange_to('RUB')
35
+
36
+ # Check last rates update
37
+ bank.rates_updated_at
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,59 @@
1
+ require 'money'
2
+ require 'savon'
3
+
4
+ class Money
5
+ module Bank
6
+ class RussianCentralBank < Money::Bank::VariableExchange
7
+
8
+ CBR_SERVICE_URL = 'http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL'
9
+
10
+ attr_accessor :rates_updated_at
11
+
12
+ def flush_rates
13
+ @mutex.synchronize{
14
+ @rates = {}
15
+ }
16
+ end
17
+
18
+ def update_rates
19
+ @mutex.synchronize{
20
+ update_parsed_rates(exchange_rates)
21
+ @rates_updated_at = Time.now
22
+ @rates
23
+ }
24
+ end
25
+
26
+ def set_rate(from, to, rate)
27
+ @rates[rate_key_for(from, to)] = rate
28
+ end
29
+
30
+ def get_rate from, to
31
+ @rates[rate_key_for(from, to)] || indirect_rate(from, to)
32
+ end
33
+
34
+ private
35
+
36
+ def indirect_rate from, to
37
+ from_base_rate = @rates[rate_key_for('RUB', from)]
38
+ to_base_rate = @rates[rate_key_for('RUB', to)]
39
+ to_base_rate / from_base_rate
40
+ end
41
+
42
+ def exchange_rates
43
+ client = Savon::Client.new(wsdl: CBR_SERVICE_URL)
44
+ response = client.call(:get_curs_on_date, message: {'On_date' => Date.today.to_s})
45
+ response.body[:get_curs_on_date_response][:get_curs_on_date_result][:diffgram][:valute_data][:valute_curs_on_date]
46
+ end
47
+
48
+ def update_parsed_rates rates
49
+ add_rate('RUB', 'RUB', 1)
50
+ rates.each do |rate|
51
+ begin
52
+ add_rate('RUB', rate[:vch_code], 1/ (rate[:vcurs].to_f / rate[:vnom].to_i))
53
+ rescue Money::Currency::UnknownCurrency
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'russian_central_bank'
7
+ spec.version = '0.1.0'
8
+ spec.authors = ['Ramil Mustafin']
9
+ spec.email = ['rommel.rmm@gmail.com']
10
+ spec.description = 'RussianCentralBank extends Money::Bank::VariableExchange and gives you access to the Central Bank of Russia currency exchange rates.'
11
+ spec.summary = 'Access to Central Bank of Russia currency exchange rates.'
12
+ spec.homepage = 'http://github.com/rmustafin/russian_central_bank'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.3'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+
24
+ spec.add_dependency 'money', '~>5.0'
25
+ spec.add_dependency 'savon', '~>2.0'
26
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'RussianCentralBank' do
4
+ before do
5
+ rates_hash = symbolize_keys YAML::load(File.open('spec/support/daily_rates.yml'))
6
+ Savon::Client.any_instance.stub_chain(:call, :body).and_return rates_hash
7
+ end
8
+
9
+ before :each do
10
+ @bank = Money::Bank::RussianCentralBank.new
11
+ end
12
+
13
+ describe '#update_rates' do
14
+ before do
15
+ @bank.update_rates
16
+ end
17
+
18
+ it 'should update rates from daily rates service' do
19
+ @bank.rates['RUB_TO_USD'].should == 0.03083678705348332
20
+ @bank.rates['RUB_TO_EUR'].should == 0.023478587528174305
21
+ @bank.rates['RUB_TO_JPY'].should == 3.086143524190736
22
+ end
23
+ end
24
+
25
+ describe '#flush_rates' do
26
+ before do
27
+ @bank.add_rate('RUB', 'USD', 0.03)
28
+ end
29
+
30
+ it 'should delete all rates' do
31
+ @bank.get_rate('RUB', 'USD')
32
+ @bank.flush_rates
33
+ @bank.rates.should == {}
34
+ end
35
+ end
36
+
37
+ describe '#get_rate' do
38
+ before do
39
+ @bank.flush_rates
40
+ @bank.add_rate('RUB', 'USD', 0.03)
41
+ @bank.add_rate('RUB', 'GBP', 0.02)
42
+ end
43
+
44
+ it 'should get rate from @rates' do
45
+ @bank.get_rate('RUB', 'USD').should == 0.03
46
+ end
47
+
48
+ it 'should calculate indirect rates' do
49
+ @bank.get_rate('USD', 'GBP').should == 0.6666666666666667
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ require 'russian_central_bank'
2
+ require 'savon'
3
+ require 'support/helpers'
4
+
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ config.tty = true
9
+
10
+ config.order = :random
11
+ end
@@ -0,0 +1,8 @@
1
+ get_curs_on_date_response:
2
+ get_curs_on_date_result:
3
+ diffgram:
4
+ valute_data:
5
+ valute_curs_on_date:
6
+ - {vname: "US Dollar", vnom: "1", vcurs: "32.4288", vcode: "840", vch_code: "USD"}
7
+ - {vname: "Euro", vnom: "1", vcurs: "42.5920", vcode: "978", vch_code: "EUR"}
8
+ - {vname: "Japanese yen", vnom: "100", vcurs: "32.4029", vcode: "392", vch_code: "JPY"}
@@ -0,0 +1,15 @@
1
+ def symbolize_keys(hash)
2
+ hash.inject({}){|result, (key, value)|
3
+ new_key = case key
4
+ when String then key.to_sym
5
+ else key
6
+ end
7
+ new_value = case value
8
+ when Hash then symbolize_keys(value)
9
+ when Array then value.map{ |v| v.is_a?(Hash) ? symbolize_keys(v) : v }
10
+ else value
11
+ end
12
+ result[new_key] = new_value
13
+ result
14
+ }
15
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: russian_central_bank
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Ramil Mustafin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ requirement: !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ type: :development
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: money
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '5.0'
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: savon
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2.0'
86
+ requirement: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: '2.0'
92
+ type: :runtime
93
+ prerelease: false
94
+ description: RussianCentralBank extends Money::Bank::VariableExchange and gives you
95
+ access to the Central Bank of Russia currency exchange rates.
96
+ email:
97
+ - rommel.rmm@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/russian_central_bank.rb
108
+ - russian_central_bank.gemspec
109
+ - spec/russian_central_bank_spec.rb
110
+ - spec/spec_helper.rb
111
+ - spec/support/daily_rates.yml
112
+ - spec/support/helpers.rb
113
+ homepage: http://github.com/rmustafin/russian_central_bank
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.25
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Access to Central Bank of Russia currency exchange rates.
138
+ test_files:
139
+ - spec/russian_central_bank_spec.rb
140
+ - spec/spec_helper.rb
141
+ - spec/support/daily_rates.yml
142
+ - spec/support/helpers.rb