cbr_rates 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a110ec71dd85ae6e834167e6882e73746aa7f9d659c16cebc2aacd333bf8cb3f
4
+ data.tar.gz: 0bdeb40b9d31ddb0d2ce5525d45fe8403ba5c36042bbba3b67e6ba9509d7468f
5
+ SHA512:
6
+ metadata.gz: 3ed29be595000346af0c902aa6709a2ac1412da822fbcd6f55ff4615fc07bc087d1a4e5e6285449fec01878400bea6d33492a77f7584f888a4fb36151dc207dc
7
+ data.tar.gz: a3c00ea2b8f0ce47d88a25c0a5fe5acc3874b25fdc5bd619fde81df42f66a1b29205d33b8ef368b891b7cff9093d77c0f7cd1397a57b95dbabc81baa5bbd98b0
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cbr_rates.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cbr_rates (0.1.0)
5
+ money (~> 6)
6
+ nokogiri (~> 1.9)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ concurrent-ruby (1.1.7)
12
+ diff-lcs (1.4.4)
13
+ i18n (1.8.5)
14
+ concurrent-ruby (~> 1.0)
15
+ mini_portile2 (2.4.0)
16
+ money (6.13.8)
17
+ i18n (>= 0.6.4, <= 2)
18
+ nokogiri (1.10.10)
19
+ mini_portile2 (~> 2.4.0)
20
+ nokogiri (1.10.10-x64-mingw32)
21
+ mini_portile2 (~> 2.4.0)
22
+ rake (12.3.3)
23
+ rspec (3.10.0)
24
+ rspec-core (~> 3.10.0)
25
+ rspec-expectations (~> 3.10.0)
26
+ rspec-mocks (~> 3.10.0)
27
+ rspec-core (3.10.0)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-expectations (3.10.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-mocks (3.10.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.10.0)
35
+ rspec-support (3.10.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+ x64-mingw32
40
+
41
+ DEPENDENCIES
42
+ cbr_rates!
43
+ rake (~> 12.0)
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 TODO: Write your name
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.
@@ -0,0 +1,40 @@
1
+ # CbrRates
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cbr_rates`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cbr_rates'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cbr_rates
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cbr_rates.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cbr_rates"
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__)
@@ -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,28 @@
1
+ require_relative 'lib/cbr_rates/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "cbr_rates"
5
+ spec.version = CbrRates::VERSION
6
+ spec.authors = ['Eugene Zolotarev', 'Vadim Venediktov']
7
+ spec.email = 'support@goodprogrammer.ru'
8
+
9
+ spec.summary = 'Fetches currency rates from cbr.ru'
10
+ spec.description = %q{Fetches currency rates from cbr.ru}
11
+ spec.homepage = "https://github.com/goodprogrammer-ru/cbr_rates"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = spec.homepage
16
+
17
+ spec.add_dependency 'money', '~> 6'
18
+ spec.add_dependency 'nokogiri', '~> 1.9'
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
@@ -0,0 +1,9 @@
1
+ require_relative '../lib/cbr_rates'
2
+
3
+ Money.locale_backend = :currency
4
+
5
+ usd = Money.new('1_50', 'USD')
6
+
7
+ result = CbrRates.new.exchange(usd, 'CAD')
8
+
9
+ puts "1.50 USD -> CAD: #{result.format}"
@@ -0,0 +1,68 @@
1
+ require_relative 'cbr_rates/version'
2
+
3
+ require 'open-uri'
4
+ require 'money'
5
+ require 'nokogiri'
6
+
7
+ Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
8
+
9
+ class CbrRates
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_string = date.strftime('%d/%m/%Y')
32
+ url =
33
+ "http://www.cbr.ru/scripts/XML_daily.asp?date_req=#{date_string}"
34
+
35
+ response = URI.open(url).read
36
+ xml_doc = Nokogiri::XML(response)
37
+
38
+ result =
39
+ xml_doc.css('Valute').map do |node|
40
+ value = BigDecimal(node.css('Value').text.gsub(',', '.'), 10)
41
+ amount = node.css('Nominal').text.to_i
42
+
43
+ [
44
+ node.css('CharCode').text,
45
+ value / amount
46
+ ]
47
+ end
48
+
49
+ result.push(["RUB", 1])
50
+
51
+ @rates = result.to_h
52
+ end
53
+ end
54
+
55
+ # cbr = CBRates.new
56
+ # Скачать курсы валют за сегодня
57
+
58
+ # cbr = CBRates.new(5.days.ago)
59
+ # Скачать курсы за конкретную дату
60
+
61
+ # cbr.refreshed_at
62
+ # => 2020-11-25 17:13:28
63
+
64
+ # cbr.rate("USD", "RUB")
65
+ # => 75.4727
66
+
67
+ # cbr.exchange(Money.new(100_00, "RUB"), "USD")
68
+ # => #<Money#... 1.25 USD>
@@ -0,0 +1,3 @@
1
+ class CbrRates
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbr_rates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eugene Zolotarev
8
+ - Vadim Venediktov
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-11-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: money
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '6'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: nokogiri
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.9'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.9'
42
+ description: Fetches currency rates from cbr.ru
43
+ email: support@goodprogrammer.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - cbr_rates.gemspec
59
+ - examples/usd_to_cad.rb
60
+ - lib/cbr_rates.rb
61
+ - lib/cbr_rates/version.rb
62
+ homepage: https://github.com/goodprogrammer-ru/cbr_rates
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ homepage_uri: https://github.com/goodprogrammer-ru/cbr_rates
67
+ source_code_uri: https://github.com/goodprogrammer-ru/cbr_rates
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.3.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.1.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Fetches currency rates from cbr.ru
87
+ test_files: []