apilayer 0.0.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/LICENSE +19 -0
- data/README.rdoc +0 -0
- data/Rakefile +5 -0
- data/lib/apilayer.rb +3 -2
- data/lib/apilayer/connection_helper.rb +39 -0
- data/lib/apilayer/currency.rb +24 -26
- data/lib/apilayer/error.rb +10 -0
- data/lib/apilayer/vat.rb +22 -32
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c90421964d0e4f1e87b8dd237cc9fd9829828d4
|
4
|
+
data.tar.gz: 0d134f0f06cfa93e4c1aeb7bab7cebf2b48f4d4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc9197e568e72415aca957bff9eebcdcac94be67a5e17c5b6d78822808c4828675eadbf1efb1726693fd309d1cbce578cc43746ef5905fce46924a90681cbdc3
|
7
|
+
data.tar.gz: 5559cc9b38fbdf4f34b92727b58c3d03f6b3222b5ade671a25708596e2f573a1a938c45786af2f77fc3a9871bfb1260a33f7f8bd62bd64b5204b51545fb122d0
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2016 Alex Fong http://www.github.com/actfong
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.rdoc
ADDED
File without changes
|
data/Rakefile
ADDED
data/lib/apilayer.rb
CHANGED
@@ -16,7 +16,7 @@ module Apilayer
|
|
16
16
|
@configs ||= init_configs
|
17
17
|
end
|
18
18
|
|
19
|
-
def self.reset
|
19
|
+
def self.reset!
|
20
20
|
@configs = init_configs
|
21
21
|
end
|
22
22
|
|
@@ -25,6 +25,7 @@ module Apilayer
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
require "apilayer/connection_helper"
|
28
29
|
require "apilayer/currency"
|
29
30
|
require "apilayer/vat"
|
30
|
-
require "apilayer/error"
|
31
|
+
require "apilayer/error"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Apilayer
|
2
|
+
module ConnectionHelper
|
3
|
+
|
4
|
+
def reset_connection
|
5
|
+
@connection = nil
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_and_parse(url, params={})
|
9
|
+
resp = get_request(url, params)
|
10
|
+
parse_response(resp)
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_request(url, params={})
|
14
|
+
# calls connection method on the extended module
|
15
|
+
connection.get do |req|
|
16
|
+
req.url "api/#{url}"
|
17
|
+
params.each_pair do |k,v|
|
18
|
+
req.params[k] = v
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse_response(resp)
|
24
|
+
body = JSON.parse(resp.body)
|
25
|
+
# According to documentation, currencylayer has a "success" field
|
26
|
+
# while vatlayer has a "valid" field to indicate whether the request was succesful or not.
|
27
|
+
# However, for both layers, an unsuccesful request would contain an "error" field.
|
28
|
+
# That's why the presence of "error" is chosen to determine whether we should raise an error or not.
|
29
|
+
if body['error']
|
30
|
+
raise Apilayer::Error.new(
|
31
|
+
body['error']['info'],
|
32
|
+
body['error']['code']
|
33
|
+
)
|
34
|
+
else
|
35
|
+
body
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/apilayer/currency.rb
CHANGED
@@ -1,42 +1,40 @@
|
|
1
1
|
module Apilayer
|
2
|
-
|
3
2
|
module Currency
|
3
|
+
extend ConnectionHelper
|
4
|
+
|
5
|
+
CURRENCYLAYER_KEY_MISSING_MSG = "Please configure access_key for currency_layer first!"
|
4
6
|
|
5
7
|
def self.connection
|
6
|
-
|
7
|
-
|
8
|
-
else
|
9
|
-
@connection ||= ::Faraday.new(:url => 'http://apilayer.net',
|
10
|
-
:params => {"access_key" => Apilayer.configs[:currency_key]})
|
11
|
-
end
|
8
|
+
@connection ||= ::Faraday.new(:url => 'http://apilayer.net',
|
9
|
+
:params => {"access_key" => Apilayer.configs[:currency_key]})
|
12
10
|
end
|
13
11
|
|
14
12
|
def self.live(*currencies)
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
if currencies.any?
|
14
|
+
currencies_str = join_by_commas(currencies)
|
15
|
+
params = {:currencies => currencies_str}
|
16
|
+
get_and_parse("live", params)
|
17
|
+
else
|
18
|
+
get_and_parse("live")
|
18
19
|
end
|
19
|
-
JSON.parse(resp.body)
|
20
20
|
end
|
21
21
|
|
22
|
-
def self.historical(date, *currencies)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
req.params['currencies'] = currencies.map(&:strip).join(",") if currencies.any?
|
27
|
-
end
|
28
|
-
JSON.parse(resp.body)
|
22
|
+
def self.historical(date, *currencies)
|
23
|
+
params = {:date => date}
|
24
|
+
params.merge!(:currencies => join_by_commas(currencies)) if currencies.any?
|
25
|
+
get_and_parse("historical", params)
|
29
26
|
end
|
30
27
|
|
28
|
+
=begin
|
31
29
|
def self.convert(from, to, amount, date=nil)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
req.params['to'] = to
|
36
|
-
req.params['amount'] = amount
|
37
|
-
req.params['date'] = date if date
|
38
|
-
end
|
39
|
-
JSON.parse(resp.body)
|
30
|
+
params = {:from => from, :to => to, :amount => amount}
|
31
|
+
params.merge!(:date => date) if date
|
32
|
+
get_and_parse("convert", params)
|
40
33
|
end
|
34
|
+
=end
|
35
|
+
def self.join_by_commas(currencies)
|
36
|
+
currencies.map(&:strip).join(",")
|
37
|
+
end
|
38
|
+
|
41
39
|
end
|
42
40
|
end
|
data/lib/apilayer/vat.rb
CHANGED
@@ -1,51 +1,41 @@
|
|
1
1
|
module Apilayer
|
2
|
-
|
3
2
|
module Vat
|
3
|
+
extend ConnectionHelper
|
4
|
+
|
5
|
+
COUNTRY_CRITERIA_MISSING_MSG = "You must provide either :country_code or :ip_address"
|
6
|
+
VATLAYER_KEY_MISSING_MSG = "Please configure access_key for vat_layer first!"
|
7
|
+
|
4
8
|
def self.connection
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
@connection ||= ::Faraday.new(:url => 'http://apilayer.net',
|
10
|
+
:params => {"access_key" => Apilayer.configs[:vat_key]})
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.validate_country_criteria(criteria)
|
14
|
+
unless [:country_code, :ip_address].include? criteria
|
15
|
+
raise Apilayer::Error.new COUNTRY_CRITERIA_MISSING_MSG
|
10
16
|
end
|
11
17
|
end
|
12
18
|
|
13
19
|
def self.validate(vat_number)
|
14
|
-
|
15
|
-
|
16
|
-
req.params['vat_number'] = vat_number
|
17
|
-
end
|
18
|
-
JSON.parse(resp.body)
|
20
|
+
params = {:vat_number => vat_number}
|
21
|
+
get_and_parse("validate", params)
|
19
22
|
end
|
20
23
|
|
21
24
|
def self.rate(criteria, value)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
resp = connection.get do |req|
|
26
|
-
req.url 'api/rate'
|
27
|
-
req.params[criteria.to_s] = value
|
28
|
-
end
|
29
|
-
JSON.parse(resp.body)
|
25
|
+
validate_country_criteria(criteria)
|
26
|
+
params = {criteria.to_sym => value}
|
27
|
+
get_and_parse("rate", params)
|
30
28
|
end
|
31
29
|
|
32
30
|
def self.rate_list
|
33
|
-
|
34
|
-
req.url 'api/rate_list'
|
35
|
-
end
|
36
|
-
JSON.parse(resp.body)
|
31
|
+
get_and_parse("rate_list")
|
37
32
|
end
|
38
33
|
|
39
34
|
def self.price(price, criteria, value)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
resp = connection.get do |req|
|
44
|
-
req.url 'api/price'
|
45
|
-
req.params['amount'] = price
|
46
|
-
req.params[criteria.to_s] = value
|
47
|
-
end
|
48
|
-
JSON.parse(resp.body)
|
35
|
+
validate_country_criteria(criteria)
|
36
|
+
params = {:amount => price, criteria.to_sym => value}
|
37
|
+
get_and_parse("price", params)
|
49
38
|
end
|
39
|
+
|
50
40
|
end
|
51
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apilayer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Fong
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -158,8 +158,14 @@ executables: []
|
|
158
158
|
extensions: []
|
159
159
|
extra_rdoc_files: []
|
160
160
|
files:
|
161
|
+
- Gemfile
|
162
|
+
- LICENSE
|
163
|
+
- README.rdoc
|
164
|
+
- Rakefile
|
161
165
|
- lib/apilayer.rb
|
166
|
+
- lib/apilayer/connection_helper.rb
|
162
167
|
- lib/apilayer/currency.rb
|
168
|
+
- lib/apilayer/error.rb
|
163
169
|
- lib/apilayer/vat.rb
|
164
170
|
homepage: https://github.com/actfong/apilayer
|
165
171
|
licenses:
|