currency_coin_converter 0.2.4 → 0.3.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 +4 -4
- data/CHANGELOG.md +1 -1
- data/Gemfile +3 -3
- data/Gemfile.lock +16 -14
- data/README.md +5 -4
- data/bin/console +1 -1
- data/currency_coin_converter.gemspec +1 -6
- data/lib/currency_coin_converter.rb +68 -18
- data/spec/currency_coin_converter_spec.rb +33 -6
- data/spec/examples/inr.json +174 -0
- metadata +4 -19
- data/lib/currency_coin_converter/version.rb +0 -5
- data/sig/currency_coin_converter.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fbacf0f12f980a242f4f6c765c1e7195aeb73a36e92f668a23caf7de23ad0d8
|
4
|
+
data.tar.gz: dcd21694c722a945656103f084110d5d42bc77143e6f488ddbaf23b2c000e242
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45dee2fb4709e41770c912927d205428aa24b4574d52777c5761e32091afc49f9b5a4d40c81f789276538371433763a03181ad30340e59d1f1e9e1f74f2a7767
|
7
|
+
data.tar.gz: 0b331d65a475f5eb86d8eca9a5831c60ddfb383e45f03d5c1e916cd0e58a97c52fe644265f4c49592684ac6ddca6bee745d75f8907b0e41da25f92293c73102a
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,32 +1,34 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
currency_coin_converter (0.
|
5
|
-
httparty (~> 0.18)
|
4
|
+
currency_coin_converter (0.3.0)
|
6
5
|
|
7
6
|
GEM
|
8
7
|
remote: https://rubygems.org/
|
9
8
|
specs:
|
10
|
-
|
11
|
-
csv (3.3.0)
|
12
|
-
dotenv (3.1.2)
|
13
|
-
httparty (0.22.0)
|
14
|
-
csv
|
15
|
-
mini_mime (>= 1.0.0)
|
16
|
-
multi_xml (>= 0.5.2)
|
17
|
-
mini_mime (1.1.5)
|
18
|
-
multi_xml (0.7.1)
|
19
|
-
bigdecimal (~> 3.1)
|
9
|
+
diff-lcs (1.5.1)
|
20
10
|
rake (13.2.1)
|
11
|
+
rspec (3.13.0)
|
12
|
+
rspec-core (~> 3.13.0)
|
13
|
+
rspec-expectations (~> 3.13.0)
|
14
|
+
rspec-mocks (~> 3.13.0)
|
15
|
+
rspec-core (3.13.1)
|
16
|
+
rspec-support (~> 3.13.0)
|
17
|
+
rspec-expectations (3.13.3)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.13.0)
|
20
|
+
rspec-mocks (3.13.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.13.0)
|
23
|
+
rspec-support (3.13.1)
|
21
24
|
|
22
25
|
PLATFORMS
|
23
26
|
x86_64-linux
|
24
27
|
|
25
28
|
DEPENDENCIES
|
26
29
|
currency_coin_converter!
|
27
|
-
dotenv
|
28
|
-
httparty
|
29
30
|
rake (~> 13.0)
|
31
|
+
rspec
|
30
32
|
|
31
33
|
BUNDLED WITH
|
32
34
|
2.4.10
|
data/README.md
CHANGED
@@ -19,10 +19,11 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
19
19
|
$ gem install currency_coin_converter
|
20
20
|
|
21
21
|
## Usage
|
22
|
-
To use this gem, you need to set up an
|
22
|
+
To use this gem, you need to set up an initializer for the Exchange Rate API key:
|
23
23
|
|
24
|
-
```
|
25
|
-
|
24
|
+
```rb
|
25
|
+
# This should be placed before the application tries to get the exchange rate.
|
26
|
+
CurrencyCoinConverter::Base.api_key = <your exchange rate api key>
|
26
27
|
```
|
27
28
|
<br>
|
28
29
|
|
@@ -30,7 +31,7 @@ In your main file you can use
|
|
30
31
|
```
|
31
32
|
require 'currency_coin_converter'
|
32
33
|
|
33
|
-
amount_in_euros = CurrencyCoinConverter.convert(100, from: "USD", to: "EUR")
|
34
|
+
amount_in_euros = CurrencyCoinConverter::Base.convert(100, from: "USD", to: "EUR", round: 3)
|
34
35
|
puts amount_in_euros
|
35
36
|
```
|
36
37
|
|
data/bin/console
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require "bundler/setup"
|
5
|
-
require "
|
5
|
+
require "currency_coin_converter"
|
6
6
|
|
7
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
8
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "lib/currency_coin_converter
|
3
|
+
require_relative "lib/currency_coin_converter"
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "currency_coin_converter"
|
@@ -16,11 +16,9 @@ Gem::Specification.new do |spec|
|
|
16
16
|
|
17
17
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
18
|
|
19
|
-
# URL do projeto no GitHub
|
20
19
|
spec.metadata["homepage_uri"] = spec.homepage
|
21
20
|
spec.metadata["source_code_uri"] = 'https://github.com/bussularf/currency_coin_converter'
|
22
21
|
|
23
|
-
# Arquivos incluídos na gem
|
24
22
|
spec.files = Dir.chdir(__dir__) do
|
25
23
|
`git ls-files -z`.split("\x0").reject do |f|
|
26
24
|
f.end_with?('currency_converter.gemspec') || f.end_with?('.gem')
|
@@ -30,7 +28,4 @@ Gem::Specification.new do |spec|
|
|
30
28
|
spec.bindir = "exe"
|
31
29
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
30
|
spec.require_paths = ["lib"]
|
33
|
-
|
34
|
-
# Dependências, se houver
|
35
|
-
spec.add_dependency "httparty", "~> 0.18" # Exemplo de dependência para chamadas HTTP
|
36
31
|
end
|
@@ -1,29 +1,79 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
require '
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
require 'singleton'
|
4
|
+
require 'uri'
|
4
5
|
|
5
6
|
module CurrencyCoinConverter
|
7
|
+
VERSION = "0.3.2"
|
8
|
+
|
6
9
|
class Error < StandardError; end
|
7
10
|
|
8
|
-
|
11
|
+
class Base
|
12
|
+
include ::Singleton
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
14
|
+
def initialize
|
15
|
+
self.api_version = "v6"
|
16
|
+
end
|
13
17
|
|
14
|
-
|
15
|
-
(amount * conversion_rate).round(
|
16
|
-
else
|
17
|
-
raise "Conversão de #{from} para #{to} não encontrada."
|
18
|
+
def convert(amount, from:, to:, round: 2)
|
19
|
+
(amount * conversion_rate(from:, to:)).round(round)
|
18
20
|
end
|
19
|
-
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
def conversion_rate(from:, to:)
|
23
|
+
conversion_rate = conversion_rates(from)[to]
|
24
|
+
return conversion_rate unless conversion_rate.nil?
|
25
|
+
|
26
|
+
raise Error, "currency code is invalid: #{to}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def conversion_rates(base_currency)
|
30
|
+
get(path: base_currency)["conversion_rates"]
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_accessor :api_key
|
34
|
+
attr_reader :api_version
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
attr_writer :api_version
|
39
|
+
|
40
|
+
def get(path:)
|
41
|
+
raise Error, "api key should be present" if api_key.strip.empty?
|
42
|
+
raise Error, "api version should be present" if api_version.strip.empty?
|
43
|
+
|
44
|
+
uri = ::URI::HTTPS.build(host: "#{api_version}.exchangerate-api.com", path: "/#{api_version}/#{api_key}/latest/#{path}")
|
45
|
+
response = ::Net::HTTP.get_response(uri)
|
46
|
+
parsed_response = JSON.parse(response.body)
|
47
|
+
|
48
|
+
return parsed_response if response.is_a?(::Net::HTTPSuccess)
|
49
|
+
|
50
|
+
raise Error, "#{parsed_response["error-type"]}: could not complete the request."
|
51
|
+
end
|
52
|
+
|
53
|
+
class << self
|
54
|
+
def api_key
|
55
|
+
instance.api_key
|
56
|
+
end
|
57
|
+
|
58
|
+
def api_key=(api_key)
|
59
|
+
instance.api_key = api_key
|
60
|
+
end
|
61
|
+
|
62
|
+
def api_version
|
63
|
+
instance.api_version
|
64
|
+
end
|
65
|
+
|
66
|
+
def convert(amount, from:, to:, round: 2)
|
67
|
+
instance.convert(amount, from:, to:, round: round)
|
68
|
+
end
|
69
|
+
|
70
|
+
def conversion_rate(from:, to:)
|
71
|
+
instance.conversion_rate(from:, to:)
|
72
|
+
end
|
73
|
+
|
74
|
+
def conversion_rates(base_currency)
|
75
|
+
instance.conversion_rates(base_currency)
|
76
|
+
end
|
27
77
|
end
|
28
78
|
end
|
29
79
|
end
|
@@ -1,15 +1,42 @@
|
|
1
1
|
require 'currency_coin_converter'
|
2
2
|
|
3
|
-
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
RSpec.describe CurrencyCoinConverter::Base do
|
4
6
|
before do
|
5
|
-
|
7
|
+
described_class.api_key = "fake_key"
|
8
|
+
end
|
9
|
+
|
10
|
+
it '#api_key returns the set api key' do
|
11
|
+
expect(described_class.api_key).to eq("fake_key")
|
12
|
+
end
|
13
|
+
|
14
|
+
it '#api_version returns the initialized api version' do
|
15
|
+
expect(described_class.api_version).to eq("v6")
|
16
|
+
end
|
17
|
+
|
18
|
+
it '#conversion_rates returns the conversion rate for a valid currency code' do
|
19
|
+
response = JSON.parse(File.read("spec/examples/inr.json"))
|
20
|
+
allow_any_instance_of(described_class).to receive(:get).with(path: "INR").and_return(response)
|
21
|
+
|
22
|
+
expect(described_class.conversion_rates("INR")).to eq(response["conversion_rates"])
|
23
|
+
end
|
24
|
+
|
25
|
+
it '#conversion_rate returns the conversion rate for valid currency codes' do
|
26
|
+
response = JSON.parse(File.read("spec/examples/inr.json"))
|
27
|
+
allow_any_instance_of(described_class).to receive(:get).with(path: "INR").and_return(response)
|
28
|
+
|
29
|
+
expect(described_class.conversion_rate(from: "INR", to: "USD")).to eq(response["conversion_rates"]["USD"])
|
30
|
+
expect { described_class.conversion_rate(from: "INR", to: "US") }.to raise_error CurrencyCoinConverter::Error, "currency code is invalid: US"
|
6
31
|
end
|
7
32
|
|
8
|
-
it
|
9
|
-
|
33
|
+
it '#convert returns the converted amount from one currency code to another' do
|
34
|
+
response = JSON.parse(File.read("spec/examples/inr.json"))
|
35
|
+
allow_any_instance_of(described_class).to receive(:get).with(path: "INR").and_return(response)
|
10
36
|
|
11
|
-
|
12
|
-
expect(
|
37
|
+
expect(described_class.convert(130, from: "INR", to: "USD")).to eq(1.55)
|
38
|
+
expect(described_class.convert(130, from: "INR", to: "USD", round: 1)).to eq(1.5)
|
39
|
+
expect(described_class.convert(130, from: "INR", to: "USD", round: 5)).to eq(1.547)
|
13
40
|
end
|
14
41
|
end
|
15
42
|
|
@@ -0,0 +1,174 @@
|
|
1
|
+
{
|
2
|
+
"result":"success",
|
3
|
+
"documentation":"https://www.exchangerate-api.com/docs",
|
4
|
+
"terms_of_use":"https://www.exchangerate-api.com/terms",
|
5
|
+
"time_last_update_unix":1726012802,
|
6
|
+
"time_last_update_utc":"Wed, 11 Sep 2024 00:00:02 +0000",
|
7
|
+
"time_next_update_unix":1726099202,
|
8
|
+
"time_next_update_utc":"Thu, 12 Sep 2024 00:00:02 +0000",
|
9
|
+
"base_code":"INR",
|
10
|
+
"conversion_rates":{
|
11
|
+
"INR":1,
|
12
|
+
"AED":0.04372,
|
13
|
+
"AFN":0.8358,
|
14
|
+
"ALL":1.0753,
|
15
|
+
"AMD":4.6129,
|
16
|
+
"ANG":0.02131,
|
17
|
+
"AOA":11.2097,
|
18
|
+
"ARS":11.4069,
|
19
|
+
"AUD":0.01788,
|
20
|
+
"AWG":0.02131,
|
21
|
+
"AZN":0.02025,
|
22
|
+
"BAM":0.02112,
|
23
|
+
"BBD":0.02381,
|
24
|
+
"BDT":1.4227,
|
25
|
+
"BGN":0.02112,
|
26
|
+
"BHD":0.004476,
|
27
|
+
"BIF":34.6000,
|
28
|
+
"BMD":0.01190,
|
29
|
+
"BND":0.01554,
|
30
|
+
"BOB":0.08246,
|
31
|
+
"BRL":0.06652,
|
32
|
+
"BSD":0.01190,
|
33
|
+
"BTN":1.0000,
|
34
|
+
"BWP":0.1593,
|
35
|
+
"BYN":0.03848,
|
36
|
+
"BZD":0.02381,
|
37
|
+
"CAD":0.01618,
|
38
|
+
"CDF":33.9533,
|
39
|
+
"CHF":0.01009,
|
40
|
+
"CLP":11.2497,
|
41
|
+
"CNY":0.08477,
|
42
|
+
"COP":50.4925,
|
43
|
+
"CRC":6.2304,
|
44
|
+
"CUP":0.2857,
|
45
|
+
"CVE":1.1906,
|
46
|
+
"CZK":0.2705,
|
47
|
+
"DJF":2.1158,
|
48
|
+
"DKK":0.08057,
|
49
|
+
"DOP":0.7139,
|
50
|
+
"DZD":1.5804,
|
51
|
+
"EGP":0.5758,
|
52
|
+
"ERN":0.1786,
|
53
|
+
"ETB":1.3612,
|
54
|
+
"EUR":0.01080,
|
55
|
+
"FJD":0.02647,
|
56
|
+
"FKP":0.009105,
|
57
|
+
"FOK":0.08058,
|
58
|
+
"GBP":0.009104,
|
59
|
+
"GEL":0.03206,
|
60
|
+
"GGP":0.009105,
|
61
|
+
"GHS":0.1897,
|
62
|
+
"GIP":0.009105,
|
63
|
+
"GMD":0.8401,
|
64
|
+
"GNF":103.2731,
|
65
|
+
"GTQ":0.09217,
|
66
|
+
"GYD":2.4918,
|
67
|
+
"HKD":0.09284,
|
68
|
+
"HNL":0.2956,
|
69
|
+
"HRK":0.08135,
|
70
|
+
"HTG":1.5707,
|
71
|
+
"HUF":4.2876,
|
72
|
+
"IDR":184.0541,
|
73
|
+
"ILS":0.04481,
|
74
|
+
"IMP":0.009105,
|
75
|
+
"IQD":15.5923,
|
76
|
+
"IRR":505.6787,
|
77
|
+
"ISK":1.6439,
|
78
|
+
"JEP":0.009105,
|
79
|
+
"JMD":1.8688,
|
80
|
+
"JOD":0.008441,
|
81
|
+
"JPY":1.7008,
|
82
|
+
"KES":1.5373,
|
83
|
+
"KGS":1.0105,
|
84
|
+
"KHR":48.4400,
|
85
|
+
"KID":0.01789,
|
86
|
+
"KMF":5.3120,
|
87
|
+
"KRW":15.9849,
|
88
|
+
"KWD":0.003637,
|
89
|
+
"KYD":0.009921,
|
90
|
+
"KZT":5.6804,
|
91
|
+
"LAK":260.9012,
|
92
|
+
"LBP":1065.4911,
|
93
|
+
"LKR":3.5819,
|
94
|
+
"LRD":2.3242,
|
95
|
+
"LSL":0.2135,
|
96
|
+
"LYD":0.05674,
|
97
|
+
"MAD":0.1161,
|
98
|
+
"MDL":0.2071,
|
99
|
+
"MGA":54.2239,
|
100
|
+
"MKD":0.6600,
|
101
|
+
"MMK":33.8932,
|
102
|
+
"MNT":40.0564,
|
103
|
+
"MOP":0.09563,
|
104
|
+
"MRU":0.4733,
|
105
|
+
"MUR":0.5495,
|
106
|
+
"MVR":0.1839,
|
107
|
+
"MWK":20.7582,
|
108
|
+
"MXN":0.2382,
|
109
|
+
"MYR":0.05172,
|
110
|
+
"MZN":0.7610,
|
111
|
+
"NAD":0.2135,
|
112
|
+
"NGN":19.5736,
|
113
|
+
"NIO":0.4385,
|
114
|
+
"NOK":0.1289,
|
115
|
+
"NPR":1.6000,
|
116
|
+
"NZD":0.01935,
|
117
|
+
"OMR":0.004577,
|
118
|
+
"PAB":0.01190,
|
119
|
+
"PEN":0.04543,
|
120
|
+
"PGK":0.04649,
|
121
|
+
"PHP":0.6708,
|
122
|
+
"PKR":3.3198,
|
123
|
+
"PLN":0.04623,
|
124
|
+
"PYG":91.5037,
|
125
|
+
"QAR":0.04333,
|
126
|
+
"RON":0.05368,
|
127
|
+
"RSD":1.2635,
|
128
|
+
"RUB":1.0843,
|
129
|
+
"RWF":16.1947,
|
130
|
+
"SAR":0.04464,
|
131
|
+
"SBD":0.1004,
|
132
|
+
"SCR":0.1613,
|
133
|
+
"SDG":5.3270,
|
134
|
+
"SEK":0.1236,
|
135
|
+
"SGD":0.01554,
|
136
|
+
"SHP":0.009105,
|
137
|
+
"SLE":0.2680,
|
138
|
+
"SLL":268.0041,
|
139
|
+
"SOS":6.8034,
|
140
|
+
"SRD":0.3491,
|
141
|
+
"SSP":39.7604,
|
142
|
+
"STN":0.2645,
|
143
|
+
"SYP":153.2201,
|
144
|
+
"SZL":0.2135,
|
145
|
+
"THB":0.4017,
|
146
|
+
"TJS":0.1263,
|
147
|
+
"TMT":0.04168,
|
148
|
+
"TND":0.03639,
|
149
|
+
"TOP":0.02777,
|
150
|
+
"TRY":0.4056,
|
151
|
+
"TTD":0.08086,
|
152
|
+
"TVD":0.01789,
|
153
|
+
"TWD":0.3824,
|
154
|
+
"TZS":32.3728,
|
155
|
+
"UAH":0.4903,
|
156
|
+
"UGX":44.2520,
|
157
|
+
"USD":0.01190,
|
158
|
+
"UYU":0.4810,
|
159
|
+
"UZS":150.3032,
|
160
|
+
"VES":0.4370,
|
161
|
+
"VND":298.5911,
|
162
|
+
"VUV":1.4093,
|
163
|
+
"WST":0.03221,
|
164
|
+
"XAF":7.0827,
|
165
|
+
"XCD":0.03214,
|
166
|
+
"XDR":0.008858,
|
167
|
+
"XOF":7.0827,
|
168
|
+
"XPF":1.2885,
|
169
|
+
"YER":2.9822,
|
170
|
+
"ZAR":0.2135,
|
171
|
+
"ZMW":0.3146,
|
172
|
+
"ZWL":0.1660
|
173
|
+
}
|
174
|
+
}
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: currency_coin_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fernanda de Jesus
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: httparty
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0.18'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0.18'
|
11
|
+
date: 2024-09-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
27
13
|
description: This gem allows you to convert amounts between different currencies using
|
28
14
|
the ExchangeRate API.
|
29
15
|
email:
|
@@ -44,9 +30,8 @@ files:
|
|
44
30
|
- bin/setup
|
45
31
|
- currency_coin_converter.gemspec
|
46
32
|
- lib/currency_coin_converter.rb
|
47
|
-
- lib/currency_coin_converter/version.rb
|
48
|
-
- sig/currency_coin_converter.rbs
|
49
33
|
- spec/currency_coin_converter_spec.rb
|
34
|
+
- spec/examples/inr.json
|
50
35
|
homepage: https://github.com/bussularf/currency_coin_converter
|
51
36
|
licenses:
|
52
37
|
- MIT
|