daily_exchange_rates_bank 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3fbfb1ca303a121bcb22b5a7d1c7302d65ad17ea36fc44660928cee4ddda9ae
|
4
|
+
data.tar.gz: 88f957f73a6ab21624a82ec9457b0bb18131f39fbe506158593fac7ec6aee680
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8846f5ce45adf7158b54d02e84af708a0e569085e29c15e80bd4064166f22d62feff17a63005ea3ed13a9469bb64372fa06e2b1dafab00cb6451303210f6d2c
|
7
|
+
data.tar.gz: b3163e9a220b8ae7a232e47462238a57cdb3c1fce5af2d5f4b44f3cd878e9fa581bc6c880d44085d2a86ae23dc4cf1e7e1dfa75198265a26f9213885476131bb
|
@@ -9,14 +9,14 @@ class Money
|
|
9
9
|
INDEX_DATE_SEPARATOR = '_ON_'
|
10
10
|
|
11
11
|
def add_rate(currency_iso_from, currency_iso_to, rate, date = nil)
|
12
|
-
|
13
|
-
|
12
|
+
guard.synchronize do
|
13
|
+
rates[rate_key_for(currency_iso_from, currency_iso_to, date)] = rate
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
def get_rate(currency_iso_from, currency_iso_to, date = nil)
|
18
|
-
|
19
|
-
|
18
|
+
guard.synchronize do
|
19
|
+
rates[rate_key_for(currency_iso_from, currency_iso_to, date)]
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -32,17 +32,17 @@ class Money
|
|
32
32
|
# store.each_rate do |iso_from, iso_to, rate, date|
|
33
33
|
# puts [iso_from, iso_to, rate, date].join
|
34
34
|
# end
|
35
|
-
def each_rate(&
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
def each_rate(&_block)
|
36
|
+
return to_enum(:each_rate) unless block_given?
|
37
|
+
|
38
|
+
guard.synchronize do
|
39
|
+
rates.each do |key, rate|
|
40
|
+
iso_from, iso_to = key.split(INDEX_KEY_SEPARATOR)
|
39
41
|
iso_to, date = iso_to.split(INDEX_DATE_SEPARATOR)
|
40
42
|
date = Date.parse(date) if date
|
41
|
-
|
43
|
+
yield iso_from, iso_to, rate, date
|
42
44
|
end
|
43
45
|
end
|
44
|
-
|
45
|
-
block_given? ? enum.each(&block) : enum
|
46
46
|
end
|
47
47
|
|
48
48
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Aschenbrenner
|
@@ -16,14 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '6.
|
19
|
+
version: '6.14'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 6.14.0
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '6.
|
29
|
+
version: '6.14'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 6.14.0
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: pry-byebug
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -105,9 +111,7 @@ files:
|
|
105
111
|
- LICENSE
|
106
112
|
- README.md
|
107
113
|
- lib/daily_exchange_rates_bank.rb
|
108
|
-
- lib/daily_exchange_rates_bank.rb~
|
109
114
|
- lib/daily_exchange_rates_bank/exchange_rates_api_client.rb
|
110
|
-
- lib/daily_exchange_rates_bank/exchange_rates_api_client.rb~
|
111
115
|
- lib/money/rates_store/store_with_date_support.rb
|
112
116
|
homepage: https://github.com/gapfish/daily_exchange_rates_bank
|
113
117
|
licenses:
|
@@ -128,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
132
|
- !ruby/object:Gem::Version
|
129
133
|
version: '0'
|
130
134
|
requirements: []
|
131
|
-
rubygems_version: 3.
|
135
|
+
rubygems_version: 3.1.4
|
132
136
|
signing_key:
|
133
137
|
specification_version: 4
|
134
138
|
summary: A bank for the money gem that determines exchange rates for any desired date.
|
@@ -1,75 +0,0 @@
|
|
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
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'open-uri'
|
4
|
-
|
5
|
-
class DailyExchangeRatesBank < Money::Bank::VariableExchange
|
6
|
-
# Access exchangeratesapi.io 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.exchangeratesapi.io/')
|
10
|
-
uri.path = "/#{date}/"
|
11
|
-
uri.query = "base=#{from}&symbols=#{to.join(',')}"
|
12
|
-
puts uri
|
13
|
-
json_response = uri.read
|
14
|
-
JSON.parse(json_response)['rates']
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|