ecb_rates 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/LICENSE +22 -0
- data/README.md +19 -0
- data/Rakefile +6 -0
- data/ecb_rates.gemspec +18 -0
- data/lib/ecb_rates.rb +61 -0
- data/spec/lib/ecb_rates_spec.rb +23 -0
- data/spec/spec_helper.rb +1 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1e583aa0c779c49b4207db206b52f990150a2291
|
4
|
+
data.tar.gz: 04dcce916bdd3f94e225dcbddb52f9a03305f486
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f5c87bb9f92206c62381a9159e8cecc24379e6a0e442bd3128c3890ad469e4041ed8533b044cd195a4dffc9d856fe2115358e55f67e1bba82f6185e3c859a54
|
7
|
+
data.tar.gz: 2ceba3d60982bcf46bb19ac75dcc308103359b8493acfe483a123059323a3f5dd3fba1b6e515d9d933d33f4e3891300ce2e5fcd73e632eaaf937421a571d5205
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Artem
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# ecb_rates
|
2
|
+
|
3
|
+
Rates gem for CNB
|
4
|
+
|
5
|
+
## INSTALL
|
6
|
+
|
7
|
+
```
|
8
|
+
$ gem install ecb_rates
|
9
|
+
```
|
10
|
+
## USAGE EXAMPLE
|
11
|
+
|
12
|
+
|
13
|
+
```
|
14
|
+
# date should be in format '1.1.2015'
|
15
|
+
ECBRates.rates_for 'currency_name', 'date'
|
16
|
+
```
|
17
|
+
As improvements for future we could add a migration generator,
|
18
|
+
witch add a xml response to preferences table and updates every day
|
19
|
+
at 3PM to get a better performance.
|
data/Rakefile
ADDED
data/ecb_rates.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "ecb_rates"
|
3
|
+
spec.version = "0.0.1"
|
4
|
+
spec.authors = ["Artsiom Shut"]
|
5
|
+
spec.email = ["artemshut@gmail.com"]
|
6
|
+
spec.description = %q{ECB rates for UOL.}
|
7
|
+
spec.summary = %q{ECB rates for UOL.}
|
8
|
+
spec.homepage = "https://github.com/artemshut/ecb_rates"
|
9
|
+
spec.license = "MIT"
|
10
|
+
|
11
|
+
spec.files = `git ls-files`.split("\n")
|
12
|
+
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
+
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
+
spec.require_paths = ["lib"]
|
15
|
+
|
16
|
+
spec.add_development_dependency 'rake', '~> 0'
|
17
|
+
spec.add_development_dependency 'rspec', '~> 0'
|
18
|
+
end
|
data/lib/ecb_rates.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'active_support/core_ext/hash'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
class ECBRates
|
7
|
+
|
8
|
+
# URLs for daily/90 days/from 1999 response after calling ECB
|
9
|
+
ECB_URL_DAILY = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
|
10
|
+
ECB_URL_90 = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'
|
11
|
+
ECB_URL_FROM_BEGINNING = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml'
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
# Example:
|
16
|
+
# >> ECBRates.rates_for 'usd', 14.09.2015
|
17
|
+
# => 1.2345
|
18
|
+
#
|
19
|
+
# Arguments:
|
20
|
+
# currency: (String)
|
21
|
+
# date: (String)
|
22
|
+
def rates_for currency, date = Date.today
|
23
|
+
converted_date = convert date
|
24
|
+
if converted_date > Date.today.to_s
|
25
|
+
puts "######## The date should be in the past. ########"
|
26
|
+
else
|
27
|
+
get_rate_for currency.upcase, converted_date
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Parse xml response from ECB
|
32
|
+
def get_rate_for currency, converted_date
|
33
|
+
rates_for_period = Hash.from_xml(get_rates_from_ecb)
|
34
|
+
rates_for_chosed_date = rates_for_period["Envelope"]["Cube"]["Cube"].detect { |h| h["time"] == converted_date }
|
35
|
+
if rates_for_chosed_date
|
36
|
+
rate_with_currency = rates_for_chosed_date["Cube"].detect { |h| h["currency"] == currency }
|
37
|
+
if rate_with_currency && rate_with_currency["rate"]
|
38
|
+
rate_with_currency["rate"].to_f
|
39
|
+
else
|
40
|
+
puts "######## Service have no such currency. Check it please. ########"
|
41
|
+
end
|
42
|
+
else
|
43
|
+
puts "######## No rate for this date. May be it was a weekend? ########"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return xml with response from ECB
|
48
|
+
def get_rates_from_ecb
|
49
|
+
uri = URI.parse ECB_URL_90
|
50
|
+
http = Net::HTTP.new uri.host, uri.port
|
51
|
+
request = Net::HTTP::Post.new uri.path
|
52
|
+
response = http.request request
|
53
|
+
response.body
|
54
|
+
end
|
55
|
+
|
56
|
+
# Convert date from parameters into a version, acceptable by ECB service
|
57
|
+
def convert date; date.is_a?(String) ? Date.parse(date).to_s : date.to_s end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ECBRates do
|
4
|
+
it "should return currency without date parameter for today" do
|
5
|
+
rate_for_usd = ECBRates.rates_for 'usd'
|
6
|
+
expect(rate_for_usd).not_to be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return weekend message" do
|
10
|
+
rate_for_usd = ECBRates.rates_for 'usd', '12.09.2015'
|
11
|
+
expect(rate_for_usd).to be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return incorrect currency message" do
|
15
|
+
rate_for_usd = ECBRates.rates_for 'xxx', '14.09.2015'
|
16
|
+
expect(rate_for_usd).to be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return incorrect date message" do
|
20
|
+
rate_for_usd = ECBRates.rates_for 'usd', Date.today + 20.years
|
21
|
+
expect(rate_for_usd).to be_nil
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ecb_rates'
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ecb_rates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artsiom Shut
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: ECB rates for UOL.
|
42
|
+
email:
|
43
|
+
- artemshut@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- ecb_rates.gemspec
|
53
|
+
- lib/ecb_rates.rb
|
54
|
+
- spec/lib/ecb_rates_spec.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
homepage: https://github.com/artemshut/ecb_rates
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.4.3
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: ECB rates for UOL.
|
80
|
+
test_files: []
|