exchanges-rates-nbp 0.0.3 → 1.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eecec237aeccf2da7bf37cbcadbc279a1eabad99
4
- data.tar.gz: 623b287b7295f2a731da6ccb6bda24f620339e2a
3
+ metadata.gz: 9c5f535ac6ac56bd9b53da04e2e76c80abb7ea55
4
+ data.tar.gz: 07e58f2b034a76a7fbc21230b860b45cd5b0f0d4
5
5
  SHA512:
6
- metadata.gz: bc86f5ba990b21e6877f93798a5c7fc7095671c5abb1cc54db56f7d72997b08292c0193e714a68057b4cb7252516aaee3e50ba94e32b480873c1927a8ee2689f
7
- data.tar.gz: c408cbef13ebc8fc108b42903edb5ee4766eebaae27108bebc16081c3cca8b77fefe309241cc9cd4b7adf242561131d0bb2d8cb97764ce1080153ded7d6285c3
6
+ metadata.gz: 2c54a074354d610b3caf1c329a91cd49c3e02245fdc7c177e7ff31ec97e59e326d30b9a4f240059e57a04595410d027e1b91f041dd91d762f7829ff420cae7cf
7
+ data.tar.gz: f77539f87e757c55b03aadffd0d63092cda74f1ae0d67bfb7d2844fadde811894da1d487b775571ba9956c3649f45a8d4e10f4d3b020dd9f9126f4e965863c06
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ 0.0.1 initial commit
2
+ 0.0.2 small bugs
3
+ 0.0.3 bug, currency rate should be float
4
+ 1.0.0 selected currencies, creating class to retrieve currency rates
5
+ 1.0.2 best practices according to http://guides.rubygems.org/
6
+ 1.0.3 CHANGELOG, README
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- exchanges-rates-nbp (0.0.3)
4
+ exchanges-rates-nbp (1.0.3)
5
+ nokogiri (~> 1.6)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Exchanges::Rates::Nbp
2
2
 
3
- Simbple gem that gets exchange rates from the Polish National Bank.
3
+ Simple gem that gets exchange rates from the Polish National Bank.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,42 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Default usage:
22
+
23
+ nbp = Exchange::Nbp.new(nil, nil)
24
+
25
+ ,parameters:
26
+
27
+ * date - default Date.today, you can pass another valid date
28
+
29
+ * args - optional parameters
30
+
31
+
32
+ Class instance has three important methods:
33
+
34
+ * codes - returns currency symbols from table,
35
+
36
+ * published_at - returns publication date exchange rates,
37
+
38
+ * rates(currency) - returns currency rate at indicated date
39
+
40
+
41
+ If you interested only selected currencies at indicated day use in this way:
42
+
43
+ nbp = Exchange::Nbp.new(Date.today - 3, {selected_currencies: ['USD', 'EUR']})
44
+
45
+
46
+ Now you can retrieve currency rates for chosen:
47
+
48
+ nbp.codes.each do |c|
49
+ nbp.rates(c)
50
+ end
51
+
52
+
53
+ ## Future ideas
54
+ 1. Currency and CurrencyRate model, Sequel/ActiveRecord migration
55
+ 2. Background job to retrieve currency rates automatically
56
+
22
57
 
23
58
  ## Contributing
24
59
 
data/Rakefile CHANGED
@@ -1,2 +1,12 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ desc "Open an irb session preloaded with this library"
4
+ task :console do
5
+ require 'pry'
6
+ require 'exchanges-rates-nbp'
7
+
8
+ ARGV.clear
9
+ Pry.start
10
+
11
+ #sh "irb -rubygems -I lib -r exchanges-rates-nbp.rb"
12
+ end
@@ -11,11 +11,12 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ["krzysztof.wieslawski@gmail.com"]
12
12
  spec.summary = %q{Gem to retrieve exchange rates from the Polish National Bank (NBP)}
13
13
  spec.description = %q{}
