czech_exchange_rates 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/lib/czech_exchange_rates.rb +41 -0
- data/spec/czech_exchange_rates_spec.rb +27 -0
- data/spec/spec_helper.rb +5 -0
- metadata +49 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding : utf-8
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
class CzechExchangeRates
|
7
|
+
|
8
|
+
attr_reader :date, :currency, :exchange_rate
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@exchange_rate = 0
|
12
|
+
end
|
13
|
+
|
14
|
+
PAGE_URI = 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt?date='
|
15
|
+
|
16
|
+
def get_exchange_rate(currency, date = Date.new)
|
17
|
+
table = get_table(date)
|
18
|
+
|
19
|
+
first_line = table.split("\n").first
|
20
|
+
|
21
|
+
table.each_line do |line|
|
22
|
+
if line.match(currency)
|
23
|
+
l = line.split('|')
|
24
|
+
|
25
|
+
@exchange_rate = l[4].strip.gsub(',','.').to_f
|
26
|
+
@currency = l[3]
|
27
|
+
@date = Date.parse(first_line)
|
28
|
+
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_table(date = Date.new)
|
35
|
+
uri = "#{PAGE_URI}#{date.day}.#{date.month}.#{date.year}"
|
36
|
+
response = Net::HTTP.get_response(URI.parse(uri))
|
37
|
+
|
38
|
+
return response.body
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding : utf-8
|
2
|
+
|
3
|
+
require_relative 'spec_helper'
|
4
|
+
|
5
|
+
require 'date'
|
6
|
+
|
7
|
+
describe CzechExchangeRates do
|
8
|
+
|
9
|
+
context 'returns course for currency and date' do
|
10
|
+
|
11
|
+
it 'gets exchange rate table' do
|
12
|
+
subject.get_table.should be_instance_of String
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'finds exchange rate for currency' do
|
16
|
+
date = Date.parse('9.10.2012')
|
17
|
+
subject.get_exchange_rate('EUR', date)
|
18
|
+
|
19
|
+
subject.date.should == date
|
20
|
+
subject.currency.should == 'EUR'
|
21
|
+
subject.exchange_rate.should be_instance_of Float
|
22
|
+
subject.exchange_rate.should > 0
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: czech_exchange_rates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Tomeček
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-09 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple gem for grabbing course from Czech National Bank course table
|
15
|
+
email: adam.tomecek@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/czech_exchange_rates.rb
|
21
|
+
- spec/spec_helper.rb
|
22
|
+
- spec/czech_exchange_rates_spec.rb
|
23
|
+
homepage: https://github.com/adamtomecek/Czech-Exchange-Rates-gem
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.15
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: CNB exchange rates grabber
|
47
|
+
test_files:
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
- spec/czech_exchange_rates_spec.rb
|