cnb 0.0.5 → 0.0.7
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/lib/cnb/{cur_rates_parser.rb → currency_rates.rb} +6 -14
- data/lib/cnb/daily_rates.rb +10 -0
- data/lib/cnb/{monthly_cur_rates_parser.rb → monthly_rates.rb} +1 -7
- data/lib/cnb/version.rb +1 -1
- data/lib/cnb.rb +5 -6
- data/spec/models/{daily_cur_rates_parser_spec.rb → daily_rates_spec.rb} +4 -4
- data/spec/models/{monthly_cur_rates_parser_spec.rb → monthly_rates_spec.rb} +10 -10
- data/spec/spec_helper.rb +3 -4
- metadata +13 -15
- data/config/cnb.yml.example +0 -1
- data/lib/cnb/daily_cur_rates_parser.rb +0 -23
- data/lib/cnb/load_config.rb +0 -15
- /data/spec/{examples → fixtures}/daily_cur_rates.txt +0 -0
- /data/spec/{examples → fixtures}/monthly_cur_rates_2013_3.txt +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74fa46a97bd5a68b12189edb0df0181100d04f1a
|
4
|
+
data.tar.gz: 17ee1ff9c96b3687285b34314e330b617e239b00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc371ab478158c10e9ca72dc46cb0c9d91e77262df1071288b4d858042dc9ace570d2604e510620ce6837642a4c1d291d03ddd50dde03cd5a464bfbbd5234443
|
7
|
+
data.tar.gz: eec19f4f464a1fbeb2a29be178c2bff5ea86ec9e798a5d32e8361d9a857820a137a4301c51f6034556dff527f537463fcf15fff0dc57b66010e6380f2c9fc77d
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'open-uri'
|
2
2
|
|
3
3
|
module CNB
|
4
|
-
class
|
4
|
+
class CurrencyRates
|
5
5
|
CUR_RATES_URL = 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/'
|
6
6
|
|
7
7
|
attr_reader :cur_rates_filepath, :date
|
@@ -26,10 +26,10 @@ module CNB
|
|
26
26
|
@currencies[cur_code]
|
27
27
|
end
|
28
28
|
|
29
|
-
def parse
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
def parse
|
30
|
+
text = get_page_content(@cur_rates_url)
|
31
|
+
lines = text.split("\n")
|
32
|
+
@date = parse_date(lines.first)
|
33
33
|
|
34
34
|
lines.shift(2)
|
35
35
|
lines.each do |line|
|
@@ -47,19 +47,11 @@ module CNB
|
|
47
47
|
{ cur_code: columns[3], data: data }
|
48
48
|
end
|
49
49
|
|
50
|
-
def parse_date(
|
51
|
-
line = File.open(filepath) { |f| f.readline }
|
50
|
+
def parse_date(line)
|
52
51
|
date = line[0..line.index('#') - 1].chomp
|
53
52
|
Date.parse(date)
|
54
53
|
end
|
55
54
|
|
56
|
-
def download_cur_rates(url)
|
57
|
-
content = get_page_content(url)
|
58
|
-
f = File.new(@cur_rates_filepath, 'w')
|
59
|
-
f.write(content)
|
60
|
-
f.close
|
61
|
-
end
|
62
|
-
|
63
55
|
def get_page_content(url)
|
64
56
|
file = open(url)
|
65
57
|
file.read
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module CNB
|
2
|
-
class
|
2
|
+
class MonthlyRates < CurrencyRates
|
3
3
|
def initialize(month, year)
|
4
4
|
@month = month
|
5
5
|
@year = year
|
@@ -9,18 +9,12 @@ module CNB
|
|
9
9
|
|
10
10
|
@currencies = {}
|
11
11
|
@cur_rates_url = "#{CUR_RATES_URL}/kurzy_ostatnich_men/kurzy.txt?mesic=#{month}&rok=#{year}"
|
12
|
-
@cur_rates_filepath = "#{CONFIG['exchange_rates_dir']}/monthly_cur_rates_#{year}_#{month}.txt"
|
13
12
|
|
14
13
|
parse
|
15
14
|
end
|
16
15
|
|
17
16
|
private
|
18
17
|
|
19
|
-
def parse
|
20
|
-
download_cur_rates(@cur_rates_url) unless File.exist?(cur_rates_filepath)
|
21
|
-
super(cur_rates_filepath)
|
22
|
-
end
|
23
|
-
|
24
18
|
def valid_month?
|
25
19
|
@month.to_i >= 1 && @month.to_i <= 12
|
26
20
|
end
|
data/lib/cnb/version.rb
CHANGED
data/lib/cnb.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
|
-
require_relative 'cnb/
|
2
|
-
require_relative 'cnb/
|
3
|
-
require_relative 'cnb/
|
4
|
-
require_relative 'cnb/monthly_cur_rates_parser'
|
1
|
+
require_relative 'cnb/currency_rates'
|
2
|
+
require_relative 'cnb/daily_rates'
|
3
|
+
require_relative 'cnb/monthly_rates'
|
5
4
|
|
6
5
|
module CNB
|
7
6
|
PRIMARY_CURRENCY = 'CZK'
|
8
7
|
|
9
8
|
class << self
|
10
9
|
def daily_rates
|
11
|
-
@parser =
|
10
|
+
@parser = DailyRates.new
|
12
11
|
end
|
13
12
|
|
14
13
|
def monthly_rates(month, year)
|
15
|
-
@parser =
|
14
|
+
@parser = MonthlyRates.new(month, year)
|
16
15
|
end
|
17
16
|
|
18
17
|
def date
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe CNB::
|
3
|
+
describe CNB::DailyRates do
|
4
4
|
before(:each) do
|
5
|
-
filepath = File.join('spec', '
|
6
|
-
CNB::
|
7
|
-
@parser = CNB::
|
5
|
+
filepath = File.join('spec', 'fixtures', 'daily_cur_rates.txt')
|
6
|
+
CNB::DailyRates.any_instance.stub(:get_page_content) { File.read(filepath) }
|
7
|
+
@parser = CNB::DailyRates.new
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'returns date from the exchange rates file' do
|
@@ -1,22 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe CNB::
|
3
|
+
describe CNB::MonthlyRates do
|
4
4
|
before(:each) do
|
5
|
-
filepath = File.join('spec', '
|
6
|
-
CNB::
|
7
|
-
@parser = CNB::
|
5
|
+
filepath = File.join('spec', 'fixtures', 'monthly_cur_rates_2013_3.txt')
|
6
|
+
CNB::MonthlyRates.any_instance.stub(:get_page_content) { File.read(filepath) }
|
7
|
+
@parser = CNB::MonthlyRates.new(3, 2013)
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'raises error if required month is invalid' do
|
11
|
-
expect{CNB::
|
12
|
-
expect{CNB::
|
13
|
-
expect{CNB::
|
11
|
+
expect{CNB::MonthlyRates.new('abc', 2013)}.to raise_error(StandardError)
|
12
|
+
expect{CNB::MonthlyRates.new(0, 2013)}.to raise_error(StandardError)
|
13
|
+
expect{CNB::MonthlyRates.new(13, 2013)}.to raise_error(StandardError)
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'raises error if required year is invalid' do
|
17
|
-
expect{CNB::
|
18
|
-
expect{CNB::
|
19
|
-
expect{CNB::
|
17
|
+
expect{CNB::MonthlyRates.new(3, 'abc')}.to raise_error(StandardError)
|
18
|
+
expect{CNB::MonthlyRates.new(3, 2000)}.to raise_error(StandardError)
|
19
|
+
expect{CNB::MonthlyRates.new(3, 2050)}.to raise_error(StandardError)
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'returns date from the exchange rates file' do
|
data/spec/spec_helper.rb
CHANGED
@@ -6,10 +6,9 @@ if ENV['COVERAGE']
|
|
6
6
|
SimpleCov.start
|
7
7
|
end
|
8
8
|
|
9
|
-
require 'cnb/
|
10
|
-
require 'cnb/
|
11
|
-
require 'cnb/
|
12
|
-
require 'cnb/monthly_cur_rates_parser'
|
9
|
+
require 'cnb/currency_rates'
|
10
|
+
require 'cnb/daily_rates'
|
11
|
+
require 'cnb/monthly_rates'
|
13
12
|
|
14
13
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
15
14
|
# in spec/support/ and its subdirectories.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cnb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Zikán
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -80,17 +80,15 @@ files:
|
|
80
80
|
- LICENSE
|
81
81
|
- README.md
|
82
82
|
- cnb.gemspec
|
83
|
-
- config/cnb.yml.example
|
84
83
|
- lib/cnb.rb
|
85
|
-
- lib/cnb/
|
86
|
-
- lib/cnb/
|
87
|
-
- lib/cnb/
|
88
|
-
- lib/cnb/monthly_cur_rates_parser.rb
|
84
|
+
- lib/cnb/currency_rates.rb
|
85
|
+
- lib/cnb/daily_rates.rb
|
86
|
+
- lib/cnb/monthly_rates.rb
|
89
87
|
- lib/cnb/version.rb
|
90
|
-
- spec/
|
91
|
-
- spec/
|
92
|
-
- spec/models/
|
93
|
-
- spec/models/
|
88
|
+
- spec/fixtures/daily_cur_rates.txt
|
89
|
+
- spec/fixtures/monthly_cur_rates_2013_3.txt
|
90
|
+
- spec/models/daily_rates_spec.rb
|
91
|
+
- spec/models/monthly_rates_spec.rb
|
94
92
|
- spec/spec_helper.rb
|
95
93
|
homepage: http://www.uol.cz
|
96
94
|
licenses:
|
@@ -118,7 +116,7 @@ signing_key:
|
|
118
116
|
specification_version: 4
|
119
117
|
summary: CNB currency rates
|
120
118
|
test_files:
|
121
|
-
- spec/
|
122
|
-
- spec/
|
123
|
-
- spec/models/
|
124
|
-
- spec/models/
|
119
|
+
- spec/fixtures/daily_cur_rates.txt
|
120
|
+
- spec/fixtures/monthly_cur_rates_2013_3.txt
|
121
|
+
- spec/models/daily_rates_spec.rb
|
122
|
+
- spec/models/monthly_rates_spec.rb
|
data/config/cnb.yml.example
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
exchange_rates_dir: /tmp
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module CNB
|
2
|
-
class DailyCurRatesParser < CurRatesParser
|
3
|
-
def initialize
|
4
|
-
@currencies = {}
|
5
|
-
@cur_rates_url = "#{CUR_RATES_URL}kurzy_devizoveho_trhu/denni_kurz.txt"
|
6
|
-
@cur_rates_filepath = "#{CONFIG['exchange_rates_dir']}/daily_cur_rates.txt"
|
7
|
-
|
8
|
-
parse
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def parse
|
14
|
-
if File.exist?(cur_rates_filepath)
|
15
|
-
download_cur_rates(@cur_rates_url) unless parse_date(cur_rates_filepath) == Date.today
|
16
|
-
else
|
17
|
-
download_cur_rates(@cur_rates_url)
|
18
|
-
end
|
19
|
-
|
20
|
-
super(cur_rates_filepath)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/cnb/load_config.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
if File.exists?('config/cnb.yml')
|
4
|
-
filename = 'config/cnb.yml'
|
5
|
-
elsif File.exists?(ENV['HOME'] + '/.cnb.yml')
|
6
|
-
filename = ENV['HOME'] + '/.cnb.yml'
|
7
|
-
end
|
8
|
-
|
9
|
-
if filename
|
10
|
-
CONFIG = YAML.load_file(filename)
|
11
|
-
else
|
12
|
-
CONFIG = {}
|
13
|
-
end
|
14
|
-
|
15
|
-
CONFIG['exchange_rates_dir'] ||= '/tmp'
|
File without changes
|
File without changes
|