open_exchange_rates 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/open_exchange_rates/rates.rb +5 -4
- data/lib/open_exchange_rates/version.rb +1 -1
- data/lib/open_exchange_rates.rb +0 -1
- data/open_exchange_rates.gemspec +1 -3
- data/test/rates_test.rb +99 -119
- data/test/test_helper.rb +1 -4
- metadata +3 -18
- data/lib/open_exchange_rates/parser.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be2fae1e12c458efcb3c1059426aec3b923c4d13f933e95c48aeec6f59adeb78
|
4
|
+
data.tar.gz: 4c434c733a556a65d2cc0ae80e81be584c09d06f7d4b21ea4b102e4e631bfac6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d33b6c67b4d67be6d77c99f0396c53fd84f85f00126260a72a5eb9f7ce9393ce32754fa2658fa760f75f2197b3f8b7404c2dd08177272b04f0e4e8267db2057d
|
7
|
+
data.tar.gz: 69c93f940c07296fa75c0b00c32dbb390c763c103fc411232d532a8d23318880aa0d40b712d7534e6019db2f3b31b51b3605e58923e8dd158569249b1e604766
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "open-uri"
|
2
2
|
require "date"
|
3
|
+
require "json"
|
3
4
|
|
4
5
|
module OpenExchangeRates
|
5
6
|
class Rates
|
@@ -90,13 +91,13 @@ module OpenExchangeRates
|
|
90
91
|
end
|
91
92
|
|
92
93
|
def parse_latest
|
93
|
-
|
94
|
-
|
94
|
+
json = URI.open("#{OpenExchangeRates::LATEST_URL}?app_id=#{@app_id}").read
|
95
|
+
JSON.parse(json)
|
95
96
|
end
|
96
97
|
|
97
98
|
def parse_on(date_string)
|
98
|
-
|
99
|
-
|
99
|
+
json = URI.open("#{OpenExchangeRates::BASE_URL}/historical/#{date_string}.json?app_id=#{@app_id}").read
|
100
|
+
JSON.parse(json)
|
100
101
|
end
|
101
102
|
|
102
103
|
end
|
data/lib/open_exchange_rates.rb
CHANGED
data/open_exchange_rates.gemspec
CHANGED
@@ -16,9 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.version = OpenExchangeRates::VERSION
|
17
17
|
gem.license = "MIT"
|
18
18
|
|
19
|
-
gem.
|
20
|
-
|
21
|
-
gem.add_development_dependency('rr')
|
19
|
+
gem.add_development_dependency('minitest')
|
22
20
|
gem.add_development_dependency('rake')
|
23
21
|
gem.add_development_dependency('dotenv')
|
24
22
|
end
|
data/test/rates_test.rb
CHANGED
@@ -1,17 +1,12 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
require 'complex'
|
3
|
+
require 'json'
|
3
4
|
|
4
|
-
class TestOpenExchangeRates < Test
|
5
|
-
|
5
|
+
class TestOpenExchangeRates < Minitest::Test
|
6
6
|
def test_app_id_is_required
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
assert_raise(OpenExchangeRates::Rates::MissingAppIdError) { OpenExchangeRates::Rates.new }
|
11
|
-
|
12
|
-
assert_nothing_raised { OpenExchangeRates::Rates.new(:app_id => "myappid") }
|
13
|
-
|
14
|
-
OpenExchangeRates.configuration.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
|
7
|
+
OpenExchangeRates.configuration.stub(:app_id, nil) do
|
8
|
+
assert_raises(OpenExchangeRates::Rates::MissingAppIdError) { OpenExchangeRates::Rates.new }
|
9
|
+
end
|
15
10
|
end
|
16
11
|
|
17
12
|
def test_app_id_configuration
|
@@ -24,129 +19,129 @@ class TestOpenExchangeRates < Test::Unit::TestCase
|
|
24
19
|
fx = OpenExchangeRates::Rates.new
|
25
20
|
assert_equal "myappid", fx.app_id
|
26
21
|
|
27
|
-
fx = OpenExchangeRates::Rates.new(:
|
22
|
+
fx = OpenExchangeRates::Rates.new(app_id: 'myotherappid')
|
28
23
|
assert_equal "myotherappid", fx.app_id
|
29
24
|
|
30
25
|
OpenExchangeRates.configuration.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
|
31
26
|
end
|
32
27
|
|
33
28
|
def test_invalid_app_id_raise_error
|
34
|
-
|
35
|
-
|
29
|
+
OpenExchangeRates.configuration.stub(:app_id, "invalid-app-id") do
|
30
|
+
fx = OpenExchangeRates::Rates.new
|
36
31
|
|
37
|
-
|
38
|
-
|
32
|
+
assert_raises OpenURI::HTTPError do
|
33
|
+
fx.exchange_rate(:from => "USD", :to => "EUR")
|
34
|
+
end
|
39
35
|
end
|
40
36
|
end
|
41
37
|
|
42
38
|
def test_exchange_rate
|
43
39
|
fx = OpenExchangeRates::Rates.new
|
44
|
-
stub(
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
assert_equal 1, fx.exchange_rate(:from => "HRK", :to => "HRK")
|
40
|
+
fx.stub(:parse_latest, parse_asset("latest.json")) do
|
41
|
+
# 1 USD = 6.0995 HRK
|
42
|
+
# 1 USD = 1.026057 AUD
|
43
|
+
assert_equal 1, fx.exchange_rate(:from => "USD", :to => "USD")
|
44
|
+
assert_equal 1, fx.exchange_rate(:from => "AUD", :to => "AUD")
|
45
|
+
assert_equal 1, fx.exchange_rate(:from => "HRK", :to => "HRK")
|
51
46
|
|
52
|
-
|
53
|
-
|
47
|
+
assert_equal 6.0995, fx.exchange_rate(:from => "USD", :to => "HRK")
|
48
|
+
assert_equal 1.026057, fx.exchange_rate(:from => "USD", :to => "AUD")
|
54
49
|
|
55
|
-
|
56
|
-
|
50
|
+
assert_equal 0.163948, fx.exchange_rate(:from => "HRK", :to => "USD")
|
51
|
+
assert_equal 0.974605, fx.exchange_rate(:from => "AUD", :to => "USD")
|
57
52
|
|
58
|
-
|
59
|
-
|
53
|
+
assert_equal 5.944602, fx.exchange_rate(:from => "AUD", :to => "HRK")
|
54
|
+
assert_equal 0.168220, fx.exchange_rate(:from => "HRK", :to => "AUD")
|
60
55
|
|
61
|
-
|
62
|
-
|
56
|
+
assert_equal 0.00023, fx.exchange_rate(:from => 'SLL', to: 'USD')
|
57
|
+
assert_equal 4350, fx.exchange_rate(:from => 'USD', to: 'SLL')
|
58
|
+
end
|
63
59
|
end
|
64
60
|
|
65
61
|
def test_exchange_rate_on_specific_date
|
66
62
|
fx = OpenExchangeRates::Rates.new
|
67
|
-
stub(
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
assert_equal 1, fx.exchange_rate(:from => "HRK", :to => "HRK", :on => "2012-05-10")
|
63
|
+
fx.stub(:parse_on, parse_asset("2012-05-10.json")) do
|
64
|
+
# 1 USD = 5.80025 HRK
|
65
|
+
# 1 USD = 0.99458 AUD
|
66
|
+
assert_equal 1, fx.exchange_rate(:from => "USD", :to => "USD", :on => "2012-05-10")
|
67
|
+
assert_equal 1, fx.exchange_rate(:from => "AUD", :to => "AUD", :on => "2012-05-10")
|
68
|
+
assert_equal 1, fx.exchange_rate(:from => "HRK", :to => "HRK", :on => "2012-05-10")
|
74
69
|
|
75
|
-
|
76
|
-
|
70
|
+
assert_equal 5.80025, fx.exchange_rate(:from => "USD", :to => "HRK", :on => "2012-05-10")
|
71
|
+
assert_equal 0.99458, fx.exchange_rate(:from => "USD", :to => "AUD", :on => "2012-05-10")
|
77
72
|
|
78
|
-
|
79
|
-
|
73
|
+
assert_equal 0.172406, fx.exchange_rate(:from => "HRK", :to => "USD", :on => "2012-05-10")
|
74
|
+
assert_equal 1.005450, fx.exchange_rate(:from => "AUD", :to => "USD", :on => "2012-05-10")
|
80
75
|
|
81
|
-
|
82
|
-
|
76
|
+
assert_equal 5.831859, fx.exchange_rate(:from => "AUD", :to => "HRK", :on => "2012-05-10")
|
77
|
+
assert_equal 0.171472, fx.exchange_rate(:from => "HRK", :to => "AUD", :on => "2012-05-10")
|
78
|
+
end
|
83
79
|
end
|
84
80
|
|
85
81
|
def test_exchange_rate_on_specific_date_specified_by_date_class
|
86
82
|
fx = OpenExchangeRates::Rates.new
|
87
|
-
stub(
|
88
|
-
|
89
|
-
|
83
|
+
fx.stub(:parse_on, parse_asset("2012-05-10.json")) do
|
84
|
+
assert_equal 1, fx.exchange_rate(:from => "USD", :to => "USD", :on => Date.new(2012,05,10))
|
85
|
+
end
|
90
86
|
end
|
91
87
|
|
92
88
|
def test_exchange_requires_valid_date
|
93
89
|
fx = OpenExchangeRates::Rates.new
|
94
90
|
|
95
|
-
|
96
|
-
fx.exchange_rate(:from => "USD", :to => "USD", :on => "
|
91
|
+
assert_raises ArgumentError do
|
92
|
+
fx.exchange_rate(:from => "USD", :to => "USD", :on => "invalid-date")
|
97
93
|
end
|
98
|
-
|
99
|
-
assert_raise ArgumentError do
|
94
|
+
assert_raises ArgumentError do
|
100
95
|
fx.exchange_rate(:from => "USD", :to => "USD", :on => Complex(0.3))
|
101
96
|
end
|
102
97
|
end
|
103
98
|
|
104
99
|
def test_convert
|
105
100
|
fx = OpenExchangeRates::Rates.new
|
106
|
-
stub(
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
101
|
+
fx.stub(:parse_latest, parse_asset("latest.json")) do
|
102
|
+
# 1 USD = 6.0995 HRK
|
103
|
+
# 1 USD = 1.026057 AUD
|
104
|
+
assert_equal 609.95, fx.convert(100, :from => "USD", :to => "HRK")
|
105
|
+
assert_equal 16.39, fx.convert(100, :from => "HRK", :to => "USD")
|
106
|
+
assert_equal 120.32, fx.convert(123.4567, :from => "AUD", :to => "USD")
|
107
|
+
assert_equal 733.90, fx.convert(123.4567, :from => "AUD", :to => "HRK")
|
108
|
+
assert_equal 20.77, fx.convert(123.4567, :from => "HRK", :to => "AUD")
|
109
|
+
end
|
115
110
|
end
|
116
111
|
|
117
112
|
def test_convert_on_specific_date
|
118
113
|
fx = OpenExchangeRates::Rates.new
|
119
|
-
stub(
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
114
|
+
fx.stub(:parse_on, parse_asset("2012-05-10.json")) do
|
115
|
+
# 1 USD = 5.80025 HRK
|
116
|
+
# 1 USD = 0.99458 AUD
|
117
|
+
assert_equal 580.03, fx.convert(100, :from => "USD", :to => "HRK", :on => "2012-10-05")
|
118
|
+
assert_equal 17.24, fx.convert(100, :from => "HRK", :to => "USD", :on => "2012-10-05")
|
119
|
+
assert_equal 124.13, fx.convert(123.4567, :from => "AUD", :to => "USD", :on => "2012-10-05")
|
120
|
+
assert_equal 719.98, fx.convert(123.4567, :from => "AUD", :to => "HRK", :on => "2012-10-05")
|
121
|
+
assert_equal 21.17, fx.convert(123.4567, :from => "HRK", :to => "AUD", :on => "2012-10-05")
|
122
|
+
end
|
128
123
|
end
|
129
124
|
|
130
125
|
def test_convert_if_from_option_is_missing
|
131
126
|
fx = OpenExchangeRates::Rates.new
|
132
|
-
stub(
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
127
|
+
fx.stub(:parse_latest, parse_asset("latest.json")) do
|
128
|
+
# from defaults to base currency (USD)
|
129
|
+
# 1 USD = 6.0995 HRK
|
130
|
+
# 1 USD = 1.026057 AUD
|
131
|
+
assert_equal 609.95, fx.convert(100, :to => "HRK")
|
132
|
+
assert_equal 102.61, fx.convert(100, :to => "AUD")
|
133
|
+
end
|
139
134
|
end
|
140
135
|
|
141
136
|
def test_convert_if_to_option_is_missing
|
142
137
|
fx = OpenExchangeRates::Rates.new
|
143
|
-
stub(
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
138
|
+
fx.stub(:parse_latest, parse_asset("latest.json")) do
|
139
|
+
# to defaults to base currency (USD)
|
140
|
+
# 1 USD = 6.0995 HRK
|
141
|
+
# 1 USD = 1.026057 AUD
|
142
|
+
assert_equal 16.39, fx.convert(100, :from => "HRK")
|
143
|
+
assert_equal 97.46, fx.convert(100, :from => "AUD")
|
144
|
+
end
|
150
145
|
end
|
151
146
|
|
152
147
|
def test_latest
|
@@ -157,23 +152,23 @@ class TestOpenExchangeRates < Test::Unit::TestCase
|
|
157
152
|
assert_equal "USD", latest_rates.base
|
158
153
|
assert latest_rates.rates.is_a?(Hash)
|
159
154
|
|
160
|
-
stub(
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
155
|
+
fx.stub(:parse_latest, parse_asset("latest.json")) do
|
156
|
+
# latest results are cached
|
157
|
+
cached_rates = fx.latest
|
158
|
+
assert_equal latest_rates.rates["USD"], cached_rates.rates["USD"]
|
159
|
+
assert_equal latest_rates.rates["AUD"], cached_rates.rates["AUD"]
|
160
|
+
assert_equal latest_rates.rates["HRK"], cached_rates.rates["HRK"]
|
161
|
+
|
162
|
+
# latest results are reloaded
|
163
|
+
stubbed_rates = fx.latest(:reload)
|
164
|
+
assert_equal latest_rates.rates["USD"], stubbed_rates.rates["USD"]
|
165
|
+
refute_equal latest_rates.rates["AUD"], stubbed_rates.rates["AUD"]
|
166
|
+
refute_equal latest_rates.rates["HRK"], stubbed_rates.rates["HRK"]
|
167
|
+
|
168
|
+
assert_equal 1, stubbed_rates.rates["USD"]
|
169
|
+
assert_equal 1.026057, stubbed_rates.rates["AUD"]
|
170
|
+
assert_equal 6.0995, stubbed_rates.rates["HRK"]
|
171
|
+
end
|
177
172
|
end
|
178
173
|
|
179
174
|
def test_on
|
@@ -197,29 +192,14 @@ class TestOpenExchangeRates < Test::Unit::TestCase
|
|
197
192
|
assert_equal 12.3457, fx.round(12.345678, 4)
|
198
193
|
end
|
199
194
|
|
200
|
-
def test_multiple_calls
|
201
|
-
fx = OpenExchangeRates::Rates.new
|
202
|
-
|
203
|
-
assert_nothing_raised do
|
204
|
-
fx.convert(123, :from => "EUR", :to => "AUD", :on => "2012-03-10")
|
205
|
-
fx.convert(100, :from => "USD", :to => "EUR")
|
206
|
-
fx.convert(123.45, :from => "EUR", :to => "USD", :on => "2012-04-10")
|
207
|
-
fx.convert(12, :from => "USD", :to => "EUR")
|
208
|
-
fx.convert(123.4567, :from => "EUR", :to => "USD", :on => "2012-05-10")
|
209
|
-
fx.exchange_rate(:from => "USD", :to => "EUR")
|
210
|
-
fx.exchange_rate(:from => "USD", :to => "EUR", :on => "2012-04-10")
|
211
|
-
fx.exchange_rate(:from => "USD", :to => "AUD", :on => "2012-05-10")
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
195
|
def test_invalid_currency_code_fails_with_rate_not_found_error
|
216
196
|
fx = OpenExchangeRates::Rates.new
|
217
197
|
|
218
|
-
|
198
|
+
assert_raises(OpenExchangeRates::Rates::RateNotFoundError) do
|
219
199
|
fx.exchange_rate(:from => "???", :to => "AUD", :on => "2012-05-10")
|
220
200
|
end
|
221
201
|
|
222
|
-
|
202
|
+
assert_raises(OpenExchangeRates::Rates::RateNotFoundError) do
|
223
203
|
fx.exchange_rate(:from => "USD", :to => "???", :on => "2012-05-10")
|
224
204
|
end
|
225
205
|
end
|
@@ -230,8 +210,8 @@ private
|
|
230
210
|
File.join(File.dirname(__FILE__), "assets")
|
231
211
|
end
|
232
212
|
|
233
|
-
def
|
234
|
-
File.open("#{assets_root}/#{filename}")
|
213
|
+
def parse_asset(filename)
|
214
|
+
json = File.open("#{assets_root}/#{filename}").read
|
215
|
+
JSON.parse(json)
|
235
216
|
end
|
236
|
-
|
237
217
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "rubygems"
|
2
|
-
require "test/unit"
|
3
|
-
require "rr"
|
4
2
|
require 'dotenv'
|
3
|
+
require "minitest/autorun"
|
5
4
|
require "open_exchange_rates"
|
6
5
|
|
7
6
|
# Pick up ENV variables from .env file if exists
|
@@ -9,5 +8,3 @@ Dotenv.load
|
|
9
8
|
|
10
9
|
OpenExchangeRates.configuration.app_id = ENV['OPEN_EXCHANGE_RATES_APP_ID']
|
11
10
|
|
12
|
-
class Test::Unit::TestCase
|
13
|
-
end
|
metadata
CHANGED
@@ -1,31 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_exchange_rates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vlado Cingel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rr
|
14
|
+
name: minitest
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
17
|
- - ">="
|
@@ -84,7 +70,6 @@ files:
|
|
84
70
|
- env.example
|
85
71
|
- lib/open_exchange_rates.rb
|
86
72
|
- lib/open_exchange_rates/configuration.rb
|
87
|
-
- lib/open_exchange_rates/parser.rb
|
88
73
|
- lib/open_exchange_rates/rates.rb
|
89
74
|
- lib/open_exchange_rates/response.rb
|
90
75
|
- lib/open_exchange_rates/version.rb
|