exchange_rates_nbp 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 660739a3cfd2ed77321e72db05a94c9d3f3136c2
4
- data.tar.gz: 2c81afbd697cc12b0594e199385efd58465bc4ae
3
+ metadata.gz: 41009cf622b4fb1042aa0db0dae7a2dd9b963c65
4
+ data.tar.gz: 613dbe94db7fb1dfe6931a2a190bd0f5a0b6052a
5
5
  SHA512:
6
- metadata.gz: dc236b567256cf7e6d7a240e19e6ab25688a24cdf385665c6f837b442f127cecca7ece7eb348bbe53a7331e7b3b56b5cac8b9dd47daf8d0bbe598a428c00902a
7
- data.tar.gz: 72156e389610fdd165f48ffc3336ba3366ec752b68a0fd1cde3b4d9acdf140b9cdb5f8596d28db18d84fc844b2ba1c7bb6285e1bcffce4e442c42364ff9718f1
6
+ metadata.gz: 2527f8c435f7c7a8904b9daa8190ea243ba462d63a4ab3153dd22341752ce37f6e7307eaf9487d269ec28602d914a53190a2093b976fdfa2eaa1446698d1d356
7
+ data.tar.gz: a215f1692f1f18ce3f4120befc50ccfd345c48ab64ad08e41d8b03d8e1f86dcc59a6555e4045303341771063d6d3c7e5ef58e524184ff2cdb6f68fa4ec6f7360
@@ -1,7 +1,4 @@
1
1
  Style/Documentation:
2
2
  Enabled: false
3
3
 
4
- Lint/UnusedBlockArgument:
5
- Enabled: false
6
-
7
4
  require: rubocop-rspec
@@ -0,0 +1,11 @@
1
+ # 0.1.2 / 2016-10-01
2
+
3
+ * Add verbose mode
4
+
5
+ # 0.1.1 / 2016-03-06
6
+
7
+ * Fixed gemset runtime dependency definitions
8
+
9
+ # 0.1.0 / 2016-03-06
10
+
11
+ * Initial release
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # ExchangeRatesNBP
2
2
 
3
3
  [![Build Status](https://travis-ci.org/maciejmajewski/exchange_rates_nbp.svg?branch=master)](https://travis-ci.org/maciejmajewski/exchange_rates_nbp)
4
+ [![Gem Version](https://badge.fury.io/rb/exchange_rates_nbp.svg)](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.
@@ -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'
@@ -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
- puts ExchangeRatesNBP.exchange_rate(options[:date], options[:currency_code])
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
@@ -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
- table_id = find_table_id(date, table)
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.find_table_id(date, table)
30
- Clients::TableList.new(date.year, table).fetch_closest_to(date)
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
- def self.extract_currency(table_id, currency_code)
34
- ExchangeRatesNBP::Clients::CurrencyDataSet.new(table_id)
35
- .exchange_rate(currency_code)
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
- FIELD_AVERAGE_CURRENCY_CODE = 'kurs_sredni'.freeze
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
- doc = Oga.parse_xml(data).css(SELECTOR_CURRENCY_ELEMENT).detect do |p|
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.css(FIELD_AVERAGE_CURRENCY_CODE).text.tr(',', '.').to_f
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
@@ -1,3 +1,3 @@
1
1
  module ExchangeRatesNBP
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  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.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-03-06 00:00:00.000000000 Z
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.5.1
241
+ rubygems_version: 2.6.4
227
242
  signing_key:
228
243
  specification_version: 4
229
244
  summary: Collects exchange rates from nbp.pl