exchange_rates_nbp 0.1.1 → 0.1.2
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 +4 -4
- data/.rubocop.yml +0 -3
- data/CHANGELOG.md +11 -0
- data/README.md +17 -1
- data/exchange_rates_nbp.gemspec +1 -0
- data/exe/exchange_rates_nbp +16 -1
- data/lib/exchange_rates_nbp.rb +16 -7
- data/lib/exchange_rates_nbp/clients/currency_data_set.rb +37 -5
- data/lib/exchange_rates_nbp/version.rb +1 -1
- metadata +18 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 41009cf622b4fb1042aa0db0dae7a2dd9b963c65
|
|
4
|
+
data.tar.gz: 613dbe94db7fb1dfe6931a2a190bd0f5a0b6052a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2527f8c435f7c7a8904b9daa8190ea243ba462d63a4ab3153dd22341752ce37f6e7307eaf9487d269ec28602d914a53190a2093b976fdfa2eaa1446698d1d356
|
|
7
|
+
data.tar.gz: a215f1692f1f18ce3f4120befc50ccfd345c48ab64ad08e41d8b03d8e1f86dcc59a6555e4045303341771063d6d3c7e5ef58e524184ff2cdb6f68fa4ec6f7360
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# ExchangeRatesNBP
|
|
2
2
|
|
|
3
3
|
[](https://travis-ci.org/maciejmajewski/exchange_rates_nbp)
|
|
4
|
+
[](https://badge.fury.io/rb/exchange_rates_nbp)
|
|
4
5
|
|
|
5
6
|
Library for accessing exchange rates from nbp.pl.
|
|
6
7
|
|
|
@@ -31,11 +32,26 @@ ExchangeRatesNBP.exchange_rate(Date.new(2016, 1, 1), 'EUR')
|
|
|
31
32
|
|
|
32
33
|
### Via command line
|
|
33
34
|
|
|
34
|
-
```
|
|
35
|
+
```sh
|
|
35
36
|
exchange_rates_nbp --date DATE --currency_code CURRENCY_CODE --table a
|
|
36
37
|
# => 4.2615
|
|
37
38
|
```
|
|
38
39
|
|
|
40
|
+
#### Getting latest exchange rate
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
exchange_rates_nbp --latest --currency-code=EUR
|
|
44
|
+
# => 4.312
|
|
45
|
+
```
|
|
46
|
+
#### Verbose mode
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
exchange_rates_nbp --latest --currency-code=EUR --verbose
|
|
50
|
+
# => Publish date: 2016-09-30
|
|
51
|
+
# => Conversion factor: 1
|
|
52
|
+
# => Exchange rate: 4.312
|
|
53
|
+
```
|
|
54
|
+
|
|
39
55
|
## Development
|
|
40
56
|
|
|
41
57
|
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.
|
data/exchange_rates_nbp.gemspec
CHANGED
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
|
|
|
24
24
|
|
|
25
25
|
spec.add_runtime_dependency 'http'
|
|
26
26
|
spec.add_runtime_dependency 'oga'
|
|
27
|
+
spec.add_runtime_dependency 'colorize'
|
|
27
28
|
|
|
28
29
|
spec.add_development_dependency 'bundler', '~> 1.11'
|
|
29
30
|
spec.add_development_dependency 'rake', '~> 10.0'
|
data/exe/exchange_rates_nbp
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'exchange_rates_nbp'
|
|
4
4
|
|
|
5
5
|
require 'optparse'
|
|
6
|
+
require 'colorize'
|
|
6
7
|
|
|
7
8
|
options = {}
|
|
8
9
|
|
|
@@ -21,12 +22,26 @@ parser = OptionParser.new do |opts|
|
|
|
21
22
|
opts.on('--latest', 'Print latest exchange rate') do
|
|
22
23
|
options[:date] = Date.today
|
|
23
24
|
end
|
|
25
|
+
|
|
26
|
+
opts.on('-v', '--verbose', 'Show verbose info') do
|
|
27
|
+
options[:verbose] = true
|
|
28
|
+
end
|
|
24
29
|
end
|
|
25
30
|
|
|
26
31
|
parser.parse!
|
|
27
32
|
|
|
28
33
|
if options[:currency_code].nil? || options[:date].nil?
|
|
34
|
+
puts '`currency_code` and `date` params are required'.red
|
|
29
35
|
puts parser.help
|
|
30
36
|
else
|
|
31
|
-
|
|
37
|
+
info = ExchangeRatesNBP.exchange_rate_info(options[:date],
|
|
38
|
+
options[:currency_code])
|
|
39
|
+
|
|
40
|
+
if options[:verbose]
|
|
41
|
+
puts "Publish date: #{info[:publish_date]}"
|
|
42
|
+
puts "Conversion factor: #{info[:conversion_factor]}"
|
|
43
|
+
puts "Exchange rate: #{info[:exchange_rate]}"
|
|
44
|
+
else
|
|
45
|
+
puts info[:exchange_rate]
|
|
46
|
+
end
|
|
32
47
|
end
|
data/lib/exchange_rates_nbp.rb
CHANGED
|
@@ -22,16 +22,25 @@ module ExchangeRatesNBP
|
|
|
22
22
|
DEFAULT_TABLE_TYPES = TABLE_TYPES.values.first
|
|
23
23
|
|
|
24
24
|
def self.exchange_rate(date, currency_code, table = DEFAULT_TABLE_TYPES)
|
|
25
|
-
|
|
26
|
-
extract_currency(table_id, currency_code)
|
|
25
|
+
exchange_rate_info(date, currency_code, table)[:exchange_rate]
|
|
27
26
|
end
|
|
28
27
|
|
|
29
|
-
def self.
|
|
30
|
-
|
|
28
|
+
def self.exchange_rate_info(date, currency_code, table = DEFAULT_TABLE_TYPES)
|
|
29
|
+
table_id = find_table_id(date, table)
|
|
30
|
+
exchange_rate_hash(table_id, currency_code)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
class << self
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def find_table_id(date, table)
|
|
37
|
+
Clients::TableList.new(date.year, table).fetch_closest_to(date)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def exchange_rate_hash(table_id, currency_code)
|
|
41
|
+
ExchangeRatesNBP::Clients::CurrencyDataSet
|
|
42
|
+
.new(table_id)
|
|
43
|
+
.exchange_rate(currency_code)
|
|
44
|
+
end
|
|
36
45
|
end
|
|
37
46
|
end
|
|
@@ -4,25 +4,57 @@ module ExchangeRatesNBP
|
|
|
4
4
|
module Clients
|
|
5
5
|
class CurrencyDataSet
|
|
6
6
|
SELECTOR_CURRENCY_ELEMENT = 'tabela_kursow pozycja'.freeze
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
FIELD_AVERAGE_EXCHANGE_RATE = 'kurs_sredni'.freeze
|
|
8
9
|
FIELD_CURRENCY_CODE = 'kod_waluty'.freeze
|
|
10
|
+
FIELD_TABLE_NAME = 'numer_tabeli'.freeze
|
|
11
|
+
FIELD_PUBLISH_DATE = 'data_publikacji'.freeze
|
|
12
|
+
FIELD_CONVERSION_FACTOR = 'przelicznik'.freeze
|
|
9
13
|
|
|
10
14
|
def initialize(table_id)
|
|
11
15
|
@table_id = table_id
|
|
12
16
|
end
|
|
13
17
|
|
|
14
18
|
def exchange_rate(currency_code)
|
|
15
|
-
|
|
19
|
+
xml = Oga.parse_xml(data)
|
|
20
|
+
|
|
21
|
+
doc = selected_currency_element(xml, currency_code)
|
|
22
|
+
|
|
23
|
+
currency_element_to_hash(xml, doc)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def currency_element_to_hash(xml, doc)
|
|
29
|
+
{
|
|
30
|
+
exchange_rate: to_exchange_rate(doc),
|
|
31
|
+
publish_date: to_publish_date(xml),
|
|
32
|
+
conversion_factor: to_conversion_factor(doc)
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def to_publish_date(xml)
|
|
37
|
+
Date.parse(xml.css(FIELD_PUBLISH_DATE).text)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def to_exchange_rate(doc)
|
|
41
|
+
doc.css(FIELD_AVERAGE_EXCHANGE_RATE).text.tr(',', '.').to_f
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def to_conversion_factor(doc)
|
|
45
|
+
doc.css(FIELD_CONVERSION_FACTOR).text.to_i
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def selected_currency_element(xml, currency_code)
|
|
49
|
+
doc = xml.css(SELECTOR_CURRENCY_ELEMENT).detect do |p|
|
|
16
50
|
p.css(FIELD_CURRENCY_CODE).text == currency_code
|
|
17
51
|
end
|
|
18
52
|
|
|
19
53
|
raise "Invalid currency code: #{currency_code}" if doc.nil?
|
|
20
54
|
|
|
21
|
-
doc
|
|
55
|
+
doc
|
|
22
56
|
end
|
|
23
57
|
|
|
24
|
-
private
|
|
25
|
-
|
|
26
58
|
def data
|
|
27
59
|
HTTP.get(BASE_URL + "#{@table_id}.xml").to_s
|
|
28
60
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: exchange_rates_nbp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Maciej Majewski
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-
|
|
11
|
+
date: 2016-10-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: http
|
|
@@ -38,6 +38,20 @@ dependencies:
|
|
|
38
38
|
- - ">="
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: colorize
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
41
55
|
- !ruby/object:Gem::Dependency
|
|
42
56
|
name: bundler
|
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -191,6 +205,7 @@ files:
|
|
|
191
205
|
- ".rspec"
|
|
192
206
|
- ".rubocop.yml"
|
|
193
207
|
- ".travis.yml"
|
|
208
|
+
- CHANGELOG.md
|
|
194
209
|
- Gemfile
|
|
195
210
|
- LICENSE.txt
|
|
196
211
|
- README.md
|
|
@@ -223,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
223
238
|
version: '0'
|
|
224
239
|
requirements: []
|
|
225
240
|
rubyforge_project:
|
|
226
|
-
rubygems_version: 2.
|
|
241
|
+
rubygems_version: 2.6.4
|
|
227
242
|
signing_key:
|
|
228
243
|
specification_version: 4
|
|
229
244
|
summary: Collects exchange rates from nbp.pl
|