nbp_exchange 0.0.1
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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +19 -0
- data/Rakefile +2 -0
- data/lib/nbp_exchange/currency.rb +38 -0
- data/lib/nbp_exchange/currency_nodes.rb +36 -0
- data/lib/nbp_exchange/currency_xml.rb +33 -0
- data/lib/nbp_exchange/rate.rb +22 -0
- data/lib/nbp_exchange/version.rb +3 -0
- data/lib/nbp_exchange.rb +13 -0
- data/nbp_exchange.gemspec +27 -0
- data/spec/cassettes/xml.yml +0 -0
- data/spec/nbp_exchange/currency_spec.rb +23 -0
- data/spec/nbp_exchange/currency_xml_spec.rb +25 -0
- data/spec/nbp_exchange/rate_spec.rb +15 -0
- data/spec/spec_helper.rb +21 -0
- metadata +111 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
NbpExchange (in dev)
|
2
|
+
====================
|
3
|
+
|
4
|
+
Simple gem for getting average exchange rate for PLN (polski zloty) from NBP
|
5
|
+
---------------------------------------------------------------------------
|
6
|
+
|
7
|
+
This gem helps to get average exchange rate PLN to (USD,EUR) for given date.
|
8
|
+
For version 0.0.1 it supports only dates from 2011-08-01
|
9
|
+
|
10
|
+
Usage
|
11
|
+
-----
|
12
|
+
|
13
|
+
```
|
14
|
+
currency = NbpExhchange::Currency.new('usd')
|
15
|
+
|
16
|
+
rate = currency.rate(Date.parse("2011-11-02"))
|
17
|
+
rate.average_currency_rate
|
18
|
+
```
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
module NbpExchange
|
3
|
+
|
4
|
+
class Currency
|
5
|
+
CurrencieNames = {
|
6
|
+
'eur' => 'Euro',
|
7
|
+
'usd' => 'Dolar amerykański'
|
8
|
+
}
|
9
|
+
|
10
|
+
attr_reader :symbol
|
11
|
+
|
12
|
+
def initialize(symbol)
|
13
|
+
@symbol = symbol.downcase
|
14
|
+
end
|
15
|
+
|
16
|
+
def name
|
17
|
+
@name ||= CurrencieNames[symbol]
|
18
|
+
end
|
19
|
+
|
20
|
+
def rate(date)
|
21
|
+
@rate ||= load_rate(date)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.available_currency_keys
|
25
|
+
CurrencieNames.keys
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def load_rate(date)
|
31
|
+
date = (date.is_a?(String)) ? Date.parse(date) : date
|
32
|
+
cn = CurrencyNodes.new(date)
|
33
|
+
raw_rate = cn.find(symbol)
|
34
|
+
Rate.parse(self, date, raw_rate)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module NbpExchange
|
2
|
+
|
3
|
+
class CurrencyNodes
|
4
|
+
|
5
|
+
def initialize(date)
|
6
|
+
@date = date
|
7
|
+
@xml = CurrencyXml.new(@date)
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(symbol)
|
11
|
+
node = nodes.xpath("//pozycja[kod_waluty='#{symbol.upcase}']")
|
12
|
+
raise RateNotFound if node.empty?
|
13
|
+
node
|
14
|
+
end
|
15
|
+
|
16
|
+
def nodes
|
17
|
+
@nodes ||= load_nodes
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def load_nodes
|
23
|
+
doc = Nokogiri::XML(@xml.open_xml)
|
24
|
+
pub_date= doc.xpath('tabela_kursow/data_publikacji').text
|
25
|
+
raise InvalidDate unless valid?(pub_date)
|
26
|
+
|
27
|
+
doc.xpath('tabela_kursow/pozycja')
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid?(pub_date)
|
31
|
+
Date.parse(pub_date) == @date
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module NbpExchange
|
2
|
+
|
3
|
+
class CurrencyXml
|
4
|
+
DIR_URL = "http://rss.nbp.pl/kursy/xml2/%{year}/a/dir.aspx"
|
5
|
+
XML_URL = "http://rss.nbp.pl/kursy/xml2/%{year}/a/%{xml_name}"
|
6
|
+
|
7
|
+
def initialize(date)
|
8
|
+
@date = date
|
9
|
+
end
|
10
|
+
|
11
|
+
def open_xml
|
12
|
+
page = open(xml_url)
|
13
|
+
page.read
|
14
|
+
end
|
15
|
+
|
16
|
+
def xml_name
|
17
|
+
page = open(dir_url)
|
18
|
+
doc = Nokogiri::HTML(page.read)
|
19
|
+
name = doc.text[/^#{@date.to_s}.+/][/(\w+.xml)/, 1]
|
20
|
+
raise NoXMLForThisDate if name.nil? || name.empty?
|
21
|
+
name
|
22
|
+
end
|
23
|
+
|
24
|
+
def xml_url
|
25
|
+
XML_URL % {:year => @date.year, :xml_name => xml_name}
|
26
|
+
end
|
27
|
+
|
28
|
+
def dir_url
|
29
|
+
DIR_URL % {:year => @date.year}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module NbpExchange
|
2
|
+
|
3
|
+
class Rate
|
4
|
+
|
5
|
+
attr_reader :currency
|
6
|
+
attr_reader :date
|
7
|
+
attr_reader :average_exchange_rate
|
8
|
+
|
9
|
+
def initialize(currency, date, aer)
|
10
|
+
@date = date
|
11
|
+
@currency = currency
|
12
|
+
@average_exchange_rate = aer
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.parse(currency, date, raw)
|
16
|
+
aer = raw.xpath("kurs_sredni").text
|
17
|
+
aer = aer.gsub(',','.').to_f
|
18
|
+
Rate.new(currency, date, aer)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/lib/nbp_exchange.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'nbp_exchange/currency'
|
5
|
+
require 'nbp_exchange/rate'
|
6
|
+
require 'nbp_exchange/currency_nodes'
|
7
|
+
require 'nbp_exchange/currency_xml'
|
8
|
+
|
9
|
+
module NbpExchange
|
10
|
+
class InvalidDate < ArgumentError;end
|
11
|
+
class RateNotFound < RuntimeError;end
|
12
|
+
class NoXMLForThisDate < RuntimeError;end
|
13
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "nbp_exchange/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "nbp_exchange"
|
7
|
+
s.version = NbpExchange::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Michal Wrobel"]
|
10
|
+
s.email = ["sparrovv@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{NBP average exchange rates for PLN}
|
13
|
+
s.description = %q{Get average exchange rate for PLN polski zloty from specific date. This lib wraps http://rss.nbp.pl/kursy/xml2/2011/a/dir.aspx site. }
|
14
|
+
|
15
|
+
s.rubyforge_project = "nbp_exchange"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_runtime_dependency('nokogiri')
|
23
|
+
|
24
|
+
s.add_development_dependency('rspec')
|
25
|
+
s.add_development_dependency('vcr')
|
26
|
+
s.add_development_dependency('fakeweb')
|
27
|
+
end
|
File without changes
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
module NbpExchange
|
4
|
+
describe Currency do
|
5
|
+
|
6
|
+
let!(:currency){Currency.new(CURRENCY_SYMBOL)}
|
7
|
+
|
8
|
+
describe "#name" do
|
9
|
+
it "shoud have name" do
|
10
|
+
currency.name.should == "Euro"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#rate" do
|
15
|
+
let(:expected){Rate.new(currency, EXCHANGE_DATE, 4.4146)}
|
16
|
+
|
17
|
+
it "should return average rate for given date" do
|
18
|
+
r = currency.rate(EXCHANGE_DATE)
|
19
|
+
r.average_exchange_rate.should == expected.average_exchange_rate
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
module NbpExchange
|
4
|
+
describe CurrencyXml do
|
5
|
+
|
6
|
+
let(:xml){ CurrencyXml.new(EXCHANGE_DATE)}
|
7
|
+
|
8
|
+
describe "#xml_name" do
|
9
|
+
|
10
|
+
it "should return proper name for the date" do
|
11
|
+
xml.xml_name.should == "11a212.xml"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "open_xml" do
|
17
|
+
|
18
|
+
it "should return tabla_kursow" do
|
19
|
+
file = xml.open_xml
|
20
|
+
file[/tabela_kursow/i].should == "tabela_kursow"
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
module NbpExchange
|
4
|
+
|
5
|
+
describe Rate do
|
6
|
+
let(:currency){Currency.new(CURRENCY_SYMBOL)}
|
7
|
+
let(:rate){Rate.new(currency, EXCHANGE_DATE, 12)}
|
8
|
+
|
9
|
+
describe ".parse" do
|
10
|
+
it "should parse rate from xml object" do
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "rspec"
|
2
|
+
#require 'fakeweb'
|
3
|
+
#require 'vcr'
|
4
|
+
require 'nbp_exchange'
|
5
|
+
require 'date'
|
6
|
+
|
7
|
+
#FakeWeb.allow_net_connect = false
|
8
|
+
|
9
|
+
#VCR.config do |c|
|
10
|
+
#c.cassette_library_dir = 'spec/cassettes'
|
11
|
+
#c.stub_with :fakeweb
|
12
|
+
#c.default_cassette_options = { :record => :once }
|
13
|
+
#end
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
#config.extend VCR::RSpec::Macros
|
17
|
+
end
|
18
|
+
|
19
|
+
CURRENCY_SYMBOL = 'eur'
|
20
|
+
EXCHANGE_DATE = Date.parse("2011-11-02")
|
21
|
+
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nbp_exchange
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michal Wrobel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-08 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: &2155739640 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2155739640
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &2155738980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2155738980
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: vcr
|
38
|
+
requirement: &2155738360 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2155738360
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: fakeweb
|
49
|
+
requirement: &2155737580 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2155737580
|
58
|
+
description: ! 'Get average exchange rate for PLN polski zloty from specific date.
|
59
|
+
This lib wraps http://rss.nbp.pl/kursy/xml2/2011/a/dir.aspx site. '
|
60
|
+
email:
|
61
|
+
- sparrovv@gmail.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- .gitignore
|
67
|
+
- Gemfile
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/nbp_exchange.rb
|
71
|
+
- lib/nbp_exchange/currency.rb
|
72
|
+
- lib/nbp_exchange/currency_nodes.rb
|
73
|
+
- lib/nbp_exchange/currency_xml.rb
|
74
|
+
- lib/nbp_exchange/rate.rb
|
75
|
+
- lib/nbp_exchange/version.rb
|
76
|
+
- nbp_exchange.gemspec
|
77
|
+
- spec/cassettes/xml.yml
|
78
|
+
- spec/nbp_exchange/currency_spec.rb
|
79
|
+
- spec/nbp_exchange/currency_xml_spec.rb
|
80
|
+
- spec/nbp_exchange/rate_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
homepage: ''
|
83
|
+
licenses: []
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project: nbp_exchange
|
102
|
+
rubygems_version: 1.8.11
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: NBP average exchange rates for PLN
|
106
|
+
test_files:
|
107
|
+
- spec/cassettes/xml.yml
|
108
|
+
- spec/nbp_exchange/currency_spec.rb
|
109
|
+
- spec/nbp_exchange/currency_xml_spec.rb
|
110
|
+
- spec/nbp_exchange/rate_spec.rb
|
111
|
+
- spec/spec_helper.rb
|