apilayer 0.9.0 → 1.0.0
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/README.rdoc +64 -0
- data/lib/apilayer/connection_helper.rb +17 -0
- data/lib/apilayer/currency.rb +22 -12
- data/lib/apilayer/error.rb +2 -0
- data/lib/apilayer/vat.rb +32 -5
- metadata +44 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 741fe800b4155a1935d910a84c38bcae92be05cf
|
4
|
+
data.tar.gz: 6eb86705872f060ee900f3aeb286f92c5abf5e2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bca4854b31225c90b787f3d63bed8134a0bc13ab4ef262f4b99ad76f5a898208e91b9439c01329c1b5e06f2b4b2bf264d20a98b880c8240f9acf5b8f704414b0
|
7
|
+
data.tar.gz: a37bfb3f5e8923a56ce13cbd99e3c005fe3cc34c38b7b6fdd73c649f6e3575549e062afd6cad651f299ab57648ef63e2e7bf72f681af50b12d5743933bb2e58c
|
data/README.rdoc
CHANGED
@@ -0,0 +1,64 @@
|
|
1
|
+
== apilayer {<img src="https://secure.travis-ci.org/actfong/apilayer.png"/>}[http://travis-ci.org/actfong/apilayer] {<img src="https://codeclimate.com/github/actfong/apilayer.png"/>}[https://codeclimate.com/github/actfong/apilayer] {<img src="https://badge.fury.io/rb/apilayer.svg" alt="Gem Version" />}[https://badge.fury.io/rb/apilayer]
|
2
|
+
|
3
|
+
Ruby wrapper for various services of apilayer.
|
4
|
+
See http://apilayer.com for more details.
|
5
|
+
|
6
|
+
=== Version 1.0
|
7
|
+
For this version, only the API-features that are available to the free accounts are supported. See the documentation of http://currencylayer.com and http://vatlayer.com for more details.
|
8
|
+
|
9
|
+
=== Installation
|
10
|
+
|
11
|
+
==== Using Bundler
|
12
|
+
|
13
|
+
Add apilayer in your <tt>Gemfile</tt>:
|
14
|
+
|
15
|
+
gem "apilayer"
|
16
|
+
|
17
|
+
Run the following in your console:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
=== Usage
|
22
|
+
|
23
|
+
==== Set up Apilayer
|
24
|
+
Once you sign up for a service of *apilayer*, you will receive an access_key. Please pay attention that different services of apilayer (such as *vatlayer* and *currencylayer*) have different access keys.
|
25
|
+
|
26
|
+
Then you can use your access_key(s) to configure your Apilayer module like this:
|
27
|
+
|
28
|
+
Apilayer.configure do |configs|
|
29
|
+
configs.vat_key = "my_vatlayer_access_key_123"
|
30
|
+
configs.currency_key = "my_currencylayer_access_key_123"
|
31
|
+
end
|
32
|
+
|
33
|
+
Please note that you don't need to assign keys for both services. You only need to assign the key for the service you have signed up for.
|
34
|
+
|
35
|
+
Once that is done, you are ready to go
|
36
|
+
|
37
|
+
==== currencylayer
|
38
|
+
After setting the access_key for *currencylayer*, you can use Apilayer::Currency to call *currencylayer*'s API
|
39
|
+
Apilayer::Currency.live
|
40
|
+
Apilayer::Currency.live("EUR", "GBP", "CHF")
|
41
|
+
|
42
|
+
Apilayer::Currency.historical("2016-01-01")
|
43
|
+
Apilayer::Currency.historical("2016-01-01", "EUR", "GBP", "CHF")
|
44
|
+
|
45
|
+
==== vatlayer
|
46
|
+
|
47
|
+
After setting the access_key for *vatlayer*, you can use Apilayer::Vat to call *vatlayer*'s API
|
48
|
+
|
49
|
+
Apilayer::Vat.validate("LU26375245")
|
50
|
+
Apilayer::Vat.rate(:country_code, "NL")
|
51
|
+
Apilayer::Vat.rate(:ip_address, "176.249.153.36")
|
52
|
+
Apilayer::Vat.rate_list
|
53
|
+
Apilayer::Vat.price(100, :country, "NL")
|
54
|
+
Apilayer::Vat.price(100, :ip_address, "176.249.153.36")
|
55
|
+
|
56
|
+
==== Re-Configure Apilayer's access_keys
|
57
|
+
If you happened to have forgotten to provide an access_key or entered an incorrect one, you can re-configure your Apilayer module by:
|
58
|
+
|
59
|
+
# Example: reconfigure access_key for vatlayer
|
60
|
+
Apilayer::Vatlayer.reset_connection
|
61
|
+
Apilayer.configure do |configs|
|
62
|
+
configs.vat_key = "my_valid_key_123"
|
63
|
+
end
|
64
|
+
|
@@ -1,15 +1,29 @@
|
|
1
1
|
module Apilayer
|
2
2
|
module ConnectionHelper
|
3
3
|
|
4
|
+
##
|
5
|
+
# Creates a connection for the extended module to an apilayer-service, such as currencylayer and vatlayer.
|
6
|
+
# Uses access_key(s) configured with Apilayer module.
|
7
|
+
def connection
|
8
|
+
@connection ||= ::Faraday.new(:url => 'http://apilayer.net',
|
9
|
+
:params => {"access_key" => Apilayer.configs[self.const_get(:APILAYER_CONFIG_KEY)]})
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Resets the connection for the extended module. Used when the user needs to re-enter his/her access_key(s)
|
4
14
|
def reset_connection
|
5
15
|
@connection = nil
|
6
16
|
end
|
7
17
|
|
18
|
+
##
|
19
|
+
# Makes a get-request to apilayer's service and parses the JSON response into a Hash
|
8
20
|
def get_and_parse(url, params={})
|
9
21
|
resp = get_request(url, params)
|
10
22
|
parse_response(resp)
|
11
23
|
end
|
12
24
|
|
25
|
+
##
|
26
|
+
# Makes a get-request to apilayer's service
|
13
27
|
def get_request(url, params={})
|
14
28
|
# calls connection method on the extended module
|
15
29
|
connection.get do |req|
|
@@ -20,6 +34,9 @@ module Apilayer
|
|
20
34
|
end
|
21
35
|
end
|
22
36
|
|
37
|
+
##
|
38
|
+
# Parses the JSON response from apilayer into a Hash
|
39
|
+
# When errors are returned by apilayer, the 'info' and 'code' from its error will be used to raise an Apilayer::Error
|
23
40
|
def parse_response(resp)
|
24
41
|
body = JSON.parse(resp.body)
|
25
42
|
# According to documentation, currencylayer has a "success" field
|
data/lib/apilayer/currency.rb
CHANGED
@@ -1,14 +1,23 @@
|
|
1
|
+
##
|
2
|
+
# Ruby wrapper for currencylayer. See https://currencylayer.com/documentation for more info
|
1
3
|
module Apilayer
|
2
4
|
module Currency
|
3
5
|
extend ConnectionHelper
|
4
6
|
|
5
|
-
|
7
|
+
##
|
8
|
+
# Determines which access_key in Apilayer.configs to use
|
9
|
+
# in order to to make a connection to currencylayer
|
10
|
+
APILAYER_CONFIG_KEY = :currency_key
|
6
11
|
|
7
|
-
|
8
|
-
|
9
|
-
:params => {"access_key" => Apilayer.configs[:currency_key]})
|
10
|
-
end
|
12
|
+
### API methods
|
13
|
+
#
|
11
14
|
|
15
|
+
##
|
16
|
+
# Api-Method: Calls the /live endpoint to get real-time exchange rates.
|
17
|
+
# When no currency-codes are specified, it will return all exchange rates for your source-currency.
|
18
|
+
# Example:
|
19
|
+
# Apilayer::Currency.live
|
20
|
+
# Apilayer::Currency.live("EUR", "GBP", "CHF")
|
12
21
|
def self.live(*currencies)
|
13
22
|
if currencies.any?
|
14
23
|
currencies_str = join_by_commas(currencies)
|
@@ -19,19 +28,20 @@ module Apilayer
|
|
19
28
|
end
|
20
29
|
end
|
21
30
|
|
31
|
+
##
|
32
|
+
# Api-Method: Calls the /historical endpoint to get exchange rates for a specific date.
|
33
|
+
# When no currency-codes are specified, it will return all exchange rates for your source-currency.
|
34
|
+
# Example:
|
35
|
+
# Apilayer::Currency.historical("2016-01-01")
|
36
|
+
# Apilayer::Currency.historical("2016-01-01", "EUR", "GBP", "CHF")
|
22
37
|
def self.historical(date, *currencies)
|
23
38
|
params = {:date => date}
|
24
39
|
params.merge!(:currencies => join_by_commas(currencies)) if currencies.any?
|
25
40
|
get_and_parse("historical", params)
|
26
41
|
end
|
27
42
|
|
28
|
-
|
29
|
-
|
30
|
-
params = {:from => from, :to => to, :amount => amount}
|
31
|
-
params.merge!(:date => date) if date
|
32
|
-
get_and_parse("convert", params)
|
33
|
-
end
|
34
|
-
=end
|
43
|
+
##
|
44
|
+
# Joins currencies in an array as a comma-separated-string
|
35
45
|
def self.join_by_commas(currencies)
|
36
46
|
currencies.map(&:strip).join(",")
|
37
47
|
end
|
data/lib/apilayer/error.rb
CHANGED
data/lib/apilayer/vat.rb
CHANGED
@@ -1,36 +1,63 @@
|
|
1
|
+
##
|
2
|
+
# Ruby wrapper for vatlayer. See https://vatlayer.com/documentation for more info
|
1
3
|
module Apilayer
|
2
4
|
module Vat
|
3
5
|
extend ConnectionHelper
|
4
6
|
|
5
7
|
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
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
##
|
10
|
+
# Determines which access_key in Apilayer.configs to use
|
11
|
+
# in order to make a connection to vatlayer
|
12
|
+
APILAYER_CONFIG_KEY = :vat_key
|
12
13
|
|
14
|
+
##
|
15
|
+
# Validates whether a supported criteria has been provided to .rate and .price
|
13
16
|
def self.validate_country_criteria(criteria)
|
14
17
|
unless [:country_code, :ip_address].include? criteria
|
15
18
|
raise Apilayer::Error.new COUNTRY_CRITERIA_MISSING_MSG
|
16
19
|
end
|
17
20
|
end
|
18
21
|
|
22
|
+
### API methods
|
23
|
+
#
|
24
|
+
|
25
|
+
##
|
26
|
+
# Api-Method: Calls the /validate endpoint to validate a given VAT-number.
|
27
|
+
# Example:
|
28
|
+
# Apilayer::Vat.validate("LU26375245")
|
19
29
|
def self.validate(vat_number)
|
20
30
|
params = {:vat_number => vat_number}
|
21
31
|
get_and_parse("validate", params)
|
22
32
|
end
|
23
33
|
|
34
|
+
##
|
35
|
+
# Api-Method: Calls the /rate endpoint to get the VAT-rate of a given country,
|
36
|
+
# based on country-code or ip-address.
|
37
|
+
# Example:
|
38
|
+
# Apilayer::Vat.rate(:country_code, "NL")
|
39
|
+
# Apilayer::Vat.rate(:ip_address, "176.249.153.36")
|
24
40
|
def self.rate(criteria, value)
|
25
41
|
validate_country_criteria(criteria)
|
26
42
|
params = {criteria.to_sym => value}
|
27
43
|
get_and_parse("rate", params)
|
28
44
|
end
|
29
45
|
|
46
|
+
##
|
47
|
+
# Api-Method: Calls the /rate_list endpoint to get the standard and reduced VAT-rates
|
48
|
+
# for all EU countries.
|
49
|
+
# Example:
|
50
|
+
# Apilayer::Vat.rate_list
|
30
51
|
def self.rate_list
|
31
52
|
get_and_parse("rate_list")
|
32
53
|
end
|
33
54
|
|
55
|
+
##
|
56
|
+
# Api-Method: Calls the /price endpoint to get price including and excluding VAT
|
57
|
+
# for a given price and country. It also returns the VAT rate for that country
|
58
|
+
# Example:
|
59
|
+
# Apilayer::Vat.price(100, :country, "NL")
|
60
|
+
# Apilayer::Vat.price(100, :ip_address, "176.249.153.36")
|
34
61
|
def self.price(price, criteria, value)
|
35
62
|
validate_country_criteria(criteria)
|
36
63
|
params = {:amount => price, criteria.to_sym => value}
|
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: 1.0.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-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -50,6 +50,26 @@ dependencies:
|
|
50
50
|
- - ">="
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: 0.9.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rake
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '10.1'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.10.1
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '10.1'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 0.10.1
|
53
73
|
- !ruby/object:Gem::Dependency
|
54
74
|
name: pry
|
55
75
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,6 +130,26 @@ dependencies:
|
|
110
130
|
- - ">="
|
111
131
|
- !ruby/object:Gem::Version
|
112
132
|
version: 3.0.0
|
133
|
+
- !ruby/object:Gem::Dependency
|
134
|
+
name: simplecov
|
135
|
+
requirement: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - "~>"
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0.11'
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: 0.11.0
|
143
|
+
type: :development
|
144
|
+
prerelease: false
|
145
|
+
version_requirements: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - "~>"
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0.11'
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 0.11.0
|
113
153
|
- !ruby/object:Gem::Dependency
|
114
154
|
name: vcr
|
115
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -190,6 +230,6 @@ rubyforge_project:
|
|
190
230
|
rubygems_version: 2.4.5
|
191
231
|
signing_key:
|
192
232
|
specification_version: 4
|
193
|
-
summary: Ruby wrapper for
|
194
|
-
|
233
|
+
summary: Ruby wrapper for currencylayer and vatlayer from apilayer.com. For more info,
|
234
|
+
see http://apilayer.com
|
195
235
|
test_files: []
|