cnb_rates 0.0.4
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 +7 -0
- data/lib/cnb_rates.rb +77 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 29b58386440357103d5cc9bd78b6da3615af6550
|
4
|
+
data.tar.gz: 8747b30c4b2709d3ed2c01215de2b60b3233d4d5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65b42fac2eb3168947dbc17edccc11e262db2502e08ce11683a6ac1f9ed6a6d1494b301e88162255862edefd300e6a562466067ea84abc2ab5be87116edc96d1
|
7
|
+
data.tar.gz: 2614511883d75d12749b783b08c90ae399e763decd46f1701f68ffc102eb32e6950373ac7f30f7c5fbc79159c9bbf00b94243f682532678dad1781c627ae598a
|
data/lib/cnb_rates.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Jaromír Červenka
|
2
|
+
# jaromir@virt.io
|
3
|
+
# https://virt.io/
|
4
|
+
# License: http://creativecommons.org/licenses/by/4.0/
|
5
|
+
|
6
|
+
require 'open-uri'
|
7
|
+
|
8
|
+
class CNBRates
|
9
|
+
SOURCE_URL = 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt?date='
|
10
|
+
|
11
|
+
attr_reader :currency_list
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
@date = options[:date]|| Date.today
|
15
|
+
@filename = options[:filename] || ''
|
16
|
+
@date = @date.strftime('%d.%m.%Y')
|
17
|
+
@currency_list = []
|
18
|
+
|
19
|
+
parse_list
|
20
|
+
end
|
21
|
+
|
22
|
+
def rate(currency, amount=1)
|
23
|
+
method_name = currency.downcase.to_sym
|
24
|
+
rate = respond_to?(method_name) ? send(method_name) : 1.0
|
25
|
+
rate * amount
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def parse_list
|
31
|
+
lines = @filename.empty? ? list_from_web : list_from_file
|
32
|
+
|
33
|
+
lines.each_line do |line|
|
34
|
+
split_line = line.split('|')
|
35
|
+
volume = split_line[2].to_f
|
36
|
+
currency_code = split_line[3].downcase
|
37
|
+
currency_rate = split_line[4].gsub(',', '.').to_f
|
38
|
+
currency_rate = volume == 1 ? currency_rate : currency_rate / volume
|
39
|
+
|
40
|
+
self.class.send(:define_method, "#{currency_code}") do
|
41
|
+
currency_rate
|
42
|
+
end
|
43
|
+
|
44
|
+
@currency_list.push(currency_code.to_sym)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def list_from_file
|
49
|
+
lines = ''
|
50
|
+
|
51
|
+
File.open @filename, 'r' do |file|
|
52
|
+
file.each_with_index do |line, index|
|
53
|
+
next if index == 0 || index == 0
|
54
|
+
lines += line
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
return lines
|
59
|
+
end
|
60
|
+
|
61
|
+
def list_from_web
|
62
|
+
lines = ''
|
63
|
+
|
64
|
+
open(source_url) do |doc|
|
65
|
+
doc.each_with_index do |line, index|
|
66
|
+
next if index == 0 || index == 1
|
67
|
+
lines += line
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
return lines
|
72
|
+
end
|
73
|
+
|
74
|
+
def source_url
|
75
|
+
"#{SOURCE_URL}#{@date}"
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cnb_rates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jaromír Červenka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Simple ruby parser for the Czech National Bank exchange rates
|
14
|
+
email: jaromir@virt.io
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/cnb_rates.rb
|
20
|
+
homepage: https://github.com/virtio/cnb_rates
|
21
|
+
licenses:
|
22
|
+
- CC BY
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.1.11
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Simple ruby parser for the Czech National Bank exchange rates
|
44
|
+
test_files: []
|