daily_exchange_rates_bank 1.0.1 → 1.0.2
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
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 626e118d401aec1e2da2ed5dd5794aa593f5dbe044fc86e2b3782920f09e2b40
|
4
|
+
data.tar.gz: 8c69771e2be6fec05fc0911dfe904793c9af5f79322528d2a43c42ab0c556997
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba518774e92dab5e196ca0f0b68810f7a32487f598e71b799ce83511480c59f29a387574ec2b679ae3ec4679063f7f259112aad4861b94f7d3d238c1d3b3d039
|
7
|
+
data.tar.gz: 9a8e350d33fd688c416ecd29a74de3f7c2bbb8d0603fd54ecec5b9e52768ccf67b2278cef9ca6f9f425effe0169b4dd0fe87f93a6f2b4357cd3101378fb0f40f
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# daily_exchange_rates_bank changelog
|
2
2
|
|
3
|
-
## 1.0.
|
3
|
+
## 1.0.2 (22.04.2021)
|
4
|
+
|
5
|
+
* Switch to api.frankfurter.app to fetch historic exchange rates
|
6
|
+
|
7
|
+
## 1.0.1 (09.02.2021)
|
8
|
+
|
9
|
+
* Loosen dependency on money gem version
|
10
|
+
|
11
|
+
## 1.0.0 (20.09.2019)
|
4
12
|
|
5
13
|
* Initial gem release
|
data/README.md
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
[](https://app.codeship.com/projects/366592)
|
4
4
|
|
5
5
|
A bank for the money gem that determines exchange rates for any desired date.
|
6
|
-
Missing exchange rates are fetched from
|
6
|
+
Missing exchange rates are fetched from api.frankfurter.app. This is
|
7
|
+
configurable with the `RATES_API_URL` environment variable.
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'money'
|
4
|
+
require 'money/rates_store/store_with_date_support'
|
5
|
+
require 'daily_exchange_rates_bank/exchange_rates_api_client'
|
6
|
+
|
7
|
+
# Class for aiding in exchanging money between different currencies.
|
8
|
+
class DailyExchangeRatesBank < Money::Bank::VariableExchange
|
9
|
+
SERIALIZER_DATE_SEPARATOR = '_ON_'
|
10
|
+
|
11
|
+
def initialize(store = Money::RatesStore::StoreWithDateSupport.new, &block)
|
12
|
+
super(store, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_rate(from, to, date = nil)
|
16
|
+
store.get_rate(::Money::Currency.wrap(from).iso_code,
|
17
|
+
::Money::Currency.wrap(to).iso_code,
|
18
|
+
date)
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_rate(from, to, rate, date = nil)
|
22
|
+
store.add_rate(::Money::Currency.wrap(from).iso_code,
|
23
|
+
::Money::Currency.wrap(to).iso_code,
|
24
|
+
rate,
|
25
|
+
date)
|
26
|
+
end
|
27
|
+
|
28
|
+
def rates
|
29
|
+
store.each_rate.each_with_object({}) do |(from, to, rate, date), hash|
|
30
|
+
key = [from, to].join(SERIALIZER_SEPARATOR)
|
31
|
+
key = [key, date.to_s].join(SERIALIZER_DATE_SEPARATOR) if date
|
32
|
+
hash[key] = rate
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def exchange(cents, from_currency, to_currency, date = nil)
|
37
|
+
exchange_with(Money.new(cents, from_currency), to_currency, date)
|
38
|
+
end
|
39
|
+
|
40
|
+
def exchange_with(from, to_currency, date = nil)
|
41
|
+
to_currency = ::Money::Currency.wrap(to_currency)
|
42
|
+
return from if from.currency == to_currency
|
43
|
+
|
44
|
+
rate = get_rate(from.currency, to_currency, date)
|
45
|
+
|
46
|
+
rate ||= rate_from_exchange_rates_api(from.currency, to_currency, date)
|
47
|
+
|
48
|
+
fractional = calculate_fractional(from, to_currency, rate)
|
49
|
+
Money.new(fractional, to_currency)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def rate_from_exchange_rates_api(from_currency, to_currency, date)
|
55
|
+
puts from_currency
|
56
|
+
puts to_currency
|
57
|
+
puts date
|
58
|
+
api_client = DailyExchangeRatesBank::ExchangeRatesApiClient.new
|
59
|
+
rates = api_client.exchange_rates(from: from_currency.iso_code,
|
60
|
+
to: [to_currency.iso_code],
|
61
|
+
date: date)
|
62
|
+
rate = rates[to_currency.iso_code]
|
63
|
+
|
64
|
+
set_rate(from_currency, to_currency, rate, date)
|
65
|
+
|
66
|
+
rate
|
67
|
+
end
|
68
|
+
|
69
|
+
def calculate_fractional(from, to_currency, rate)
|
70
|
+
BigDecimal(rate.to_s) * BigDecimal(from.fractional.to_s) / (
|
71
|
+
BigDecimal(from.currency.subunit_to_unit.to_s) /
|
72
|
+
BigDecimal(to_currency.subunit_to_unit.to_s)
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
@@ -3,10 +3,11 @@
|
|
3
3
|
require 'open-uri'
|
4
4
|
|
5
5
|
class DailyExchangeRatesBank < Money::Bank::VariableExchange
|
6
|
-
# Access
|
6
|
+
# Access api.frankfurter.app to fetch historic exchange rates
|
7
7
|
class ExchangeRatesApiClient
|
8
8
|
def exchange_rates(from: 'EUR', to: %w[USD GBP CHF], date: Date.today)
|
9
|
-
|
9
|
+
api_url = ENV.fetch('RATES_API_URL', 'https://api.frankfurter.app/')
|
10
|
+
uri = URI.parse(api_url)
|
10
11
|
uri.path = "/#{date}/"
|
11
12
|
uri.query = "base=#{from}&symbols=#{to.join(',')}"
|
12
13
|
json_response = uri.read
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
class DailyExchangeRatesBank < Money::Bank::VariableExchange
|
6
|
+
# Access api.frankfurter.app to fetch historic exchange rates
|
7
|
+
class ExchangeRatesApiClient
|
8
|
+
def exchange_rates(from: 'EUR', to: %w[USD GBP CHF], date: Date.today)
|
9
|
+
uri = URI.parse('https://api.frankfurter.app/')
|
10
|
+
uri.path = "/#{date}/"
|
11
|
+
uri.query = "base=#{from}&symbols=#{to.join(',')}"
|
12
|
+
json_response = uri.read
|
13
|
+
JSON.parse(json_response)['rates']
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: daily_exchange_rates_bank
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Aschenbrenner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: money
|
@@ -101,7 +101,7 @@ dependencies:
|
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '3.7'
|
103
103
|
description: This gem supports money conversions with historic exchange rates. Missing
|
104
|
-
exchange rates are fetched from
|
104
|
+
exchange rates are fetched from api.frankfurter.app
|
105
105
|
email:
|
106
106
|
executables: []
|
107
107
|
extensions: []
|
@@ -111,7 +111,9 @@ files:
|
|
111
111
|
- LICENSE
|
112
112
|
- README.md
|
113
113
|
- lib/daily_exchange_rates_bank.rb
|
114
|
+
- lib/daily_exchange_rates_bank.rb~
|
114
115
|
- lib/daily_exchange_rates_bank/exchange_rates_api_client.rb
|
116
|
+
- lib/daily_exchange_rates_bank/exchange_rates_api_client.rb~
|
115
117
|
- lib/money/rates_store/store_with_date_support.rb
|
116
118
|
homepage: https://github.com/gapfish/daily_exchange_rates_bank
|
117
119
|
licenses:
|
@@ -132,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
134
|
- !ruby/object:Gem::Version
|
133
135
|
version: '0'
|
134
136
|
requirements: []
|
135
|
-
rubygems_version: 3.
|
137
|
+
rubygems_version: 3.2.3
|
136
138
|
signing_key:
|
137
139
|
specification_version: 4
|
138
140
|
summary: A bank for the money gem that determines exchange rates for any desired date.
|