omni_exchange 1.2.0 → 1.5.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: 79069dcd87cf3788896dbc2093d114028b94592743a5fea03559cfce974b4d5c
4
- data.tar.gz: 03a24de2dcfe6dca9ee1c0532e40b8bab6addbc14c9e4863ef644789a0a24adb
3
+ metadata.gz: c591dfb40110a465f93f0c714d6f26369ba9717592a8deb4da4f92e044a4f660
4
+ data.tar.gz: 91f086e1912b69998b55d6e76030dfdc8738388071c0405084fc6d8e8fa6174a
5
5
  SHA512:
6
- metadata.gz: bb380807a18d884f6ebef61b911e9a5301f6704577a15b8b4fc75cc11a4e0337ee1c911ea01c9a8269f919f6df62f86225cc9270bf24c8cad753eed0cefa8e2d
7
- data.tar.gz: f0909dabbacc96857512b405cc590431ca9df724f622d0e59d589c62e2856e2d6193e992370f4a06d52df2eff7c3f6d1acca1741f5d6c12c9be20c9736b6eef0
6
+ metadata.gz: 813ec7f4ce3f0d18360a39cd864c39fcb62bb1dbacc36a4b089b7d970d32b9bfa6c334fd6d11921f99dcc0d4b48ec7e5a45fd169b11ae6e852969e0f6d768ee3
7
+ data.tar.gz: 75904791e0aaff4d160b5d11424c50ddfbd32450a5e0d52da2e4a246e5dde5aba81efe23f1bb9ed0ab656d4399eb3d48106f68c790c660fa50d76258265e3d55
data/.gitignore CHANGED
@@ -18,3 +18,7 @@ omni_exchange-0.1.0.gem
18
18
  omni_exchange-0.2.0.gem
19
19
  omni_exchange-1.1.0.gem
20
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
data/.rubocop_todo.yml CHANGED
@@ -1,15 +1,20 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2022-06-30 01:49:03 UTC using RuboCop version 0.93.1.
3
+ # on 2022-07-12 04:26:20 UTC using RuboCop version 0.93.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
+ # Offense count: 1
10
+ Lint/UselessAssignment:
11
+ Exclude:
12
+ - 'lib/sandbox.rb'
13
+
9
14
  # Offense count: 2
10
15
  # Configuration parameters: IgnoredMethods.
11
16
  Metrics/AbcSize:
12
- Max: 28
17
+ Max: 33
13
18
 
14
19
  # Offense count: 1
15
20
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
@@ -39,7 +44,7 @@ Style/ExponentialNotation:
39
44
  Exclude:
40
45
  - 'spec/omni_exchange_spec.rb'
41
46
 
42
- # Offense count: 3
47
+ # Offense count: 4
43
48
  # Cop supports --auto-correct.
44
49
  # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
45
50
  # SupportedStyles: single_quotes, double_quotes
@@ -47,9 +52,17 @@ Style/StringLiterals:
47
52
  Exclude:
48
53
  - 'lib/sandbox.rb'
49
54
 
50
- # Offense count: 16
55
+ # Offense count: 1
56
+ # Cop supports --auto-correct.
57
+ # Configuration parameters: .
58
+ # SupportedStyles: percent, brackets
59
+ Style/SymbolArray:
60
+ EnforcedStyle: percent
61
+ MinSize: 3
62
+
63
+ # Offense count: 19
51
64
  # Cop supports --auto-correct.
52
65
  # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
53
66
  # URISchemes: http, https
54
67
  Layout/LineLength:
55
- Max: 226
68
+ Max: 220
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- omni_exchange (1.2.0)
5
- faraday (= 0.17.4)
4
+ omni_exchange (1.4.0)
5
+ faraday (< 2)
6
6
  money (~> 6.13.1)
7
7
 
8
8
  GEM
@@ -12,17 +12,38 @@ GEM
12
12
  coderay (1.1.3)
13
13
  concurrent-ruby (1.1.10)
14
14
  diff-lcs (1.5.0)
15
- dotenv (2.7.6)
16
- faraday (0.17.4)
17
- multipart-post (>= 1.2, < 3)
18
- i18n (1.10.0)
15
+ dotenv (2.8.1)
16
+ faraday (1.10.1)
17
+ faraday-em_http (~> 1.0)
18
+ faraday-em_synchrony (~> 1.0)
19
+ faraday-excon (~> 1.1)
20
+ faraday-httpclient (~> 1.0)
21
+ faraday-multipart (~> 1.0)
22
+ faraday-net_http (~> 1.0)
23
+ faraday-net_http_persistent (~> 1.0)
24
+ faraday-patron (~> 1.0)
25
+ faraday-rack (~> 1.0)
26
+ faraday-retry (~> 1.0)
27
+ ruby2_keywords (>= 0.0.4)
28
+ faraday-em_http (1.0.0)
29
+ faraday-em_synchrony (1.0.0)
30
+ faraday-excon (1.1.0)
31
+ faraday-httpclient (1.0.1)
32
+ faraday-multipart (1.0.4)
33
+ multipart-post (~> 2)
34
+ faraday-net_http (1.0.1)
35
+ faraday-net_http_persistent (1.2.0)
36
+ faraday-patron (1.0.0)
37
+ faraday-rack (1.0.0)
38
+ faraday-retry (1.0.3)
39
+ i18n (1.12.0)
19
40
  concurrent-ruby (~> 1.0)