14
- spec.homepage = ""
14
+ spec.homepage = "https://github.com/xborn/exchanges-rates-nbp"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0")
18
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ["lib"]
21
- end
21
+ spec.add_runtime_dependency "nokogiri", ["~> 1.6"]
22
+ end
@@ -0,0 +1,10 @@
1
+ require 'exchanges/rates'
2
+
3
+ class Exchanges::Nbp
4
+ include Exchanges::Rates
5
+
6
+ def initialize(date, args)
7
+ @date = date ||= Date.today
8
+ @selected_currencies = args.nil? ? [] : args[:selected_currencies]
9
+ end
10
+ end
@@ -6,23 +6,17 @@ require 'nokogiri'
6
6
 
7
7
  module Exchanges
8
8
  module Rates
9
- NBP = 'http://www.nbp.pl/kursy/xml/'
10
-
11
- @@date = Date.today
12
9
 
13
- def self.date
14
- @@date
15
- end
10
+ attr_accessor :date, :selected_currencies
16
11
 
17
- def self.date=(at_date)
18
- @@date = at_date
19
- end
12
+ NBP = 'http://www.nbp.pl/kursy/xml/'
20
13
 
21
- def self.codes
22
- self.xml.xpath("//pozycja/kod_waluty/text()").map {|c| c.to_s }
14
+ def codes
15
+ res = xml.xpath("//pozycja/kod_waluty/text()").map {|c| c.to_s }
16
+ @selected_currencies.empty? ? res : res.select {|c| @selected_currencies.include?(c)}
23
17
  end
24
18
 
25
- def self.rates(currency)
19
+ def rates(currency)
26
20
  rate = {}
27
21
  rate[:symbol] = xml.xpath("//pozycja[kod_waluty='#{currency}']/kod_waluty/text()").to_s
28
22
  rate[:name] = xml.xpath("//pozycja[kod_waluty='#{currency}']/nazwa_waluty/text()").to_s
@@ -31,28 +25,27 @@ module Exchanges
31
25
  rate
32
26
  end
33
27
 
34
- def self.published_at
28
+ def published_at
35
29
  Date.parse(xml.xpath("//tabela_kursow/data_publikacji/text()").to_s)
36
30
  end
37
31
 
38
- private
39
- def self.filename
32
+ def filename
40
33
  # according to: http://www.nbp.pl/home.aspx?f=/kursy/instrukcja_pobierania_kursow_walut.html
41
34
  index = Net::HTTP.get(URI.parse(NBP + 'dir.txt'))
42
35
 
