nbu_rates 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cf367061c3263a11db1306b628f4d554b63f4d820daa4c3a3663eda066c36a01
4
+ data.tar.gz: fe40812cbf14b3553711ede67d2f2716dc6ce02dd53d150f96b28b50a8787ae2
5
+ SHA512:
6
+ metadata.gz: a39ebd2836ee851b35cd27ba6dbad18316ce192eba9bb5dda320f94546bd5367efadeec4caa371eeb2dbfa2db8a287f11de17934bfcbf607643f0ac3e42b2738
7
+ data.tar.gz: 4cab11ea60233b22b27af89dbd15ec5e7e9091a0b17828ca0293da67996b5de0ffdb7d11185507797533ab0ee455db1b575bef0cbdfe61071d8b44f5ddd36391
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in nbu_rates.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nbu_rates (0.1.0)
5
+ money (~> 6)
6
+ nokogiri (~> 1.9)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ concurrent-ruby (1.1.9)
13
+ diff-lcs (1.5.0)
14
+ i18n (1.9.1)
15
+ concurrent-ruby (~> 1.0)
16
+ money (6.16.0)
17
+ i18n (>= 0.6.4, <= 2)
18
+ nokogiri (1.13.1-x86_64-linux)
19
+ racc (~> 1.4)
20
+ parallel (1.21.0)
21
+ parser (3.1.0.0)
22
+ ast (~> 2.4.1)
23
+ racc (1.6.0)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.2.0)
27
+ rexml (3.2.5)
28
+ rspec (3.10.0)
29
+ rspec-core (~> 3.10.0)
30
+ rspec-expectations (~> 3.10.0)
31
+ rspec-mocks (~> 3.10.0)
32
+ rspec-core (3.10.2)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-expectations (3.10.2)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-mocks (3.10.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-support (3.10.3)
41
+ rubocop (1.25.0)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.1.0.0)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml
47
+ rubocop-ast (>= 1.15.1, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 3.0)
50
+ rubocop-ast (1.15.1)
51
+ parser (>= 3.0.1.1)
52
+ ruby-progressbar (1.11.0)
53
+ unicode-display_width (2.1.0)
54
+
55
+ PLATFORMS
56
+ x86_64-linux
57
+
58
+ DEPENDENCIES
59
+ nbu_rates!
60
+ rake (~> 13.0)
61
+ rspec (~> 3.0)
62
+ rubocop (~> 1.21)
63
+
64
+ BUNDLED WITH
65
+ 2.3.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ivan Dmytrenko
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,44 @@
1
+ # NBU Rates
2
+
3
+ Gem for fetching currency from National bank of Ukraine
4
+
5
+ Gem done by [this tutorial](https://www.youtube.com/watch?v=rTRzXBWGmvI) from [GoodProgrammer](https://www.youtube.com/channel/UCDPdTky4sQtQEwOLAe5v-NA)
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```rb
13
+ gem 'nbu_rates'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install nbu_rates
23
+
24
+ ## Usage
25
+
26
+ ``` rb
27
+ require 'cbr_rates'
28
+
29
+ usd = Money.new('1_50', 'USD')
30
+
31
+ result = CbrRates.new.exchange(usd, 'CAD')
32
+
33
+ puts "1.50 USD ~> CAD: #{result.format}"
34
+ ```
35
+
36
+ How to work with object `Money`: [documentation](https://github.com/RubyMoney/money)
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Dmytrenko-I/nbu_rates.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "nbu_rates"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require_relative '../lib/nbu_rates'
2
+
3
+ usd = Money.new('1_50', 'USD')
4
+
5
+ result = NbuRates.new.exchange(usd, 'CAD')
6
+
7
+ puts "1.5 USD ~> CAD: #{result.format}"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class NbuRates
4
+ VERSION = "0.1.1"
5
+ end
data/lib/nbu_rates.rb ADDED
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'nbu_rates/version'
4
+
5
+ require 'open-uri'
6
+ require 'nokogiri'
7
+ require 'money'
8
+
9
+ Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
10
+ Money.locale_backend = :currency
11
+
12
+ class NbuRates
13
+ attr_reader :refreshed_at
14
+
15
+ def initialize(date = Date.today)
16
+ @refreshed_at = Time.now
17
+
18
+ parse!(date)
19
+ end
20
+
21
+ def rate(currency_code)
22
+ @rates[currency_code.upcase]
23
+ end
24
+
25
+ def exchange(money, currency_to)
26
+ currency_from = money.currency.iso_code
27
+
28
+ money.with_currency(currency_to) * rate(currency_from) / rate(currency_to)
29
+ end
30
+
31
+ private
32
+
33
+ def parse!(date)
34
+ date = date.strftime('%d.%m.%Y')
35
+ url = "https://bank.gov.ua/NBU_Exchange/exchange?date=#{date}"
36
+
37
+ response = URI.open(url).read
38
+ xml_doc = Nokogiri::XML(response)
39
+
40
+ result =
41
+ xml_doc.css('ROW').map do |node|
42
+ # amount -- price in UAH per X units
43
+ # units -- quantity of valute
44
+ amount = BigDecimal(node.css('Amount').text, 10)
45
+ units = node.css('Units').text.to_i
46
+ [
47
+ node.css('CurrencyCodeL').text,
48
+ amount / units
49
+ ]
50
+ end
51
+
52
+ result.push(['UAH', 1])
53
+
54
+ @rates = result.to_h
55
+ end
56
+ end
data/sig/nbu_rates.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module NbuRates
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nbu_rates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Dmytrenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-30 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
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ description: Fetches currency rates from https://bank.gov.ua/
42
+ email:
43
+ - dmytrenko.i.t@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - examples/usd_to_cad.rb
58
+ - lib/nbu_rates.rb
59
+ - lib/nbu_rates/version.rb
60
+ - sig/nbu_rates.rbs
61
+ homepage: https://github.com/Dmytrenko-I/nbu_rates
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ homepage_uri: https://github.com/Dmytrenko-I/nbu_rates
66
+ source_code_uri: https://github.com/Dmytrenko-I/nbu_rates
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.6.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.2.22
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Fetches currency rates from https://bank.gov.ua/
86
+ test_files: []