exrates 0.1.1
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 +10 -0
- data/.rspec +3 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +40 -0
- data/README.md +58 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/usd_to_cad.rb +7 -0
- data/exrates.gemspec +28 -0
- data/lib/.DS_Store +0 -0
- data/lib/exrates/version.rb +3 -0
- data/lib/exrates.rb +47 -0
- data/rspec/exrates_spec.rb +33 -0
- data/rspec/spec_helper.rb +11 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c1659bbbd2fdd7cf649093aeaeb875ba389436b8e5926cdbde378ebd23f924a0
|
4
|
+
data.tar.gz: d860447e7df0c895a351feb98cc83382ae3c83d4e6ad76ed5e0ff467c1ac5d85
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 160d2d7914a35d091578f70ef4fd06fc72888666e8e48d2046bb1cfb32197bf4cf53fd93157c86beed0003a7170570b6f18ea143deab51896ba11b086920488c
|
7
|
+
data.tar.gz: 30ec35240a144bac726dc597f634d7d4857de9aec6608f193b1f9f89d4e543e24b4d7c50f6fadd65cb4e9ebfedbf63fa3f412c40306ba9abd29a2ae8ebfe3225
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
exrates (0.1.0)
|
5
|
+
money (~> 6)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
concurrent-ruby (1.1.10)
|
11
|
+
diff-lcs (1.5.0)
|
12
|
+
i18n (1.12.0)
|
13
|
+
concurrent-ruby (~> 1.0)
|
14
|
+
money (6.16.0)
|
15
|
+
i18n (>= 0.6.4, <= 2)
|
16
|
+
rake (12.3.3)
|
17
|
+
rspec (3.11.0)
|
18
|
+
rspec-core (~> 3.11.0)
|
19
|
+
rspec-expectations (~> 3.11.0)
|
20
|
+
rspec-mocks (~> 3.11.0)
|
21
|
+
rspec-core (3.11.0)
|
22
|
+
rspec-support (~> 3.11.0)
|
23
|
+
rspec-expectations (3.11.0)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.11.0)
|
26
|
+
rspec-mocks (3.11.1)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.11.0)
|
29
|
+
rspec-support (3.11.0)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
exrates!
|
36
|
+
rake (~> 12.0)
|
37
|
+
rspec (~> 3.0)
|
38
|
+
|
39
|
+
BUNDLED WITH
|
40
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# ExRates
|
2
|
+
|
3
|
+
ExRates is a Ruby gem, that fethes exchange rates from National Bank of Ukraine (bank.gov.ua) and perform currency conversion. With the help of this gem, you can easily find out the exchange rate for a certain date in relation to the hryvnia and convert one currency to another. This gem also uses another gem, called 'Money'. The documentation for this gem you can read [here](https://github.com/RubyMoney/money).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'exrates'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install exrates
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
**Find out the exchange rate in relation to the hryvnia for a certain currency:**
|
24
|
+
|
25
|
+
currency = ExRate.new
|
26
|
+
|
27
|
+
puts currency.rate('USD')
|
28
|
+
|
29
|
+
|
30
|
+
**Find out the exchange rate for a specific date:**
|
31
|
+
|
32
|
+
currency = ExRate.new('22.02.2022')
|
33
|
+
|
34
|
+
puts currency.rate('USD')
|
35
|
+
|
36
|
+
|
37
|
+
**Convertion currency to hryvna:**
|
38
|
+
|
39
|
+
usd = Money.new('1_00', 'USD')
|
40
|
+
|
41
|
+
result = ExRates.new.exchange(usd, 'UAH')
|
42
|
+
|
43
|
+
puts "1.00 USD -> UAH: #{result.format}"
|
44
|
+
|
45
|
+
|
46
|
+
**Convertion one currency to another:**
|
47
|
+
|
48
|
+
usd = Money.new('1_50', 'USD')
|
49
|
+
|
50
|
+
result = ExRates.new.exchange(usd, 'CAD')
|
51
|
+
|
52
|
+
puts "1.50 USD -> CAD: #{result.format}"
|
53
|
+
|
54
|
+
*See the 'Money' gem's [documentation](https://github.com/RubyMoney/money) for how to handle the Money object.*
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mashk4/exrates.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "exrates"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/exrates.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/exrates/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "exrates"
|
5
|
+
spec.version = ExRates::VERSION
|
6
|
+
spec.authors = ["Maria Slashchilina"]
|
7
|
+
spec.email = ["fppl0101@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = 'Fetches currency rates from bank.gov.ua'
|
10
|
+
spec.description = 'This gem allows you to fetch exchange rates from National Bank of Ukraine (bank.gov.ua) and perform currency conversion.'
|
11
|
+
spec.homepage = 'https://github.com/mashk4/exrates'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
|
18
|
+
spec.add_dependency 'money', '~> 6'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
end
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/exrates.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative 'exrates/version'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
require 'money'
|
5
|
+
|
6
|
+
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
|
7
|
+
Money.locale_backend = :currency
|
8
|
+
|
9
|
+
class ExRates
|
10
|
+
attr_reader :refreshed_at
|
11
|
+
|
12
|
+
def initialize(date = Date.today)
|
13
|
+
@refreshed_at = Time.now
|
14
|
+
|
15
|
+
parse!(date)
|
16
|
+
end
|
17
|
+
|
18
|
+
def rate(currency_code)
|
19
|
+
@rates[currency_code.upcase]
|
20
|
+
end
|
21
|
+
|
22
|
+
def exchange(money, currency_to)
|
23
|
+
currency_from = money.currency.iso_code
|
24
|
+
|
25
|
+
money.with_currency(currency_to) * rate(currency_from) / rate(currency_to)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def parse!(date)
|
31
|
+
date = (date.is_a? Date) ? date : Date.parse(date)
|
32
|
+
|
33
|
+
date_string = date.strftime('%d%m%Y')
|
34
|
+
url = "https://bank.gov.ua/NBU_Exchange/exchange?date=#{date_string}&json"
|
35
|
+
response = URI.open(url).read
|
36
|
+
json_doc = JSON.parse(response)
|
37
|
+
|
38
|
+
result = json_doc.map do |node|
|
39
|
+
[ node['CurrencyCodeL'], node['Amount'] ]
|
40
|
+
end
|
41
|
+
|
42
|
+
result.push(['UAH', 1])
|
43
|
+
|
44
|
+
@rates = result.to_h
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative '../lib/exrates'
|
2
|
+
require 'money'
|
3
|
+
|
4
|
+
RSpec.describe ExRates do
|
5
|
+
let (:rates) do
|
6
|
+
date = '21/07/2022'
|
7
|
+
ExRates.new(date)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#rate' do
|
11
|
+
it "returns currency's rate for certain date" do
|
12
|
+
expect(rates.rate('USD')).to eq 36.5686
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#exchange' do
|
17
|
+
it 'converts other currency into hryvnia' do
|
18
|
+
usd = Money.new('1_00', 'USD')
|
19
|
+
|
20
|
+
result = rates.exchange(usd, 'UAH')
|
21
|
+
|
22
|
+
expect(result).to eq Money.new(36_57, 'UAH')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'converts one currency to another currency' do
|
26
|
+
aud = Money.new('1_50', 'AUD')
|
27
|
+
|
28
|
+
result = rates.exchange(aud, 'USD')
|
29
|
+
|
30
|
+
expect(result).to eq Money.new(83, 'USD')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: exrates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maria Slashchilina
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: money
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
description: This gem allows you to fetch exchange rates from National Bank of Ukraine
|
28
|
+
(bank.gov.ua) and perform currency conversion.
|
29
|
+
email:
|
30
|
+
- fppl0101@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- bin/console
|
42
|
+
- bin/setup
|
43
|
+
- examples/usd_to_cad.rb
|
44
|
+
- exrates.gemspec
|
45
|
+
- lib/.DS_Store
|
46
|
+
- lib/exrates.rb
|
47
|
+
- lib/exrates/version.rb
|
48
|
+
- rspec/exrates_spec.rb
|
49
|
+
- rspec/spec_helper.rb
|
50
|
+
homepage: https://github.com/mashk4/exrates
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
homepage_uri: https://github.com/mashk4/exrates
|
55
|
+
source_code_uri: https://github.com/mashk4/exrates
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 2.3.0
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubygems_version: 3.3.18
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: Fetches currency rates from bank.gov.ua
|
75
|
+
test_files: []
|