omni_exchange 1.5.0 → 1.7.0

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: c591dfb40110a465f93f0c714d6f26369ba9717592a8deb4da4f92e044a4f660
4
- data.tar.gz: 91f086e1912b69998b55d6e76030dfdc8738388071c0405084fc6d8e8fa6174a
3
+ metadata.gz: a9e96486f0fec5e014b79827ca801fc70c717ad3f3c36a333fc2d1248ea7b3f6
4
+ data.tar.gz: f52a460e18ad19a5cfeefe507dbce77563faf8e81ad5c2b23701fa7fec058190
5
5
  SHA512:
6
- metadata.gz: 813ec7f4ce3f0d18360a39cd864c39fcb62bb1dbacc36a4b089b7d970d32b9bfa6c334fd6d11921f99dcc0d4b48ec7e5a45fd169b11ae6e852969e0f6d768ee3
7
- data.tar.gz: 75904791e0aaff4d160b5d11424c50ddfbd32450a5e0d52da2e4a246e5dde5aba81efe23f1bb9ed0ab656d4399eb3d48106f68c790c660fa50d76258265e3d55
6
+ metadata.gz: e948110df897320119664e7dadd069a9b32a74c9f683f21d9af48c4166eda64dfd6494fee2719c11c670088b91276de8f8659e7248aa324297219981ae18865f
7
+ data.tar.gz: 218e427ae203f24f0eeb4e74235b63fadaf47638061d6735f52d854b9413d5bfbcb08d5e541365cfcdeb9e4dfc481b2aedba145f6bfcf1f69b4a880a0e1451e9
data/.gitignore CHANGED
@@ -14,11 +14,4 @@
14
14
  .env
15
15
 
16
16
  sandbox.rb
17
- omni_exchange-0.1.0.gem
18
- omni_exchange-0.2.0.gem
19
- omni_exchange-1.1.0.gem
20
- omni_exchange-1.1.1.gem
21
- omni_exchange-1.2.0.gem
22
- omni_exchange-1.3.0.gem
23
- omni_exchange-1.4.0.gem
24
- omni_exchange-1.5.0.gem
17
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- omni_exchange (1.4.0)
4
+ omni_exchange (1.7.0)
5
5
  faraday (< 2)
6
6
  money (~> 6.13.1)
7
7
 
@@ -13,7 +13,7 @@ GEM
13
13
  concurrent-ruby (1.1.10)
14
14
  diff-lcs (1.5.0)
15
15
  dotenv (2.8.1)
16
- faraday (1.10.1)
16
+ faraday (1.10.3)
17
17
  faraday-em_http (~> 1.0)
18
18
  faraday-em_synchrony (~> 1.0)
19
19
  faraday-excon (~> 1.1)
@@ -41,7 +41,7 @@ GEM
41
41
  method_source (1.0.0)
42
42
  money (6.13.8)
43
43
  i18n (>= 0.6.4, <= 2)
44
- multipart-post (2.2.3)
44
+ multipart-post (2.3.0)
45
45
  parallel (1.22.1)
46
46
  parser (3.1.2.1)
47
47
  ast (~> 2.4.1)
@@ -49,7 +49,7 @@ GEM
49
49
  coderay (~> 1.1)
50
50
  method_source (~> 1.0)
51
51
  rainbow (3.1.1)
52
- rake (10.5.0)
52
+ rake (12.3.3)
53
53
  regexp_parser (2.5.0)
54
54
  rexml (3.2.5)
55
55
  rspec (3.11.0)
@@ -89,7 +89,7 @@ DEPENDENCIES
89
89
  dotenv
90
90
  omni_exchange!
91
91
  pry
92
- rake (~> 10.0)
92
+ rake (~> 12.3)
93
93
  rspec (~> 3.0)
94
94
  rubocop (~> 0.80)
95
95
  vcr
data/README.md CHANGED
@@ -57,7 +57,8 @@ To convert currency and/or get an exchange rate, all you have to do is call `Omn
57
57
  What you get back is a hash containing:
58
58
  1. converted_amount: (BigDecimal) the amount of money exchanged from the base currency to the target currency
59
59
  2. exchange_rate: (BigDecimal) the rate used to calculate the converted_amount
60
- 3. provider: (Symbol) the provider that supplied the exchange_rate (ie. :open_exchange_rates, :xe)
60
+ 3. non_subunit_fx_rate: (BigDecimal) an exchange rate that can be used to convert a formatted version of the base currency (ie. 100 USD being formatted as 1.00)
61
+ 4. provider: (Symbol) the provider that supplied the exchange_rate (ie. :open_exchange_rates, :xe)
61
62
 
62
63
  [For the sake of precise calculation](https://www.bigbinary.com/blog/handling-money-in-ruby), converted_amount and exchange_rate are BigDecimal. Simply call `.to_f` to the results if you'd like to see a number that is easier to read.
63
64
 
@@ -67,7 +68,7 @@ Here is an example. Lets say I want to convert $1.00 US Dollar to Japanese Yen,
67
68
  ```ruby