20
41
  method_source (1.0.0)
21
42
  money (6.13.8)
22
43
  i18n (>= 0.6.4, <= 2)
23
44
  multipart-post (2.2.3)
24
45
  parallel (1.22.1)
25
- parser (3.1.2.0)
46
+ parser (3.1.2.1)
26
47
  ast (~> 2.4.1)
27
48
  pry (0.14.1)
28
49
  coderay (~> 1.1)
@@ -53,14 +74,16 @@ GEM
53
74
  rubocop-ast (>= 0.6.0)
54
75
  ruby-progressbar (~> 1.7)
55
76
  unicode-display_width (>= 1.4.0, < 2.0)
56
- rubocop-ast (1.18.0)
77
+ rubocop-ast (1.21.0)
57
78
  parser (>= 3.1.1.0)
58
79
  ruby-progressbar (1.11.0)
80
+ ruby2_keywords (0.0.5)
59
81
  unicode-display_width (1.8.0)
60
82
  vcr (6.1.0)
61
83
 
62
84
  PLATFORMS
63
85
  ruby
86
+ x86_64-darwin-19
64
87
 
65
88
  DEPENDENCIES
66
89
  dotenv
data/README.md CHANGED
@@ -57,7 +57,7 @@ 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_class: (Class) the name of the provider class that supplied the exchange_rate (ie. OmniExchange::OpenExhangeRates)
60
+ 3. provider: (Symbol) the provider that supplied the exchange_rate (ie. :open_exchange_rates, :xe)
61
61
 
