exchange_rates_nbp 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 660739a3cfd2ed77321e72db05a94c9d3f3136c2
4
+ data.tar.gz: 2c81afbd697cc12b0594e199385efd58465bc4ae
5
+ SHA512:
6
+ metadata.gz: dc236b567256cf7e6d7a240e19e6ab25688a24cdf385665c6f837b442f127cecca7ece7eb348bbe53a7331e7b3b56b5cac8b9dd47daf8d0bbe598a428c00902a
7
+ data.tar.gz: 72156e389610fdd165f48ffc3336ba3366ec752b68a0fd1cde3b4d9acdf140b9cdb5f8596d28db18d84fc844b2ba1c7bb6285e1bcffce4e442c42364ff9718f1
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Lint/UnusedBlockArgument:
5
+ Enabled: false
6
+
7
+ require: rubocop-rspec
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install:
6
+ - gem install bundler -v 1.11.2
7
+ notifications:
8
+ email:
9
+ on_success: never
10
+ script:
11
+ - bundle exec rspec spec
12
+ - bundle exec rubocop --display-cop-names
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in exchange_rates_nbp.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Maciej Majewski
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,51 @@
1
+ # ExchangeRatesNBP
2
+
3
+ [![Build Status](https://travis-ci.org/maciejmajewski/exchange_rates_nbp.svg?branch=master)](https://travis-ci.org/maciejmajewski/exchange_rates_nbp)
4
+
5
+ Library for accessing exchange rates from nbp.pl.
6
+
7
+ It can be used via API or via command line utility.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'exchange_rates_nbp'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install exchange_rates_nbp
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ ExchangeRatesNBP.exchange_rate(Date.new(2016, 1, 1), 'EUR')
29
+ # => 4.2615
30
+ ```
31
+
32
+ ### Via command line
33
+
34
+ ```
35
+ exchange_rates_nbp --date DATE --currency_code CURRENCY_CODE --table a
36
+ # => 4.2615
37
+ ```
38
+
39
+ ## Development
40
+
41
+ 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. Run `bundle exec exchange_rates_nbp` to use the gem in this directory, ignoring other installed copies of this gem.
42
+
43
+ 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/maciejmajewski/exchange_rates_nbp.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://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,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'exchange_rates_nbp'
5
+
6
+ require 'pry'
7
+ Pry.start
@@ -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,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'exchange_rates_nbp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'exchange_rates_nbp'
8
+ spec.version = ExchangeRatesNBP::VERSION
9
+ spec.authors = ['Maciej Majewski']
10
+ spec.email = ['maciej.majewski@gooddesign.pl']
11
+
12
+ spec.summary = 'Collects exchange rates from nbp.pl'
13
+ spec.description = 'Exposes simple API and command line tool for fetching '\
14
+ 'exchange rates from nbp.pl'
15
+ spec.homepage = 'http://github.com/maciejmajewski/exchange_rates_nbp'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_runtime_dependency 'http'
26
+ spec.add_runtime_dependency 'oga'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.11'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop'
32
+ spec.add_development_dependency 'rubocop-rspec'
33
+ spec.add_development_dependency 'pry'
34
+ spec.add_development_dependency 'timecop'
35
+ spec.add_development_dependency 'vcr'
36
+ spec.add_development_dependency 'webmock'
37
+ spec.add_development_dependency 'byebug'
38
+ end
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'exchange_rates_nbp'
4
+
5
+ require 'optparse'
6
+
7
+ options = {}
8
+
9
+ parser = OptionParser.new do |opts|
10
+ opts.banner = 'Usage: exchange_rates_nbp [options]'
11
+
12
+ opts.on('--currency-code=CURRENCY_CODE',
13
+ 'Currency code for query') do |currency_code|
14
+ options[:currency_code] = currency_code
15
+ end
16
+
17
+ opts.on('--date=DATE', 'Date for query') do |date|
18
+ options[:date] = Date.parse(date)
19
+ end
20
+
21
+ opts.on('--latest', 'Print latest exchange rate') do
22
+ options[:date] = Date.today
23
+ end
24
+ end
25
+
26
+ parser.parse!
27
+
28
+ if options[:currency_code].nil? || options[:date].nil?
29
+ puts parser.help
30
+ else
31
+ puts ExchangeRatesNBP.exchange_rate(options[:date], options[:currency_code])
32
+ end
@@ -0,0 +1,37 @@
1
+ require 'exchange_rates_nbp/version'
2
+
3
+ require 'http'
4
+
5
+ require 'exchange_rates_nbp/clients/table_list'
6
+ require 'exchange_rates_nbp/clients/currency_data_set'
7
+
8
+ module ExchangeRatesNBP
9
+ BASE_URL = 'http://www.nbp.pl/kursy/xml/'.freeze
10
+ FILE_NAME_YEAR_PATTERN = 'dir{year}.txt'.freeze
11
+ FILE_NAME_CURRENT_YEAR = 'dir.txt'.freeze
12
+
13
+ # 'xnnnzrrmmdd.xml'
14
+
15
+ TABLE_TYPES = {
16
+ middle_exchange_rates_table_a: 'a',
17
+ middle_exchange_rates_table_b: 'b',
18
+ buy_and_sell_table: 'c',
19
+ settlement_units_table: 'h'
20
+ }.freeze
21
+
22
+ DEFAULT_TABLE_TYPES = TABLE_TYPES.values.first
23
+
24
+ def self.exchange_rate(date, currency_code, table = DEFAULT_TABLE_TYPES)
25
+ table_id = find_table_id(date, table)
26
+ extract_currency(table_id, currency_code)
27
+ end
28
+
29
+ def self.find_table_id(date, table)
30
+ Clients::TableList.new(date.year, table).fetch_closest_to(date)
31
+ end
32
+
33
+ def self.extract_currency(table_id, currency_code)
34
+ ExchangeRatesNBP::Clients::CurrencyDataSet.new(table_id)
35
+ .exchange_rate(currency_code)
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ require 'oga'
2
+
3
+ module ExchangeRatesNBP
4
+ module Clients
5
+ class CurrencyDataSet
6
+ SELECTOR_CURRENCY_ELEMENT = 'tabela_kursow pozycja'.freeze
7
+ FIELD_AVERAGE_CURRENCY_CODE = 'kurs_sredni'.freeze
8
+ FIELD_CURRENCY_CODE = 'kod_waluty'.freeze
9
+
10
+ def initialize(table_id)
11
+ @table_id = table_id
12
+ end
13
+
14
+ def exchange_rate(currency_code)
15
+ doc = Oga.parse_xml(data).css(SELECTOR_CURRENCY_ELEMENT).detect do |p|
16
+ p.css(FIELD_CURRENCY_CODE).text == currency_code
17
+ end
18
+
19
+ raise "Invalid currency code: #{currency_code}" if doc.nil?
20
+
21
+ doc.css(FIELD_AVERAGE_CURRENCY_CODE).text.tr(',', '.').to_f
22
+ end
23
+
24
+ private
25
+
26
+ def data
27
+ HTTP.get(BASE_URL + "#{@table_id}.xml").to_s
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,59 @@
1
+ module ExchangeRatesNBP
2
+ module Clients
3
+ class TableList
4
+ PATTERN = /[abch][0-9]{3}z[0-9]{6}/
5
+
6
+ def initialize(year, table_type)
7
+ @year = year
8
+ @table_type = table_type
9
+
10
+ validate_year
11
+ end
12
+
13
+ def fetch_closest_to(date)
14
+ loop do
15
+ table_id = table_id_for(date)
16
+ return table_id unless table_id.nil?
17
+ date -= 1
18
+ return fetch_data_for_previous_year if date.year != @year
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def validate_year
25
+ raise 'Data for nbp.pl is available from 2002 onwards' if @year < 2002
26
+ raise "Can't fetch data for the future" if @year > Date.today.year
27
+ end
28
+
29
+ def fetch_data_for_previous_year
30
+ last_day = Date.new(@year - 1, 12, 31)
31
+ self.class.new(@year - 1, @table_type).fetch_closest_to(last_day)
32
+ end
33
+
34
+ def table_id_for(date)
35
+ date_string = date.strftime('%y%m%d')
36
+ selected_table_ids.detect { |l| l =~ /#{date_string}$/ }
37
+ end
38
+
39
+ def selected_table_ids
40
+ @selected_table_ids ||= all_table_ids.select do |table_id|
41
+ table_id =~ /^#{@table_type}/
42
+ end
43
+ end
44
+
45
+ def all_table_ids
46
+ body = HTTP.get(BASE_URL + table_list_url, encoding: 'utf-8').to_s
47
+ body.scan(PATTERN)
48
+ end
49
+
50
+ def table_list_url
51
+ if @year == Date.today.year
52
+ FILE_NAME_CURRENT_YEAR
53
+ else
54
+ FILE_NAME_YEAR_PATTERN.sub('{year}', @year.to_s)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module ExchangeRatesNBP
2
+ VERSION = '0.1.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,230 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exchange_rates_nbp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Maciej Majewski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oga
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: timecop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: vcr
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: byebug
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Exposes simple API and command line tool for fetching exchange rates
182
+ from nbp.pl
183
+ email:
184
+ - maciej.majewski@gooddesign.pl
185
+ executables:
186
+ - exchange_rates_nbp
187
+ extensions: []
188
+ extra_rdoc_files: []
189
+ files:
190
+ - ".gitignore"
191
+ - ".rspec"
192
+ - ".rubocop.yml"
193
+ - ".travis.yml"
194
+ - Gemfile
195
+ - LICENSE.txt
196
+ - README.md
197
+ - Rakefile
198
+ - bin/console
199
+ - bin/setup
200
+ - exchange_rates_nbp.gemspec
201
+ - exe/exchange_rates_nbp
202
+ - lib/exchange_rates_nbp.rb
203
+ - lib/exchange_rates_nbp/clients/currency_data_set.rb
204
+ - lib/exchange_rates_nbp/clients/table_list.rb
205
+ - lib/exchange_rates_nbp/version.rb
206
+ homepage: http://github.com/maciejmajewski/exchange_rates_nbp
207
+ licenses:
208
+ - MIT
209
+ metadata: {}
210
+ post_install_message:
211
+ rdoc_options: []
212
+ require_paths:
213
+ - lib
214
+ required_ruby_version: !ruby/object:Gem::Requirement
215
+ requirements:
216
+ - - ">="
217
+ - !ruby/object:Gem::Version
218
+ version: '0'
219
+ required_rubygems_version: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: '0'
224
+ requirements: []
225
+ rubyforge_project:
226
+ rubygems_version: 2.5.1
227
+ signing_key:
228
+ specification_version: 4
229
+ summary: Collects exchange rates from nbp.pl
230
+ test_files: []