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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5900cf0a63e8bfba9da32eb6e36f70898cb0a4c2
4
- data.tar.gz: 099dad1d8d2db942144c5b11ffb20f416ead3e4e
3
+ metadata.gz: 74fa46a97bd5a68b12189edb0df0181100d04f1a
4
+ data.tar.gz: 17ee1ff9c96b3687285b34314e330b617e239b00
5
5
  SHA512:
6
- metadata.gz: b5f7595d50d3cc5834ed71b73b90a5d9f786fd5ba503e6b9e4e26ac29662542b26acabfe812bc4e15be660ba767d7a4c23a337e167735f9eed73efe27c3aa922
7
- data.tar.gz: 7c8e69002530cb07323d2b63ccae74940d39b22ab73586f079e9b51054ec0c823fb337a36708c7a72ccbd6cc2f182478fa1a230225751fd9d6898887b8f27b12
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 CurRatesParser
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(filepath)
30
- @date = parse_date(filepath)
31
- cur_rates = File.read(filepath)
32
- lines = cur_rates.lines.to_a
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(filepath)
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
@@ -0,0 +1,10 @@
1
+ module CNB
2
+ class DailyRates < CurrencyRates
3
+ def initialize
4
+ @currencies = {}
5
+ @cur_rates_url = "#{CUR_RATES_URL}kurzy_devizoveho_trhu/denni_kurz.txt"
6
+
7
+ parse
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  module CNB
2
- class MonthlyCurRatesParser < CurRatesParser
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
@@ -1,3 +1,3 @@
1
1
  module CNB
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.7'
3
3
  end
data/lib/cnb.rb CHANGED
@@ -1,18 +1,17 @@
1
- require_relative 'cnb/load_config'
2
- require_relative 'cnb/cur_rates_parser'
3
- require_relative 'cnb/daily_cur_rates_parser'
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 = DailyCurRatesParser.new
10
+ @parser = DailyRates.new
12
11
  end
13
12
 
14
13
  def monthly_rates(month, year)
15
- @parser = MonthlyCurRatesParser.new(month, year)
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::DailyCurRatesParser do
3
+ describe CNB::DailyRates do
4
4
  before(:each) do
5
- filepath = File.join('spec', 'examples', 'daily_cur_rates.txt')
6
- CNB::DailyCurRatesParser.any_instance.stub(:cur_rates_filepath) { filepath }
7
- @parser = CNB::DailyCurRatesParser.new
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::MonthlyCurRatesParser do
3
+ describe CNB::MonthlyRates do
4
4
  before(:each) do
5
- filepath = File.join('spec', 'examples', 'monthly_cur_rates_2013_3.txt')
6
- CNB::MonthlyCurRatesParser.any_instance.stub(:cur_rates_filepath) { filepath }
7
- @parser = CNB::MonthlyCurRatesParser.new(3, 2013)
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::MonthlyCurRatesParser.new('abc', 2013)}.to raise_error(StandardError)
12
- expect{CNB::MonthlyCurRatesParser.new(0, 2013)}.to raise_error(StandardError)
13
- expect{CNB::MonthlyCurRatesParser.new(13, 2013)}.to raise_error(StandardError)
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::MonthlyCurRatesParser.new(3, 'abc')}.to raise_error(StandardError)
18
- expect{CNB::MonthlyCurRatesParser.new(3, 2000)}.to raise_error(StandardError)
19
- expect{CNB::MonthlyCurRatesParser.new(3, 2050)}.to raise_error(StandardError)
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/load_config'
10
- require 'cnb/cur_rates_parser'
11
- require 'cnb/daily_cur_rates_parser'
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.5
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-25 00:00:00.000000000 Z
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/cur_rates_parser.rb
86
- - lib/cnb/daily_cur_rates_parser.rb
87
- - lib/cnb/load_config.rb
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/examples/daily_cur_rates.txt
91
- - spec/examples/monthly_cur_rates_2013_3.txt
92
- - spec/models/daily_cur_rates_parser_spec.rb
93
- - spec/models/monthly_cur_rates_parser_spec.rb
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/examples/daily_cur_rates.txt
122
- - spec/examples/monthly_cur_rates_2013_3.txt
123
- - spec/models/daily_cur_rates_parser_spec.rb
124
- - spec/models/monthly_cur_rates_parser_spec.rb
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
@@ -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
@@ -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