62
62
  [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
63
 
@@ -67,7 +67,7 @@ Here is an example. Lets say I want to convert $1.00 US Dollar to Japanese Yen,
67
67
  ```ruby
68
68
  USD_to_JPY = OmniExchange.get_fx_data(amount: 100, base_currency: 'USD', target_currency: 'JPY', providers: [:open_exchange_rates, :xe])
69
69
 
70
- puts USD_to_JPY # => { :converted_amount=>0.13566633333e3, :exchange_rate=>0.13566633333e1, :provider_class=>OmniExchange::OpenExchangeRates }
70
+ puts USD_to_JPY # => { :converted_amount=>0.13566633333e3, :exchange_rate=>0.13566633333e1, :provider=>:open_exchange_rates }
71
71
 
72
72
  puts USD_to_JPY[:converted_amount] # => 0.13566633333e3
73
73
  puts USD_to_JPY[:converted_amount].to_f # => 135.66633333
data/bin/console CHANGED
@@ -6,6 +6,7 @@ require 'omni_exchange'
6
6
  require 'dotenv/load'
7
7
  require 'faraday'
8
8
  require 'money'
9
+ require 'net/http'
9
10
  require_relative '../lib/omni_exchange/provider'
10
11
  require_relative '../lib/omni_exchange/configuration'
11
12
  require_relative '../lib/omni_exchange/error'
@@ -27,11 +27,15 @@ module OmniExchange
27
27
  req.options.timeout = config[:read_timeout] || OmniExchange::Configuration::DEFAULT_READ_TIMEOUT
28
28
  req.options.open_timeout = config[:connect_timeout] || OmniExchange::Configuration::DEFAULT_CONNECTION_TIMEOUT
29
29
  end
30
- rescue Faraday::Error, Faraday::ConnectionFailed => e
30
+ rescue *EXCEPTIONS => e
31
31
  raise e.class, 'Open Exchange Rates has timed out.'
32
32
  end
33
33
 
34
- exchange_rate = JSON.parse(response.body, symbolize_names: true)[:rates][target_currency.to_sym].to_d
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'
38
+ end
35
39
 
36
40
  currency_unit = get_currency_unit(base_currency).to_d
37
41
 
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/AbcSize
4
3
  require 'omni_exchange'
5
4
 
6
5
  module OmniExchange
@@ -33,11 +32,15 @@ module OmniExchange
33
32
  req.options.timeout = config[:read_timeout] || OmniExchange::Configuration::DEFAULT_READ_TIMEOUT
34
33
  req.options.open_timeout = config[:connect_timeout] || OmniExchange::Configuration::DEFAULT_CONNECTION_TIMEOUT
35
34
  end
36
- rescue Faraday::Error, Faraday::ConnectionFailed => e
35
+ rescue *EXCEPTIONS => e
37
36
  raise e.class, 'xe.com has timed out.'
38
37
  end
39
38
 
40
- body = JSON.parse(response.body, symbolize_names: true)
39
+ begin
40
+ body = JSON.parse(response.body, symbolize_names: true)
41
+ rescue JSON::ParserError => e
42
+ raise e.class, 'JSON::ParserError in OmniExchange::Xe'
43
+ end
41
44
 
42
45
  raise OmniExchange::XeMonthlyLimit, 'Xe.com monthly limit has been exceeded' if body[:code] == 3
43
46
 
@@ -49,4 +52,3 @@ module OmniExchange
49
52
  OmniExchange::Provider.register_provider(:xe, self)
50
53
  end
51
54
  end
52
- # rubocop:enable Metrics/AbcSize
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniExchange
4
- VERSION = '1.2.0'
4
+ VERSION = '1.5.0'
5
5
  end
data/lib/omni_exchange.rb CHANGED
@@ -12,6 +12,7 @@ require 'faraday'
12
12
  require 'money'
13
13
  require 'json'
14
14
  require 'bigdecimal/util'
15
+ require 'net/http'
15
16
 
16
17
  # rubocop:disable Lint/Syntax
17
18
  module OmniExchange
@@ -32,6 +33,9 @@ module OmniExchange
32
33
  yield(configuration)
33
34
  end
34
35
 
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]
38
+
35
39
  module_function
36
40
 
37
41
  # returns foreign exchange data including the amount of money in one country's currency when exchanged from an
@@ -44,15 +48,12 @@ module OmniExchange
44
48
  # @param providers: [Array] an array of symbols of the providers that will be used to get exchange rates API
45
49
  # data. The symbols must be found in the @providers hash in the Provider class (lib/omni_exchange/provider.rb).
46
50
  # ie. xe:, :open_exchange_rates
47
- # @return [Hash] If all of the providers in the providers hash fail to retrieve data, or if one of the currencies
48
- # is not valid, an exception is raised.
51
+ # @return [Hash] If none of the providers in the providers hash are able to retrieve data, or if one of the
52
+ # currencies is not valid, an exception is raised.
49
53
  # * :converted_amount [BigDecimal] the amount of money exchanged from the base currency to the target
50
54
  # currency as a BigDecimal for precice calculation. ie. 1, 10, 100
51
55
  # * :exchange_rate [BigDecimal] the rate used to calculate the converted_amount as a BigDecimal. ie. 0.95211e1
52
- # * :provider_class [Class] the provider class that supplied the exchange_rate data. ie. OmniExchange::Xe
53
- #the amount of the base currency exchanged to the target currency using an exchange rate
54
- # provided by one of the data providers in the providers hash. The final amount is returned as a BigDecimal
55
- # for precise calculation. If all of the providers in the providers hash fail to retrieve data, an exception is raised.
56
+ # * :provider [Symbol] the provider that supplied the exchange_rate data. ie. :xe, :open_exchange_rates
56
57
  def get_fx_data(amount:, base_currency:, target_currency:, providers:)
57
58
  # if one of the currencies is not valid (ie. 'fake_crypto'), an exception is raised.
58
59
  begin
@@ -73,8 +74,8 @@ module OmniExchange
73
74
 
74
75
  exchanged_amount = rate.to_d * amount.to_d
75
76
 
76
- return { converted_amount: exchanged_amount, exchange_rate: rate, provider_class: klass }
77
- rescue Faraday::Error, Faraday::ConnectionFailed, OmniExchange::XeMonthlyLimit => e
77
+ return { converted_amount: exchanged_amount, exchange_rate: rate, provider: OmniExchange::Provider.all.key(klass) }
78
+ rescue *EXCEPTIONS, OmniExchange::XeMonthlyLimit, JSON::ParserError => e
78
79
  error_messages << e.inspect
79
80
  end
80
81
 
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ['lib']
29
29
 
30
- spec.add_dependency 'faraday', '0.17.4'
30
+ spec.add_dependency 'faraday', '< 2'
31
31
  spec.add_dependency 'money', '~> 6.13.1'
32
32
 
33
33
  spec.add_development_dependency 'dotenv'
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omni_exchange
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.5.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-07-05 00:00:00.000000000 Z
11
+ date: 2022-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "<"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.17.4
19
+ version: '2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - "<"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.17.4
26
+ version: '2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: money
29
29
  requirement: !ruby/object:Gem::Requirement