43
- while index[/(a[0-9][0-9][0-9]z#{@@date.strftime("%y%m%d")}+)/, 1].nil? do
44
- @@date -= 1
36
+ while index[/(a[0-9][0-9][0-9]z#{@date.strftime("%y%m%d")}+)/, 1].nil? do
37
+ @date -= 1
45
38
  end
46
- index[/(a[0-9][0-9][0-9]z#{@@date.strftime("%y%m%d")}+)/, 1]
47
- end
39
+ index[/(a[0-9][0-9][0-9]z#{@date.strftime("%y%m%d")}+)/, 1]
40
+ end
48
41
 
49
- def self.url
50
- File.join(NBP, self.filename + '.xml') if self.filename
42
+ def url
43
+ File.join(NBP, filename + '.xml') if filename
51
44
  end
52
45
 
53
- def self.xml
46
+ def xml
54
47
  begin
55
- doc = open(self.url)
48
+ doc = open(url)
56
49
  rescue Exception => e
57
50
  puts e.message
58
51
  puts e.backtrace.inspect
@@ -60,5 +53,6 @@ module Exchanges
60
53
 
61
54
  Nokogiri::HTML(doc) if doc
62
55
  end
56
+
63
57
  end
64
58
  end
@@ -1,3 +1,3 @@
1
1
  module Exchanges
2
- VERSION = "0.0.3"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -0,0 +1 @@
1
+ require 'exchanges/nbp'
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Exchanges::Nbp do
4
+
5
+ before(:all) do
6
+ @dummy = Exchanges::Nbp.new(nil, nil)
7
+ end
8
+
9
+ context 'when date is not passed' do
10
+ it 'returns @date equal Date.today' do
11
+ expect(@dummy.date).to eq Date.today
12
+ end
13
+ end
14
+
15
+ context 'when date is passed e.g. \'2014-08-31\'' do
16
+ before(:all) do
17
+ @dummy = Exchanges::Nbp.new(Date.new(2014,8,31), nil)
18
+ end
19
+
20
+ it 'returns @date equal Date.today' do
21
+ expect(@dummy.date).to eq Date.new(2014,8,31)
22
+ end
23
+ end
24
+
25
+ context 'when two valid currencies are chosen' do
26
+ before(:all) do
27
+ @dummy = Exchanges::Nbp.new(nil, {selected_currencies: ['USD','EUR']})
28
+ end
29
+
30
+ it '#codes returns 2-elements Array' do
31
+ expect(@dummy.codes).to be_kind_of(Array)
32
+ expect(@dummy.codes.length).to eq 2
33
+ end
34
+ end
35
+
36
+ context 'when any currencies are selected at \'2014-08-31\'' do
37
+ before(:all) do
38
+ @dummy = Exchanges::Nbp.new(nil, nil)
39
+ end
40
+
41
+ it '#codes returns 36-elements Array' do
42
+ expect(@dummy.codes.length).to eq 36
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ class DummyClass
4
+ include Exchanges::Rates
5
+
6
+ def initialize(date, args)
7
+ @date = date ||= Date.today
8
+ @selected_currencies = args.nil? ? [] : args[:selected_currencies]
9
+ end
10
+ end
11
+
12
+ RSpec.describe Exchanges::Rates do
13
+ include Exchanges::Rates
14
+
15
+ context 'when date is default' do
16
+ before(:each) do
17
+ @dummy = DummyClass.new(Date.today, nil)
18
+ end
19
+
20
+ describe 'constant NBP' do
21
+ it 'responds to constant NBP' do
22
+ expect(Exchanges::Rates::NBP).to be
23
+ end
24
+ it 'NBP is url' do
25
+ expect(Exchanges::Rates::NBP.match(/^http:\/\//)).to be_truthy
26
+ end
27
+ end
28
+
29
+ describe '@date' do
30
+ it 'module responds to @@date' do
31
+ expect(@dummy.date).to be
32
+ end
33
+ it 'default @date is today' do
34
+ expect(@dummy.date).to eq Date.today
35
+ end
36
+ it 'is possible to change @date' do
37
+ @dummy.date = Date.today - 3
38
+ expect(@dummy.date).to eq Date.today - 3
39
+ end
40
+ end
41
+
42
+ it 'returns url to xml file' do
43
+ expect(@dummy.url.match(/.xml$/)).to be_truthy
44
+ end
45
+
46
+ describe '#xml' do
47
+ it 'returns xml for chosen date' do
48
+ xml = @dummy.xml
49
+ expect(xml.xpath("//data_publikacji/text()").to_s).to eq @dummy.date.strftime("%Y-%m-%d")
50
+ end
51
+ end
52
+
53
+ describe '#codes' do
54
+ it 'returns array' do
55
+ expect(@dummy.codes).to be_kind_of(Array)
56
+ end
57
+ end
58
+
59
+ describe '#rates' do
60
+ it 'returns Hash contained necessary informations' do
61
+ expect(@dummy.rates('USD')).to be_kind_of(Hash)
62
+ end
63
+ it 'Hash contains keys :symbol, :name, :base, :average_rate' do
64
+ expect(@dummy.rates('USD').keys.sort).to eq [:average_rate, :base, :name, :symbol]
65
+ end
66
+ it 'rate is a Float' do
67
+ expect(@dummy.rates('USD')[:average_rate]).to be_kind_of(Float)
68
+ end
69
+ it 'symbol, name, base and rate are not empty' do
70
+ [:symbol, :name, :base, :average_rate].each do |k|
71
+ expect(@dummy.rates('USD')[k].to_s).not_to be_empty
72
+ end
73
+ end
74
+ end
75
+
76
+ describe '#published_at' do
77
+ it 'returns Date' do
78
+ expect(@dummy.published_at).to be_kind_of(Date)
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ context 'when date is weekend day \'2014-08-31\', Sunday' do
85
+ before(:each) do
86
+ @dummy = DummyClass.new(Date.new(2014, 8, 31), nil)
87
+ end
88
+
89
+ describe '#filename' do
90
+ it 'returns \'a167z140829\' for Sunday, 2014-08-31\'' do
91
+ expect(@dummy.filename).to eq 'a167z140829'
92
+ end
93
+ it 'returns \'a167z140829\' for Friday, 2014-08-29\'' do
94
+ expect(@dummy.filename).to eq 'a167z140829'
95
+ end
96
+ end
97
+
98
+ describe '#rates' do
99
+ it 'at \'2014-08-31\' USD rate is 3.1965 (from table at \'2014-08-29\')' do
100
+ expect(@dummy.rates('USD')[:average_rate]).to eq 3.1965
101
+ end
102
+ end
103
+ end
104
+
105
+ context 'when only \'USD\' is selected' do
106
+ before(:each) do
107
+ @dummy = DummyClass.new(Date.new(2014, 8, 31), {selected_currencies: ['USD']})
108
+ end
109
+
110
+ describe '#codes' do
111
+ it 'returns only one code' do
112
+ expect(@dummy.codes.length).to eq 1
113
+ end
114
+ end
115
+ end
116
+
117
+ context 'when only \'USD\', \'EUR\' and \'GBP\' are selected' do
118
+ before(:each) do
119
+ @dummy = DummyClass.new(Date.new(2014, 8, 31), {selected_currencies: ['USD', 'EUR', 'GBP']})
120
+ end
121
+
122
+ describe '#codes' do
123
+ it 'returns three codes' do
124
+ expect(@dummy.codes.length).to eq 3
125
+ end
126
+ end
127
+ end
128
+
129
+ context 'when wrong code is selected' do
130
+ before(:each) do
131
+ @dummy = DummyClass.new(Date.new(2014, 8, 31), {selected_currencies: ['XXX']})
132
+ end
133
+
134
+ describe '#codes' do
135
+ it 'returns three codes' do
136
+ expect(@dummy.codes.length).to eq 0
137
+ end
138
+ end
139
+ end
140
+
141
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
 
4
- require 'nbp'
4
+ require 'exchanges/nbp'
5
5
 
6
6
  RSpec.configure do |config|
7
7
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exchanges-rates-nbp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - krzysztof.wieslawski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-05 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
13
27
  description: ''
14
28
  email:
15
29
  - krzysztof.wieslawski@gmail.com
@@ -18,6 +32,7 @@ extensions: []
18
32
  extra_rdoc_files: []
19
33
  files:
20
34
  - ".gitignore"
35
+ - CHANGELOG.md
21
36
  - Gemfile
22
37
  - Gemfile.lock
23
38
  - Guardfile
@@ -25,13 +40,15 @@ files:
25
40
  - README.md
26
41
  - Rakefile
27
42
  - exchanges-rates-nbp.gemspec
43
+ - lib/exchanges-rates-nbp.rb
44
+ - lib/exchanges/nbp.rb
28
45
  - lib/exchanges/rates.rb
29
46
  - lib/exchanges/version.rb
30
- - lib/nbp.rb
31
- - spec/exchanges-rates-nbp_spec.rb
47
+ - spec/exchanges_nbp_spec.rb
48
+ - spec/exchanges_rates_spec.rb
32
49
  - spec/spec_helper.rb
33
50
  - spec/support/a167z140829.xml
34
- homepage: ''
51
+ homepage: https://github.com/xborn/exchanges-rates-nbp
35
52
  licenses:
36
53
  - MIT
37
54
  metadata: {}
@@ -56,6 +73,7 @@ signing_key:
56
73
  specification_version: 4
57
74
  summary: Gem to retrieve exchange rates from the Polish National Bank (NBP)
58
75
  test_files:
59
- - spec/exchanges-rates-nbp_spec.rb
76
+ - spec/exchanges_nbp_spec.rb
77
+ - spec/exchanges_rates_spec.rb
60
78
  - spec/spec_helper.rb
61
79
  - spec/support/a167z140829.xml
data/lib/nbp.rb DELETED
@@ -1,2 +0,0 @@
1
- require 'exchanges/version'
2
- require 'exchanges/rates'
@@ -1,95 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Exchanges::Rates do
4
-
5
- context 'when date is default' do
6
- before(:each) do
7
- Exchanges::Rates.date = Date.today
8
- end
9
-
10
- describe 'constant NBP' do
11
- it 'responds to constant NBP' do
12
- expect(Exchanges::Rates::NBP).to be
13
- end
14
- it 'NBP is url' do
15
- expect(Exchanges::Rates::NBP.match(/^http:\/\//)).to be_truthy
16
- end
17
- end
18
-
19
- describe '@@date' do
20
- it 'module responds to @@date' do
21
- expect(Exchanges::Rates.date).to be
22
- end
23
- it 'default @@date is today' do
24
- expect(Exchanges::Rates.date).to eq Date.today
25
- end
26
- it 'is possible to change @@date' do
27
- Exchanges::Rates.date = Date.today - 3
28
- expect(Exchanges::Rates.date).to eq Date.today - 3
29
- end
30
- end
31
-
32
- it 'returns url to xml file' do
33
- expect(Exchanges::Rates.url.match(/.xml$/)).to be_truthy
34
- end
35
-
36
- describe '.xml' do
37
- it 'returns xml for chosen date' do
38
- xml = Exchanges::Rates.xml
39
- expect(xml.xpath("//data_publikacji/text()").to_s).to eq Exchanges::Rates.date.strftime("%Y-%m-%d")
40
- end
41
- end
42
-
43
- describe '.codes' do
44
- it 'returns array' do
45
- expect(Exchanges::Rates.codes).to be_kind_of(Array)
46
- end
47
- end
48
-
49
- describe '.rates' do
50
- it 'returns Hash contained necessary informations' do
51
- expect(Exchanges::Rates.rates('USD')).to be_kind_of(Hash)
52
- end
53
- it 'Hash contains keys :symbol, :name, :base, :average_rate' do
54
- expect(Exchanges::Rates.rates('USD').keys.sort).to eq [:average_rate, :base, :name, :symbol]
55
- end
56
- it 'rate is a Float' do
57
- expect(Exchanges::Rates.rates('USD')[:average_rate]).to be_kind_of(Float)
58
- end
59
- it 'symbol, name, base and rate are not empty' do
60
- [:symbol, :name, :base, :average_rate].each do |k|
61
- expect(Exchanges::Rates.rates('USD')[k].to_s).not_to be_empty
62
- end
63
- end
64
- end
65
-
66
- describe '.published_at' do
67
- it 'returns Date' do
68
- expect(Exchanges::Rates.published_at).to be_kind_of(Date)
69
- end
70
- end
71
-
72
- end
73
-
74
- context 'when date is weekend day \'2014-08-31\', Sunday' do
75
- before(:each) do
76
- Exchanges::Rates.date = Date.new(2014,8,31)
77
- end
78
-
79
- describe '.filename' do
80
- it 'returns filename \'a167z140829\' for Sunday, 2014-08-31\'' do
81
- expect(Exchanges::Rates.filename).to eq 'a167z140829'
82
- end
83
- it 'returns filename \'a167z140829\' for Friday, 2014-08-29\'' do
84
- expect(Exchanges::Rates.filename).to eq 'a167z140829'
85
- end
86
- end
87
-
88
- describe '.rates' do
89
- it 'at \'2014-08-31\' USD rate is 3.1965 (from table at \'2014-08-29\')' do
90
- expect(Exchanges::Rates.rates('USD')[:average_rate]).to eq 3.1965
91
- end
92
- end
93
- end
94
-
95
- end