riksbank_currency 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +122 -0
- data/Rakefile +6 -0
- data/lib/riksbank_currency/business_day.rb +33 -0
- data/lib/riksbank_currency/fetcher/date_fetcher.rb +81 -0
- data/lib/riksbank_currency/fetcher/today_fetcher.rb +61 -0
- data/lib/riksbank_currency/fetcher.rb +60 -0
- data/lib/riksbank_currency/helper.rb +36 -0
- data/lib/riksbank_currency/rates.rb +51 -0
- data/lib/riksbank_currency/request.rb +24 -0
- data/lib/riksbank_currency/version.rb +3 -0
- data/lib/riksbank_currency.rb +26 -0
- data/riksbank_currency.gemspec +36 -0
- metadata +104 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: d9cb06cc185f232c081fab9056bf2332c9ded9fb
|
|
4
|
+
data.tar.gz: 36ae9d4767b38845cc27aa9e244892c64a47ceda
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b4fb912717f93c4176b426793ac97c9e4595b6d595f9d2ead159ca9efa91ade61fcbca2a2d6f293df78b0daf723f5df5528f57f7374f44760cf037be15748a53
|
|
7
|
+
data.tar.gz: 46aba20322e6226a4802f8c0026b6cfc8495b316b29fdc7095d5a0edd1d84bb42f1a8e23da7b5d79c6e82520d46804ff9b75e88fbfa4f075de2753ef226e8c3d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
riksbank_currency (0.1.0)
|
|
5
|
+
nokogiri (~> 1.6)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
diff-lcs (1.3)
|
|
11
|
+
mini_portile2 (2.3.0)
|
|
12
|
+
nokogiri (1.8.1)
|
|
13
|
+
mini_portile2 (~> 2.3.0)
|
|
14
|
+
rspec (3.7.0)
|
|
15
|
+
rspec-core (~> 3.7.0)
|
|
16
|
+
rspec-expectations (~> 3.7.0)
|
|
17
|
+
rspec-mocks (~> 3.7.0)
|
|
18
|
+
rspec-core (3.7.1)
|
|
19
|
+
rspec-support (~> 3.7.0)
|
|
20
|
+
rspec-expectations (3.7.0)
|
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
+
rspec-support (~> 3.7.0)
|
|
23
|
+
rspec-mocks (3.7.0)
|
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
25
|
+
rspec-support (~> 3.7.0)
|
|
26
|
+
rspec-support (3.7.0)
|
|
27
|
+
|
|
28
|
+
PLATFORMS
|
|
29
|
+
ruby
|
|
30
|
+
|
|
31
|
+
DEPENDENCIES
|
|
32
|
+
bundler (~> 1.16)
|
|
33
|
+
riksbank_currency!
|
|
34
|
+
rspec (~> 3.3)
|
|
35
|
+
|
|
36
|
+
BUNDLED WITH
|
|
37
|
+
1.16.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Andrey Yakovlev
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Riksbank Exchange Rates
|
|
2
|
+
|
|
3
|
+
Simple wrapper for Riksbank API that returns currency exchange rates for the specific date.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add this line to your application's Gemfile:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'riksbank_currency'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
And then execute:
|
|
15
|
+
|
|
16
|
+
$ bundle
|
|
17
|
+
|
|
18
|
+
Or install it yourself as:
|
|
19
|
+
|
|
20
|
+
$ gem install riksbank_currency
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Initialize
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
bank = RiksbankCurrency::Rates.new(date: Date.yesterday, base: 'USD')
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Initializer options:
|
|
31
|
+
|
|
32
|
+
* `date` - the day for which we want to get exchange rates. It should be `Date` object
|
|
33
|
+
* `base` - base currency (by default `SEK`)
|
|
34
|
+
* `rates` - you can pass your own hash with rates. Can be useful for caching purposes
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
### Retrieve a specific rate
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
# how many euros in one american dollar?
|
|
41
|
+
bank.rate('USD', 'EUR').to_f # => 0.80607
|
|
42
|
+
|
|
43
|
+
# by default it converts to SEK
|
|
44
|
+
bank.rate('USD').to_f # => 7.913
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Get all available currencies:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
bank.currencies # => ['AUD', 'BRL', 'CAD', 'CHF', 'CNY', 'CZK', 'DKK', 'EUR'...]
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Get all rates
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
bank.rates # => { 'NOK' => 105.556298, 'INR' => 13.619511, … }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Bank days and holidays
|
|
60
|
+
|
|
61
|
+
During the holidays or weekends bank doesn't change exchange rates. It means that
|
|
62
|
+
bank doesn't have exchange rates for Saturday or Christmas.
|
|
63
|
+
|
|
64
|
+
Also, another problem in the current day. Bank refreshes rates twice per day (morning and evening).
|
|
65
|
+
So, therefore at 3 o'clock of morning bank doensn't have fresh rates and we should use
|
|
66
|
+
rates from prevoius day.
|
|
67
|
+
|
|
68
|
+
This gem deals with all of this problems:
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
# 20.01.2018 is Saturday, bank doesn't work at this day
|
|
72
|
+
holiday_bank = RiksbankCurrency::Rates.new(date: Date.new(2018, 1, 20))
|
|
73
|
+
|
|
74
|
+
# rates will be got from Friday!
|
|
75
|
+
holiday_bank.rate_date # Fri, 19 Jan 2018
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# imagine that we do it at night
|
|
81
|
+
# Time.current => Fri, 26 Jan 2018 03:00:00 UTC +00:00
|
|
82
|
+
morning_bank = RiksbankCurrency::Rates.new
|
|
83
|
+
|
|
84
|
+
# it is too early, bank doensn't work at this time
|
|
85
|
+
# let's use yesterday rates
|
|
86
|
+
morning_bank.rate_date # Thu, 25 Jan 2018
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Available currencies
|
|
90
|
+
|
|
91
|
+
Pay attention to check available currencies at [official bank page](http://www.riksbank.se/en/Interest-and-exchange-rates/Series-for-web-services/).
|
|
92
|
+
|
|
93
|
+
By default this gem uses only existing currencies for 2018. For example, FIM (Finland Marka)
|
|
94
|
+
has rates only till 2002 and it doesn't exist anymore in 2018.
|
|
95
|
+
|
|
96
|
+
So, if you wanna get information about FIM exchange rate in 2001 you should
|
|
97
|
+
change the gem defaults:
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
RiksbankCurrency.currencies << 'FIM'
|
|
101
|
+
|
|
102
|
+
old_bank = RiksbankCurrency::Rates.new(date: Date.new(2001, 1, 16))
|
|
103
|
+
old_bank.rate('FIM').to_f # 1.495527
|
|
104
|
+
|
|
105
|
+
today_bank = RiksbankCurrency::Rates.new
|
|
106
|
+
today_bank.rate('FIM') # nil
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### Default currencies
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
%w(AUD BRL CAD CHF CNY CZK DKK EUR GBP HKD HUF IDR INR ISK JPY
|
|
113
|
+
KRW MAD MXN NOK NZD PLN RUB SAR SGD THB TRY USD ZAR)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Legal
|
|
117
|
+
|
|
118
|
+
The author of this gem is not affiliated with the Riksbank.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'nokogiri'
|
|
2
|
+
|
|
3
|
+
module RiksbankCurrency
|
|
4
|
+
module BusinessDay
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def get_latest(date)
|
|
8
|
+
template = <<-XML
|
|
9
|
+
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://swea.riksbank.se/xsd">
|
|
10
|
+
<soap:Header/>
|
|
11
|
+
<soap:Body>
|
|
12
|
+
<xsd:getCalendarDays>
|
|
13
|
+
<datefrom>#{Helper.format_date(date.prev_month)}</datefrom>
|
|
14
|
+
<dateto>#{Helper.format_date(date)}</dateto>
|
|
15
|
+
</xsd:getCalendarDays>
|
|
16
|
+
</soap:Body>
|
|
17
|
+
</soap:Envelope>
|
|
18
|
+
XML
|
|
19
|
+
|
|
20
|
+
date = nil
|
|
21
|
+
|
|
22
|
+
Request.call(template).xpath('//return').reverse_each do |block|
|
|
23
|
+
bankday = block.at_xpath('bankday').content == 'Y'
|
|
24
|
+
next unless bankday
|
|
25
|
+
|
|
26
|
+
date = Helper.parse_date(block.at_xpath('caldate').content)
|
|
27
|
+
break
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
date
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
module RiksbankCurrency
|
|
2
|
+
# Get historical rates by date.
|
|
3
|
+
#
|
|
4
|
+
# @see https://swea.riksbank.se/sweaWS/docs/api/call/getInterestAndExchangeRates.htm
|
|
5
|
+
#
|
|
6
|
+
# IMPORTANT! The date should be less than current date. To get rates for today
|
|
7
|
+
# see at TodayFetcher
|
|
8
|
+
class DateFetcher
|
|
9
|
+
def initialize(date)
|
|
10
|
+
@date = date
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def rate_date
|
|
14
|
+
@rate_date ||= BusinessDay.get_latest(@date)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_hash
|
|
18
|
+
rates = {}
|
|
19
|
+
|
|
20
|
+
response.xpath("//series").each do |series|
|
|
21
|
+
currency = Helper.currency_from_seriesid(series.at_xpath('seriesid').content)
|
|
22
|
+
|
|
23
|
+
if (rate = series.at_xpath('resultrows/value').content).length > 0
|
|
24
|
+
unit = BigDecimal(series.at_xpath('unit').content)
|
|
25
|
+
rate = BigDecimal(rate)
|
|
26
|
+
|
|
27
|
+
rates[currency] = rate / unit
|
|
28
|
+
else
|
|
29
|
+
next
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
rates
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def response
|
|
37
|
+
@response ||= Request.call(xml_template)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
protected
|
|
41
|
+
|
|
42
|
+
def groups
|
|
43
|
+
RiksbankCurrency.currencies.map do |currency|
|
|
44
|
+
group(currency)
|
|
45
|
+
end.join('')
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def group(currency)
|
|
49
|
+
<<-XML
|
|
50
|
+
<searchGroupSeries>
|
|
51
|
+
<groupid>130</groupid>
|
|
52
|
+
<seriesid>#{Helper.currency_to_seriesid(currency)}</seriesid>
|
|
53
|
+
</searchGroupSeries>
|
|
54
|
+
XML
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def xml_template
|
|
58
|
+
formatted_date = Helper.format_date(rate_date)
|
|
59
|
+
<<-XML
|
|
60
|
+
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://swea.riksbank.se/xsd">
|
|
61
|
+
<soap:Header/>
|
|
62
|
+
<soap:Body>
|
|
63
|
+
<xsd:getInterestAndExchangeRates>
|
|
64
|
+
<searchRequestParameters>
|
|
65
|
+
<aggregateMethod>D</aggregateMethod>
|
|
66
|
+
<avg>false</avg>
|
|
67
|
+
<datefrom>#{formatted_date}</datefrom>
|
|
68
|
+
<dateto>#{formatted_date}</dateto>
|
|
69
|
+
<languageid>en</languageid>
|
|
70
|
+
<max>true</max>
|
|
71
|
+
<min>false</min>
|
|
72
|
+
#{groups}
|
|
73
|
+
<ultimo>false</ultimo>
|
|
74
|
+
</searchRequestParameters>
|
|
75
|
+
</xsd:getInterestAndExchangeRates>
|
|
76
|
+
</soap:Body>
|
|
77
|
+
</soap:Envelope>
|
|
78
|
+
XML
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
module RiksbankCurrency
|
|
2
|
+
# Get the latest rates that will be correct for the current day.
|
|
3
|
+
#
|
|
4
|
+
# @see https://swea.riksbank.se/sweaWS/docs/api/call/getLatestInterestAndExchangeRates.htm
|
|
5
|
+
class TodayFetcher
|
|
6
|
+
def rate_date
|
|
7
|
+
@rate_date ||=
|
|
8
|
+
begin
|
|
9
|
+
response.xpath("//resultrows/date").map do |date_node|
|
|
10
|
+
Helper.parse_date(date_node.content)
|
|
11
|
+
end.max
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def to_hash
|
|
16
|
+
rates = {}
|
|
17
|
+
|
|
18
|
+
response.xpath("//series").each do |series|
|
|
19
|
+
currency = Helper.currency_from_seriesid(series.at_xpath('seriesid').content)
|
|
20
|
+
|
|
21
|
+
if (rate = series.at_xpath('resultrows/value').content).length > 0
|
|
22
|
+
unit = BigDecimal(series.at_xpath('unit').content)
|
|
23
|
+
rate = BigDecimal(rate)
|
|
24
|
+
|
|
25
|
+
rates[currency] = rate / unit
|
|
26
|
+
else
|
|
27
|
+
next
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
rates
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def response
|
|
35
|
+
@response ||= Request.call(xml_template)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
protected
|
|
39
|
+
|
|
40
|
+
def series
|
|
41
|
+
RiksbankCurrency.currencies.map do |currency|
|
|
42
|
+
id = Helper.currency_to_seriesid(currency)
|
|
43
|
+
"<seriesid>#{id}</seriesid>"
|
|
44
|
+
end.join('')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def xml_template
|
|
48
|
+
<<-XML
|
|
49
|
+
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://swea.riksbank.se/xsd">
|
|
50
|
+
<soap:Header/>
|
|
51
|
+
<soap:Body>
|
|
52
|
+
<xsd:getLatestInterestAndExchangeRates>
|
|
53
|
+
<languageid>en</languageid>
|
|
54
|
+
#{series}
|
|
55
|
+
</xsd:getLatestInterestAndExchangeRates>
|
|
56
|
+
</soap:Body>
|
|
57
|
+
</soap:Envelope>
|
|
58
|
+
XML
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require 'nokogiri'
|
|
2
|
+
require 'date'
|
|
3
|
+
|
|
4
|
+
module RiksbankCurrency
|
|
5
|
+
class Fetcher
|
|
6
|
+
# @param [Date] date
|
|
7
|
+
# @param [String] base currency
|
|
8
|
+
def initialize(date, base)
|
|
9
|
+
@date = date
|
|
10
|
+
@base = base.to_s
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Convert XML response to Hash and recalculate it by @base currency
|
|
14
|
+
# Example:
|
|
15
|
+
# {
|
|
16
|
+
# "EUR": 9.021,
|
|
17
|
+
# "USD": 7.65
|
|
18
|
+
# }
|
|
19
|
+
# @return [Hash]
|
|
20
|
+
def to_hash
|
|
21
|
+
rates = fetcher.to_hash
|
|
22
|
+
rates['SEK'] = 1.0
|
|
23
|
+
recombine_by_base(rates)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Define correct rate fetcher
|
|
27
|
+
# @return [TodayFetcher,DateFetcher]
|
|
28
|
+
def fetcher
|
|
29
|
+
@fetcher ||=
|
|
30
|
+
if @date == Date.today
|
|
31
|
+
TodayFetcher.new
|
|
32
|
+
else
|
|
33
|
+
DateFetcher.new(@date)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @return [Date]
|
|
38
|
+
def rate_date
|
|
39
|
+
fetcher.rate_date
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
protected
|
|
43
|
+
|
|
44
|
+
def recombine_by_base(rates)
|
|
45
|
+
if @base != 'SEK'
|
|
46
|
+
new_rates = {}
|
|
47
|
+
|
|
48
|
+
rates.each do |currency, value|
|
|
49
|
+
new_rates[currency] = rates[@base] / value
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
rates = new_rates
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
rates[@base] = 1.0
|
|
56
|
+
|
|
57
|
+
rates
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module RiksbankCurrency
|
|
2
|
+
module Helper
|
|
3
|
+
module_function
|
|
4
|
+
|
|
5
|
+
# Extract currency ISO name from Riksbank series name
|
|
6
|
+
# Example: SEKEURPMI => EUR
|
|
7
|
+
#
|
|
8
|
+
# @see http://www.riksbank.se/en/Interest-and-exchange-rates/Series-for-web-services/
|
|
9
|
+
#
|
|
10
|
+
# @param [String] Riksbank SeriesId
|
|
11
|
+
def currency_from_seriesid(seriesid)
|
|
12
|
+
unless (matches = seriesid.match(/SEK(\w+)PMI/)).nil?
|
|
13
|
+
matches[1]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Generate series name for specific currency
|
|
18
|
+
# @param [String] currency ISO name
|
|
19
|
+
def currency_to_seriesid(currency)
|
|
20
|
+
"SEK#{currency.upcase}PMI"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Converts date to the bank format
|
|
24
|
+
# @param [Date] date
|
|
25
|
+
def format_date(date)
|
|
26
|
+
date.strftime("%Y-%m-%d")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Parse date from bank format
|
|
30
|
+
# @param [String] date string in format "YYYY-MM-DD"
|
|
31
|
+
# @return [Date]
|
|
32
|
+
def parse_date(string_date)
|
|
33
|
+
Date.new(*string_date.split('-').map(&:to_i))
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
|
|
3
|
+
module RiksbankCurrency
|
|
4
|
+
class Rates
|
|
5
|
+
def initialize(**options)
|
|
6
|
+
@date = options[:date] || Date.today
|
|
7
|
+
@base = options[:base] || 'SEK'
|
|
8
|
+
|
|
9
|
+
# it is possible to pass cached rates to initializer to avoid extra http calls
|
|
10
|
+
unless options[:rates].nil?
|
|
11
|
+
@rates = options[:rates]
|
|
12
|
+
@rate_date = @date
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# @return [RiksbankCurreny::Fetcher]
|
|
17
|
+
def fetcher
|
|
18
|
+
@fetcher ||= Fetcher.new(@date, @base)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def rate(from, to = @base)
|
|
22
|
+
return nil if rates[from].nil? || rates[to].nil?
|
|
23
|
+
rates[from] / rates[to]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Hash with all available currencies
|
|
27
|
+
# {
|
|
28
|
+
# "EUR": 9.021,
|
|
29
|
+
# "USD": 7.211
|
|
30
|
+
# }
|
|
31
|
+
# @return [Hash]
|
|
32
|
+
def rates
|
|
33
|
+
@rates ||= fetcher.to_hash
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Sometimes date passed to the initializer differs from real exchange date.
|
|
37
|
+
# It causes from holidays, weekends and etc.
|
|
38
|
+
#
|
|
39
|
+
# For example, if we want to get rates for the `1st of January 2018` then rate date
|
|
40
|
+
# will be `27th of December 2017`
|
|
41
|
+
# @return [Date]
|
|
42
|
+
def rate_date
|
|
43
|
+
@rate_date ||= fetcher.rate_date
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @return [Array]
|
|
47
|
+
def currencies
|
|
48
|
+
rates.keys
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
require 'nokogiri'
|
|
4
|
+
|
|
5
|
+
module RiksbankCurrency
|
|
6
|
+
module Request
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
ENDPOINT = "http://swea.riksbank.se/sweaWS/services/SweaWebServiceHttpSoap12Endpoint"
|
|
10
|
+
|
|
11
|
+
# @param [String] xml_body
|
|
12
|
+
# @return [Nokogiri::XML::Document]
|
|
13
|
+
def call(xml_body)
|
|
14
|
+
uri = URI.parse(ENDPOINT)
|
|
15
|
+
|
|
16
|
+
request = Net::HTTP::Post.new(uri.path)
|
|
17
|
+
request.body = xml_body
|
|
18
|
+
request.content_type = 'text/xml'
|
|
19
|
+
|
|
20
|
+
response = Net::HTTP.new(uri.host, uri.port).start { |http| http.request request }
|
|
21
|
+
Nokogiri::XML(response.body)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'bigdecimal'
|
|
2
|
+
require 'bigdecimal/util'
|
|
3
|
+
require 'uri'
|
|
4
|
+
|
|
5
|
+
require 'riksbank_currency/helper'
|
|
6
|
+
require 'riksbank_currency/business_day'
|
|
7
|
+
require 'riksbank_currency/fetcher'
|
|
8
|
+
require 'riksbank_currency/fetcher/date_fetcher'
|
|
9
|
+
require 'riksbank_currency/fetcher/today_fetcher'
|
|
10
|
+
require 'riksbank_currency/rates'
|
|
11
|
+
require 'riksbank_currency/request'
|
|
12
|
+
require 'riksbank_currency/version'
|
|
13
|
+
|
|
14
|
+
module RiksbankCurrency
|
|
15
|
+
# list of available currencies in 2018
|
|
16
|
+
@@currencies = %w(AUD BRL CAD CHF CNY CZK DKK EUR GBP HKD HUF IDR INR ISK JPY
|
|
17
|
+
KRW MAD MXN NOK NZD PLN RUB SAR SGD THB TRY USD ZAR)
|
|
18
|
+
|
|
19
|
+
def self.currencies
|
|
20
|
+
@@currencies
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.currencies=(val)
|
|
24
|
+
@@currencies = val
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "riksbank_currency/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "riksbank_currency"
|
|
8
|
+
spec.version = RiksbankCurrency::VERSION
|
|
9
|
+
spec.authors = ["Andrey Yakovlev"]
|
|
10
|
+
spec.email = ["sigra.yandex@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = "Retrieves exchange rates from the Riksbank Sweden"
|
|
13
|
+
spec.homepage = "https://github.com/sigra/riksbank-currency"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
18
|
+
# if spec.respond_to?(:metadata)
|
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
|
20
|
+
# else
|
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
|
22
|
+
# "public gem pushes."
|
|
23
|
+
# end
|
|
24
|
+
|
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
|
27
|
+
end
|
|
28
|
+
spec.bindir = "exe"
|
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
30
|
+
spec.require_paths = ["lib"]
|
|
31
|
+
|
|
32
|
+
spec.add_runtime_dependency 'nokogiri', '~> 1.6'
|
|
33
|
+
|
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
|
36
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: riksbank_currency
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andrey Yakovlev
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-01-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: nokogiri
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.6'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.6'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.16'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.16'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.3'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.3'
|
|
55
|
+
description:
|
|
56
|
+
email:
|
|
57
|
+
- sigra.yandex@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- ".rspec"
|
|
64
|
+
- ".travis.yml"
|
|
65
|
+
- Gemfile
|
|
66
|
+
- Gemfile.lock
|
|
67
|
+
- LICENSE.txt
|
|
68
|
+
- README.md
|
|
69
|
+
- Rakefile
|
|
70
|
+
- lib/riksbank_currency.rb
|
|
71
|
+
- lib/riksbank_currency/business_day.rb
|
|
72
|
+
- lib/riksbank_currency/fetcher.rb
|
|
73
|
+
- lib/riksbank_currency/fetcher/date_fetcher.rb
|
|
74
|
+
- lib/riksbank_currency/fetcher/today_fetcher.rb
|
|
75
|
+
- lib/riksbank_currency/helper.rb
|
|
76
|
+
- lib/riksbank_currency/rates.rb
|
|
77
|
+
- lib/riksbank_currency/request.rb
|
|
78
|
+
- lib/riksbank_currency/version.rb
|
|
79
|
+
- riksbank_currency.gemspec
|
|
80
|
+
homepage: https://github.com/sigra/riksbank-currency
|
|
81
|
+
licenses:
|
|
82
|
+
- MIT
|
|
83
|
+
metadata: {}
|
|
84
|
+
post_install_message:
|
|
85
|
+
rdoc_options: []
|
|
86
|
+
require_paths:
|
|
87
|
+
- lib
|
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: '0'
|
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
requirements: []
|
|
99
|
+
rubyforge_project:
|
|
100
|
+
rubygems_version: 2.6.14
|
|
101
|
+
signing_key:
|
|
102
|
+
specification_version: 4
|
|
103
|
+
summary: Retrieves exchange rates from the Riksbank Sweden
|
|
104
|
+
test_files: []
|