nbu_currency 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +3 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/nbu_currency.rb +129 -0
- data/lib/nbu_currency/version.rb +5 -0
- data/nbu_currency.gemspec +27 -0
- data/tasks/rspec.rake +3 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eadde143ecc5055f47ac8df94bea1918e19da3c3
|
4
|
+
data.tar.gz: a713228111f3db6c4056a90c0dfe28249ffe3afd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ed4b1be1c9ac7d9c1b2244e7287df7a5b74534373b3c4c29d8bcbcf90f7ec0d0ca5fa888450a279a9fab9471ec7d748553291f19cdbe298a08e23be4ac9f4e0
|
7
|
+
data.tar.gz: 7bcf90befb632a92d0accbdedc3bf6450f0d21f2b48ffd5439d4b00bf199532f1502e9dfb1e083b942f6548be9d16a8dfb36e4050b5b5489e352d728b7f4db07
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Nazar Matus
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# nbu_currency
|
2
|
+
|
3
|
+
## Introduction
|
4
|
+
|
5
|
+
This gem downloads the exchange rates from the National Bank Of Ukraine. You can calculate exchange rates with it. It is compatible with the money gem.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'nbu_currency'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install nbu_currency
|
22
|
+
|
23
|
+
## Dependencies
|
24
|
+
|
25
|
+
- nokogiri
|
26
|
+
- money
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
With the gem, you do not need to manually add exchange rates. Calling update_rates will download the rates from the National Bank Of Ukraine. The API is the same as the money gem. Feel free to use Money objects with the bank.
|
31
|
+
|
32
|
+
For performance reasons, you may prefer to read from a file instead. Furthermore, NBU publishes their rates daily. It makes sense to save the rates in a file to read from. It also adds an __update_at__ field so that you can manage the update.
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
# cached location
|
36
|
+
cache = "/some/file/location/exchange_rates.xml"
|
37
|
+
|
38
|
+
# saves the rates in a specified location
|
39
|
+
nbu_bank.save_rates(cache)
|
40
|
+
|
41
|
+
# reads the rates from the specified location
|
42
|
+
nbu_bank.update_rates(cache)
|
43
|
+
|
44
|
+
if !nbu_bank.rates_updated_at || nbu_bank.rates_updated_at < Time.now - 1.days
|
45
|
+
nbu_bank.save_rates(cache)
|
46
|
+
nbu_bank.update_rates(cache)
|
47
|
+
end
|
48
|
+
|
49
|
+
# exchange 100 CAD to USD as usual
|
50
|
+
nbu_bank.exchange_with(Money.new(100, "CAD"), "USD") # Money.new(80, "USD")
|
51
|
+
```
|
52
|
+
|
53
|
+
## Development
|
54
|
+
|
55
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
56
|
+
|
57
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it ( https://github.com/FunkyloverOne/nbu_currency/fork )
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nbu_currency"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/nbu_currency.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'nbu_currency/version'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'money'
|
5
|
+
|
6
|
+
class InvalidCache < StandardError ; end
|
7
|
+
|
8
|
+
class NbuCurrency < Money::Bank::VariableExchange
|
9
|
+
|
10
|
+
attr_accessor :last_updated
|
11
|
+
attr_accessor :rates_updated_at
|
12
|
+
|
13
|
+
CURRENCIES = %w(USD CAD EUR GBP UAH).map(&:freeze).freeze
|
14
|
+
NBU_RATES_URL = 'https://privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML&apicour&country=ua&full'.freeze
|
15
|
+
|
16
|
+
def update_rates(cache=nil)
|
17
|
+
update_parsed_rates(doc(cache))
|
18
|
+
end
|
19
|
+
|
20
|
+
def save_rates(cache, url=NBU_RATES_URL)
|
21
|
+
raise InvalidCache unless cache
|
22
|
+
File.open(cache, "w") do |file|
|
23
|
+
io = open(url);
|
24
|
+
io.each_line { |line| file.puts line }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_rates_from_s(content)
|
29
|
+
update_parsed_rates(doc_from_s(content))
|
30
|
+
end
|
31
|
+
|
32
|
+
def save_rates_to_s(url=NBU_RATES_URL)
|
33
|
+
open(url).read
|
34
|
+
end
|
35
|
+
|
36
|
+
def exchange(cents, from_currency, to_currency)
|
37
|
+
exchange_with(Money.new(cents, from_currency), to_currency)
|
38
|
+
end
|
39
|
+
|
40
|
+
def exchange_with(from, to_currency)
|
41
|
+
from_base_rate, to_base_rate = nil, nil
|
42
|
+
rate = get_rate(from, to_currency)
|
43
|
+
|
44
|
+
unless rate
|
45
|
+
@mutex.synchronize do
|
46
|
+
opts = { without_mutex: true }
|
47
|
+
from_base_rate = get_rate("UAH", from.currency.to_s, opts)
|
48
|
+
to_base_rate = get_rate("UAH", to_currency, opts)
|
49
|
+
end
|
50
|
+
rate = to_base_rate / from_base_rate
|
51
|
+
end
|
52
|
+
|
53
|
+
calculate_exchange(from, to_currency, rate)
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_rate(from, to, opts = {})
|
57
|
+
fn = -> { @rates[rate_key_for(from, to, opts)] }
|
58
|
+
|
59
|
+
if opts[:without_mutex]
|
60
|
+
fn.call
|
61
|
+
else
|
62
|
+
@mutex.synchronize { fn.call }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def set_rate(from, to, rate, opts = {})
|
67
|
+
fn = -> { @rates[rate_key_for(from, to, opts)] = rate }
|
68
|
+
|
69
|
+
if opts[:without_mutex]
|
70
|
+
fn.call
|
71
|
+
else
|
72
|
+
@mutex.synchronize { fn.call }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
|
78
|
+
def doc(cache, url=NBU_RATES_URL)
|
79
|
+
rates_source = !!cache ? cache : url
|
80
|
+
begin
|
81
|
+
Nokogiri::XML(open(rates_source)).tap do |doc|
|
82
|
+
if doc.xpath('exchangerate/exchangerate/@date').any?
|
83
|
+
doc.xpath('exchangerate/exchangerate')
|
84
|
+
else
|
85
|
+
raise Nokogiri::XML::XPath::SyntaxError
|
86
|
+
end
|
87
|
+
end
|
88
|
+
rescue Nokogiri::XML::XPath::SyntaxError
|
89
|
+
Nokogiri::XML(open(url))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def doc_from_s(content)
|
94
|
+
Nokogiri::XML(content)
|
95
|
+
end
|
96
|
+
|
97
|
+
def update_parsed_rates(doc)
|
98
|
+
rates = doc.xpath('exchangerate/exchangerate')
|
99
|
+
|
100
|
+
@mutex.synchronize do
|
101
|
+
rates.each do |exchange_rate|
|
102
|
+
rate = BigDecimal(exchange_rate.attribute("buy").value.to_i) / exchange_rate.attribute("unit").value.to_f / 10000.0
|
103
|
+
currency = exchange_rate.attribute("ccy").value
|
104
|
+
set_rate("UAH", currency, rate, :without_mutex => true)
|
105
|
+
end
|
106
|
+
set_rate("UAH", "UAH", 1, :without_mutex => true)
|
107
|
+
end
|
108
|
+
|
109
|
+
rates_updated_at = doc.xpath('exchangerate/exchangerate/@date').first.value
|
110
|
+
@rates_updated_at = Time.parse(rates_updated_at)
|
111
|
+
|
112
|
+
@last_updated = Time.now
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def calculate_exchange(from, to_currency, rate)
|
118
|
+
to_currency_money = Money::Currency.wrap(to_currency).subunit_to_unit
|
119
|
+
from_currency_money = from.currency.subunit_to_unit
|
120
|
+
decimal_money = BigDecimal(to_currency_money) / BigDecimal(from_currency_money)
|
121
|
+
money = (decimal_money * from.cents * rate).round
|
122
|
+
Money.new(money, to_currency)
|
123
|
+
end
|
124
|
+
|
125
|
+
def rate_key_for(from, to, opts)
|
126
|
+
key = "#{from}_TO_#{to}"
|
127
|
+
key.upcase
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nbu_currency/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nbu_currency"
|
8
|
+
spec.version = NbuCurrency::VERSION
|
9
|
+
spec.authors = ["Nazar Matus"]
|
10
|
+
spec.email = ["funkyloverone@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Calculates exchange rates based on rates from National Bank of Ukraine. Money gem compatible.}
|
13
|
+
spec.description = %q{This gem reads exchange rates from the National Bank of Ukraine website. It uses it to calculates exchange rates. It is compatible with the money gem.}
|
14
|
+
spec.homepage = "https://github.com/FunkyloverOne/nbu_currency"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
if spec.respond_to?(:metadata)
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.add_dependency "nokogiri", "~> 1.6.3"
|
26
|
+
spec.add_dependency "money", "~> 6.5.0"
|
27
|
+
end
|
data/tasks/rspec.rake
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nbu_currency
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nazar Matus
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.6.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.6.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: money
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 6.5.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 6.5.0
|
41
|
+
description: This gem reads exchange rates from the National Bank of Ukraine website.
|
42
|
+
It uses it to calculates exchange rates. It is compatible with the money gem.
|
43
|
+
email:
|
44
|
+
- funkyloverone@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/nbu_currency.rb
|
59
|
+
- lib/nbu_currency/version.rb
|
60
|
+
- nbu_currency.gemspec
|
61
|
+
- tasks/rspec.rake
|
62
|
+
homepage: https://github.com/FunkyloverOne/nbu_currency
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Calculates exchange rates based on rates from National Bank of Ukraine. Money
|
86
|
+
gem compatible.
|
87
|
+
test_files: []
|