68
69
  USD_to_JPY = OmniExchange.get_fx_data(amount: 100, base_currency: 'USD', target_currency: 'JPY', providers: [:open_exchange_rates, :xe])
69
70
 
70
- puts USD_to_JPY # => { :converted_amount=>0.13566633333e3, :exchange_rate=>0.13566633333e1, :provider=>:open_exchange_rates }
71
+ puts USD_to_JPY # => { :converted_amount=>0.13566633333e3, :exchange_rate=>0.13566633333e1, :non_subunit_fx_rate=>0.13566633333e3, :provider=>:open_exchange_rates }
71
72
 
72
73
  puts USD_to_JPY[:converted_amount] # => 0.13566633333e3
73
74
  puts USD_to_JPY[:converted_amount].to_f # => 135.66633333
@@ -75,6 +76,10 @@ puts USD_to_JPY[:converted_amount].to_f # => 135.66633333
75
76
  puts USD_to_JPY[:exchange_rate] # => 0.13566633333e1
76
77
  puts USD_to_JPY[:exchange_rate].to_f # => 1.3566633333
77
78
 
79
+ # :fx_rate can be used when 100 USD is written as 1.00 instead of 100. In other words, you can do 1.00 * USD_to_JPY[:non_subunit_fx_rate] and get 135.66633333
80
+ puts USD_to_JPY[:non_subunit_fx_rate] # => 0.13566633333e3
81
+ puts USD_to_JPY[:non_subunit_fx_rate].to_f # => 135.66633333
82
+
78
83
  ```
79
84
 
80
85
  ## Development
@@ -36,7 +36,7 @@ module OmniExchange
36
36
 
37
37
  # Each provider class should inherit from Provider and have a .get_exchange_rates method. If a provider class
38
38
  # doesn't have a .get_exchange_rates method, the method below will be called and an error will be raised.
39
- def self.get_exchange_rate
39
+ def self.get_exchange_rate(base_currency:, target_currency:) # rubocop:disable Lint/UnusedMethodArgument
40
40
  raise 'method not implemented...'
41
41
  end
42
42
 
@@ -4,42 +4,67 @@ require 'omni_exchange'
4
4
 
5
5
  module OmniExchange
6
6
  class OpenExchangeRates < Provider
7
- ENDPOINT_URL = 'https://openexchangerates.org/api/latest.json'
8
-
9
- # This method returns the exchange rate, the rate at which the smallest unit of one currency (the base currency)
10
- # will be exchanged for another currency (the target currency), from Open Exchange Rate's API.
11
- # This method is called in the OmniExchange.exchange_currency method.
12
- #
13
- # @param base_currency: [String] the ISO Currency Code of the currency that you're exchanging from. ie. "USD", "JPY"
14
- # @param target_currency: [String] the ISO Currency Code of the currency that you're exchanging to. ie. "EUR", "KRW"
15
- # @ return [BigDecimal] an exchange rate is returned as a BigDecimal for precise calculation since this exchange
16
- # rate will be used to calculate an convert an exchange of currencies. However, an exception will be raised
17
- # if there is a timeout while connecting to xe.com or a timeout while reading Open Exchange Rate's API.
18
- def self.get_exchange_rate(base_currency:, target_currency:)
19
- config = OmniExchange.configuration.provider_config[:open_exchange_rates]
20
- app_id = config[:app_id]
21
-
22
- api = Faraday.new(OmniExchange::OpenExchangeRates::ENDPOINT_URL)
23
-
24
- begin
25
- response = api.get do |req|
26
- req.url "?app_id=#{app_id}&base=#{base_currency}"
27
- req.options.timeout = config[:read_timeout] || OmniExchange::Configuration::DEFAULT_READ_TIMEOUT
28
- req.options.open_timeout = config[:connect_timeout] || OmniExchange::Configuration::DEFAULT_CONNECTION_TIMEOUT
7
+ ENDPOINT_URL = 'https://openexchangerates.org/api/'
8
+
9
+ class << self
10
+ # This method returns the exchange rate, the rate at which the smallest unit of one currency (the base currency)
11
+ # will be exchanged for another currency (the target currency), from Open Exchange Rate's API.
12
+ # This method is called in the OmniExchange.exchange_currency method.
13
+ #
14
+ # @param base_currency: [String] the ISO Currency Code of the currency that you're exchanging from. ie. "USD", "JPY"
15
+ # @param target_currency: [String] the ISO Currency Code of the currency that you're exchanging to. ie. "EUR", "KRW"
16
+ # @ return [BigDecimal] an exchange rate is returned as a BigDecimal for precise calculation since this exchange
17
+ # rate will be used to calculate an convert an exchange of currencies. However, an exception will be raised
18
+ # if there is a timeout while connecting to xe.com or a timeout while reading Open Exchange Rate's API.
19
+ def get_exchange_rate(base_currency:, target_currency:)
20
+ body = api_get do |req|
21
+ req.url 'latest.json'
22
+ req.params['base'] = base_currency
23
+ req.params['symbols'] = target_currency
29
24
  end
30
- rescue *EXCEPTIONS => e
31
- raise e.class, 'Open Exchange Rates has timed out.'
25
+
26
+ exchange_rate = body['rates'][target_currency].to_d
27
+ currency_unit = get_currency_unit(base_currency).to_d
28
+
29
+ (exchange_rate * currency_unit).to_d
32
30
  end
33
31
 
34
- begin
35
- exchange_rate = JSON.parse(response.body, symbolize_names: true)[:rates][target_currency.to_sym].to_d
36
- rescue JSON::ParserError => e
37
- raise e.class, 'JSON::ParserError in OmniExchange::OpenExchangeRates'
32
+ def get_historic_rate(base_currency:, target_currencies:, date:)
33
+ body = api_get do |req|
34
+ req.url "historical/#{date.strftime('%Y-%m-%d')}.json"
35
+
36
+ req.params['base'] = base_currency
37
+ req.params['symbols'] = target_currencies.join(',')
38
+ end
39
+
40
+ currency_unit = get_currency_unit(base_currency).to_d
41
+ body['rates'].transform_values do |rate|
42
+ (rate * currency_unit).to_d
43
+ end
38
44
  end
39
45
 
40
- currency_unit = get_currency_unit(base_currency).to_d
46
+ private
47
+
48
+ def api_get(&blk)
49
+ api = Faraday.new(OmniExchange::OpenExchangeRates::ENDPOINT_URL)
41
50
 
42
- (exchange_rate * currency_unit).to_d
51
+ response = api.get do |req|
52
+ blk.call(req)
53
+
54
+ req.params['app_id'] = config[:app_id]
55
+
56
+ req.options.timeout = config[:read_timeout] ||
57
+ OmniExchange::Configuration::DEFAULT_READ_TIMEOUT
58
+ req.options.open_timeout = config[:connect_timeout] ||
59
+ OmniExchange::Configuration::DEFAULT_CONNECTION_TIMEOUT
60
+ end
61
+
62
+ JSON.parse(response.body)
63
+ end
64
+
65
+ def config
66
+ OmniExchange.configuration.provider_config[:open_exchange_rates]
67
+ end
43
68
  end
44
69
 
45
70
  # when this file is required at the top of lib/omni_exchange.rb, this method call is run and allows
@@ -6,45 +6,89 @@ module OmniExchange
6
6
  class Xe < Provider
7
7
  ENDPOINT_URL = 'https://xecdapi.xe.com/'
8
8
 
9
- # This method returns the exchange rate, the rate at which the smallest unit of one currency (the base currency)
10
- # will be exchanged for another currency (the target currency), from xe.com's API.
11
- # This method is called in the OmniExchange.exchange_currency method.
12
- #
13
- # @param base_currency: [String] the ISO Currency Code of the currency that you're exchanging from. ie. "USD", "JPY"
14
- # @param target_currency: [String] the ISO Currency Code of the currency that you're exchanging to. ie. "EUR", "KRW"
15
- # @ return [BigDecimal] an exchange rate is returned as a BigDecimal for precise calculation since this exchange
16
- # rate will be used to calculate an convert an exchange of currencies. However, an exception will be raised
17
- # if there is a timeout while connecting to xe.com or a timeout while reading xe.com's API.
18
- def self.get_exchange_rate(base_currency:, target_currency:)
19
- config = OmniExchange.configuration.provider_config[:xe]
20
- api_id = config[:api_id]
21
- api_key = config[:api_key]
22
- currency_unit = get_currency_unit(base_currency)
23
-
24
- api = Faraday.new(OmniExchange::Xe::ENDPOINT_URL) do |f|
25
- f.request :basic_auth, api_id, api_key
26
- f.adapter :net_http
9
+ class << self
10
+ # This method returns the exchange rate, the rate at which the smallest unit of one currency (the base currency)
11
+ # will be exchanged for another currency (the target currency), from xe.com's API.
12
+ # This method is called in the OmniExchange.exchange_currency method.
13
+ #
14
+ # @param base_currency: [String] the ISO Currency Code of the currency that you're exchanging from. ie. "USD", "JPY"
15
+ # @param target_currency: [String] the ISO Currency Code of the currency that you're exchanging to. ie. "EUR", "KRW"
16
+ # @ return [BigDecimal] an exchange rate is returned as a BigDecimal for precise calculation since this exchange
17
+ # rate will be used to calculate an convert an exchange of currencies. However, an exception will be raised
18
+ # if there is a timeout while connecting to xe.com or a timeout while reading xe.com's API.
19
+ def get_exchange_rate(base_currency:, target_currency:)
20
+ currency_unit = get_currency_unit(base_currency)
21
+
22
+ body = api_get do |req|
23
+ req.url 'v1/convert_from.json'
24
+
25
+ req.params['from'] = base_currency
26
+ req.params['to'] = target_currency
27
+ req.params['amount'] = currency_unit
28
+ end
29
+
30
+ body[:to][0][:mid].to_d
27
31
  end
28
32
 
29
- begin
30
- response = api.get do |req|
31
- req.url "v1/convert_from.json/?from=#{base_currency}&to=#{target_currency}&amount=#{currency_unit}"
32
- req.options.timeout = config[:read_timeout] || OmniExchange::Configuration::DEFAULT_READ_TIMEOUT
33
- req.options.open_timeout = config[:connect_timeout] || OmniExchange::Configuration::DEFAULT_CONNECTION_TIMEOUT
33
+ # This method returns the historic exchange rate for multiple currencies for a given date.
34
+ #
35
+ # @param base_currency: [String] the ISO Currency Code of the currency that you're exchanging from.
36
+ # ie. "USD", "JPY"
37
+ # @param target_currencies: [Array] an array of ISO Currency Codes of the currencies that you're
38
+ # exchanging to. ie. ["EUR", "KRW"]
39
+ # @param date: [Date] the date for which you want the historic exchange rate.
40
+ def get_historic_rate(base_currency:, target_currencies:, date:)
41
+ currency_unit = get_currency_unit(base_currency)
42
+
43
+ body = api_get do |req|
44
+ req.url 'v1/historic_rate.json'
45
+
46
+ req.params['from'] = base_currency
47
+ req.params['to'] = target_currencies.join(',')
48
+ req.params['amount'] = currency_unit
49
+ req.params['date'] = date.strftime('%Y-%m-%d')
50
+ end
51
+
52
+ rates = {}
53
+ body[:to].each do |rate|
54
+ rates[rate[:quotecurrency]] = rate[:mid].to_d
34
55
  end
35
- rescue *EXCEPTIONS => e
36
- raise e.class, 'xe.com has timed out.'
56
+
57
+ rates
37
58
  end
38
59
 
39
- begin
60
+ private
61
+
62
+ def api_get(&blk)
63
+ response = api.get do |req|
64
+ blk.call(req)
65
+
66
+ req.options.timeout = config[:read_timeout] ||
67
+ OmniExchange::Configuration::DEFAULT_READ_TIMEOUT
68
+ req.options.open_timeout = config[:connect_timeout] ||
69
+ OmniExchange::Configuration::DEFAULT_CONNECTION_TIMEOUT
70
+ end
71
+
40
72
  body = JSON.parse(response.body, symbolize_names: true)
41
- rescue JSON::ParserError => e
42
- raise e.class, 'JSON::ParserError in OmniExchange::Xe'
73
+
74
+ raise OmniExchange::XeMonthlyLimit, 'Xe.com monthly limit has been exceeded' if body[:code] == 3
75
+
76
+ body
43
77
  end
44
78
 
45
- raise OmniExchange::XeMonthlyLimit, 'Xe.com monthly limit has been exceeded' if body[:code] == 3
79
+ def api
80
+ api_id = config[:api_id]
81
+ api_key = config[:api_key]
46
82
 
47
- body[:to][0][:mid].to_d
83
+ Faraday.new(OmniExchange::Xe::ENDPOINT_URL) do |f|
84
+ f.request :basic_auth, api_id, api_key
85
+ f.adapter :net_http
86
+ end
87
+ end
88
+
89
+ def config
90
+ OmniExchange.configuration.provider_config[:xe]
91
+ end
48
92
  end
49
93
 
50
94
  # when this file is required at the top of lib/omni_exchange.rb, this method call is run and allows
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniExchange
4
- VERSION = '1.5.0'
4
+ VERSION = '1.7.0'
5
5
  end
data/lib/omni_exchange.rb CHANGED
@@ -34,7 +34,16 @@ module OmniExchange
34
34
  end
35
35
 
36
36
  # if a provider raises one of these exceptions, OmniExchange will gracefully attempt to use another provider
37
- EXCEPTIONS = [Faraday::Error, Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError, Net::OpenTimeout, Net::WriteTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError]
37
+ EXCEPTIONS = [
38
+ Faraday::Error,
39
+ Faraday::ConnectionFailed,
40
+ Faraday::TimeoutError,
41
+ Faraday::SSLError,
42
+ Net::OpenTimeout,
43
+ Net::WriteTimeout,
44
+ Net::ReadTimeout,
45
+ OpenSSL::SSL::SSLError
46
+ ]
38
47
 
39
48
  module_function
40
49
 
@@ -53,6 +62,7 @@ module OmniExchange
53
62
  # * :converted_amount [BigDecimal] the amount of money exchanged from the base currency to the target
54
63
  # currency as a BigDecimal for precice calculation. ie. 1, 10, 100
55
64
  # * :exchange_rate [BigDecimal] the rate used to calculate the converted_amount as a BigDecimal. ie. 0.95211e1
65
+ # * :non_subunit_fx_rate [BigDecimal] a rate that can be used when a currency with subunits is not in cents . ie. 0.95211e3
56
66
  # * :provider [Symbol] the provider that supplied the exchange_rate data. ie. :xe, :open_exchange_rates
57
67
  def get_fx_data(amount:, base_currency:, target_currency:, providers:)
58
68
  # if one of the currencies is not valid (ie. 'fake_crypto'), an exception is raised.
@@ -71,15 +81,22 @@ module OmniExchange
71
81
  # Gracefully hit each provider and fail-over to the next one
72
82
  provider_classes.each do |klass|
73
83
  rate = klass.get_exchange_rate(base_currency: base_currency, target_currency: target_currency)
84
+ plain_format_rate = (rate * Money::Currency.wrap(base_currency).subunit_to_unit).to_d
74
85
 
75
86
  exchanged_amount = rate.to_d * amount.to_d
76
87
 
77
- return { converted_amount: exchanged_amount, exchange_rate: rate, provider: OmniExchange::Provider.all.key(klass) }
88
+ return {
89
+ converted_amount: exchanged_amount,
90
+ exchange_rate: rate,
91
+ non_subunit_fx_rate: plain_format_rate,
92
+ provider: OmniExchange::Provider.all.key(klass)
93
+ }
78
94
  rescue *EXCEPTIONS, OmniExchange::XeMonthlyLimit, JSON::ParserError => e
79
95
  error_messages << e.inspect
80
96
  end
81
-
82
- raise OmniExchange::HttpError, "Failed to load #{base_currency}->#{target_currency}:\n#{error_messages.join("\n")}"
97
+
98
+ raise OmniExchange::HttpError, "Failed to load #{base_currency}->#{target_currency}:\n" \
99
+ "#{error_messages.join("\n")}"
83
100
  end
84
101
  end
85
102
  # rubocop:enable Lint/Syntax
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  spec.add_development_dependency 'dotenv'
34
34
  spec.add_development_dependency 'pry'
35
- spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rake', '~> 12.3'
36
36
  spec.add_development_dependency 'rspec', '~> 3.0'
37
37
  spec.add_development_dependency 'rubocop', '~> 0.80'
38
38
  spec.add_development_dependency 'vcr'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omni_exchange
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yun Chung
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-17 00:00:00.000000000 Z
11
+ date: 2023-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: '12.3'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: '12.3'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  - !ruby/object:Gem::Version
176
176
  version: '0'
177
177
  requirements: []
178
- rubygems_version: 3.2.3
178
+ rubygems_version: 3.2.33
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: OmniExchange converts currencies using up-to-the-minute foreign exchange