apilayer 1.5.0 → 2.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 +16 -80
- data/lib/apilayer.rb +0 -34
- data/lib/apilayer/connection_helper.rb +22 -8
- metadata +4 -6
- data/lib/apilayer/currency.rb +0 -158
- data/lib/apilayer/vat.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ece8c553f9af332d873b2c0577caf0ae7f899fab
|
4
|
+
data.tar.gz: 0fb25263dfad5bf7d1444f47d88b022cb173b216
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd5b9cd11fb91ca457ffbf67d21e4140504da160dcf1091ddfb0df5b035cbb098a934b75ad630d053ffccce7f9be2d5771af4d69ff4b2af0775e34902f7a6537
|
7
|
+
data.tar.gz: 7d955d30a368d56a418666651727c5f3c7d084c4aa4ce5d3ad5402b71059d6c2c26ca29aca896a49a1109e496eab086a6c3bb4a0b069905d829a4c77b11e24c6
|
data/README.rdoc
CHANGED
@@ -1,17 +1,26 @@
|
|
1
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
2
|
|
3
|
-
|
3
|
+
With the release of version 2.0, the apilayer gem now act as a dependency for the *currency_layer* and *vat_layer* gems.
|
4
|
+
|
5
|
+
Please see https://github.com/actfong/currency_layer and https://github.com/actfong/vat_layer
|
6
|
+
|
7
|
+
*apilayer* provides a common-interface to configure and create connections to apilayer.net
|
4
8
|
See http://apilayer.com for more details.
|
5
9
|
|
10
|
+
=== Version 2.0
|
11
|
+
Removed all code related to currencylayer (Apilayer::Currency) and vatlayer (Apilayer::Vat).
|
12
|
+
Now act as a dependency to currency_layer and vat_layer gems.
|
13
|
+
ConnectionHelper contains all code related to configuring and creating connections to apilayer.net
|
14
|
+
|
6
15
|
=== Version 1.5
|
7
|
-
Improved reconfiguration-mechanism in Apilayer.
|
8
|
-
Added HTTPS-option in Apilayer module.
|
9
|
-
With this release, all paid-features of vatlayer and currencylayer are fully covered.
|
16
|
+
- Improved reconfiguration-mechanism in Apilayer.
|
17
|
+
- Added HTTPS-option in Apilayer module.
|
18
|
+
- With this release, all paid-features of vatlayer and currencylayer are fully covered.
|
10
19
|
|
11
20
|
=== Version 1.2 - 1.4
|
12
|
-
Added .change and .list to Apilayer::Currency
|
13
|
-
Added .timeframe to Apilayer::Currency
|
14
|
-
Added .convert to Apilayer::Currency
|
21
|
+
- Added .change and .list to Apilayer::Currency
|
22
|
+
- Added .timeframe to Apilayer::Currency
|
23
|
+
- Added .convert to Apilayer::Currency
|
15
24
|
|
16
25
|
=== Version 1.1
|
17
26
|
- Major change to how .live and .historical in Apilayer::Currency are invoked.
|
@@ -30,78 +39,5 @@ Run the following in your console:
|
|
30
39
|
|
31
40
|
$ bundle install
|
32
41
|
|
33
|
-
=== Usage
|
34
|
-
|
35
|
-
==== Set up Apilayer
|
36
|
-
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.
|
37
|
-
|
38
|
-
Then you can use your access_key(s) to configure your Apilayer module like this:
|
39
|
-
|
40
|
-
Apilayer.configure do |configs|
|
41
|
-
configs.vat_key = "my_vatlayer_access_key_123"
|
42
|
-
configs.currency_key = "my_currencylayer_access_key_123"
|
43
|
-
configs.vat_https = false
|
44
|
-
configs.currency_https = true
|
45
|
-
end
|
46
|
-
|
47
|
-
Please note that you only need to set the access keys and https connections that you need.
|
48
|
-
If unset, these values are just nil.
|
49
|
-
|
50
|
-
You can always review you configurations with:
|
51
|
-
|
52
|
-
Apilayer.configs
|
53
|
-
|
54
|
-
Once your configurations are set, you are ready to go
|
55
|
-
|
56
|
-
==== currencylayer
|
57
|
-
After setting the access_key for *currencylayer*, you can use Apilayer::Currency to call *currencylayer*'s API
|
58
|
-
Apilayer::Currency.list
|
59
|
-
|
60
|
-
Apilayer::Currency.live
|
61
|
-
Apilayer::Currency.live(:currencies => %w[GBP, CHF])
|
62
|
-
Apilayer::Currency.live(:source => "EUR") # source-currency is USD by default
|
63
|
-
Apilayer::Currency.live(:source => "EUR", :currencies => %w[GBP, CHF])
|
64
|
-
|
65
|
-
Apilayer::Currency.historical("2016-01-01")
|
66
|
-
Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF])
|
67
|
-
Apilayer::Currency.historical(:source => "EUR") # source-currency is USD by default
|
68
|
-
Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF], :source => "EUR")
|
69
|
-
|
70
|
-
Apilayer::Currency.convert("EUR", "CHF", 100) # convert 100 EUR to CHF
|
71
|
-
Apilayer::Currency.convert("EUR", "CHF", 100, "2015-06-01") # based on specific date
|
72
|
-
|
73
|
-
Apilayer::Currency.timeframe("2016-01-01", "2016-06-01")
|
74
|
-
Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF])
|
75
|
-
Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :source => "EUR")
|
76
|
-
Apilayer::Currency.timeframe("2016-01-01", "2016-06-01", :currencies => %w[GBP CHF], :source => "EUR")
|
77
|
-
|
78
|
-
Apilayer::Currency.change
|
79
|
-
Apilayer::Currency.change("2016-01-01", "2016-03-01")
|
80
|
-
Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR")
|
81
|
-
Apilayer::Currency.change("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
|
82
|
-
Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR", :currencies => %w[GBP CHF])
|
83
|
-
Apilayer::Currency.change(nil, nil, {:source => "EUR"})
|
84
|
-
Apilayer::Currency.change(nil, nil, {:currencies => %w[GBP CHF]})
|
85
|
-
Apilayer::Currency.change(nil, nil, {:source => "EUR", :currencies => %w[GBP CHF]})
|
86
|
-
|
87
|
-
==== vatlayer
|
88
|
-
After setting the access_key for *vatlayer*, you can use Apilayer::Vat to call *vatlayer*'s API
|
89
|
-
|
90
|
-
Apilayer::Vat.validate("LU26375245")
|
91
|
-
Apilayer::Vat.rate(:country_code, "NL")
|
92
|
-
Apilayer::Vat.rate(:ip_address, "176.249.153.36")
|
93
|
-
Apilayer::Vat.rate_list
|
94
|
-
Apilayer::Vat.price(100, :country, "NL")
|
95
|
-
Apilayer::Vat.price(100, :ip_address, "176.249.153.36")
|
96
|
-
|
97
|
-
==== Re-Configure Apilayer
|
98
|
-
If you happened to have forgotten to set or entered an incorrect value, you can re-configure your Apilayer module by:
|
99
42
|
|
100
|
-
# Example: reconfigure https for vatlayer
|
101
|
-
Apilayer.configure do |configs|
|
102
|
-
configs.vat_https = true
|
103
|
-
end
|
104
43
|
|
105
|
-
The action above will not affect the other Apilayer configurations you have set previously.
|
106
|
-
When you try to connect to Apilayer again, it will create a new connection based on the values you provided.
|
107
|
-
|
data/lib/apilayer.rb
CHANGED
@@ -2,39 +2,5 @@ require 'rubygems'
|
|
2
2
|
require "faraday"
|
3
3
|
require "json"
|
4
4
|
|
5
|
-
module Apilayer
|
6
|
-
class << self
|
7
|
-
attr_writer :configs
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.init_configs
|
11
|
-
keys = Struct.new(:currency_key, :vat_key, :currency_https, :vat_https)
|
12
|
-
keys.new
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.configs
|
16
|
-
@configs ||= init_configs
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.reset!
|
20
|
-
@configs = init_configs
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.configure
|
24
|
-
reset_observers_connections
|
25
|
-
yield(configs)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
5
|
require "apilayer/connection_helper"
|
30
|
-
require "apilayer/currency"
|
31
|
-
require "apilayer/vat"
|
32
|
-
|
33
|
-
module Apilayer
|
34
|
-
OBSERVERS = [Apilayer::Currency, Apilayer::Vat]
|
35
|
-
def self.reset_observers_connections
|
36
|
-
OBSERVERS.each(&:reset_connection)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
6
|
require "apilayer/error"
|
@@ -1,18 +1,36 @@
|
|
1
1
|
module Apilayer
|
2
2
|
module ConnectionHelper
|
3
3
|
|
4
|
+
def init_configs
|
5
|
+
keys = Struct.new(:access_key, :https)
|
6
|
+
keys.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def configs
|
10
|
+
@configs ||= init_configs
|
11
|
+
end
|
12
|
+
|
13
|
+
def reset_configs!
|
14
|
+
@configs = init_configs
|
15
|
+
end
|
16
|
+
|
17
|
+
def configure(&block)
|
18
|
+
self.reset_connection
|
19
|
+
yield(configs)
|
20
|
+
end
|
21
|
+
|
4
22
|
##
|
5
23
|
# Creates a connection for the extended module to an apilayer-service, such as currencylayer and vatlayer.
|
6
24
|
# Uses access_key(s) configured with Apilayer module.
|
7
25
|
def connection
|
8
26
|
@connection ||= ::Faraday.new(
|
9
27
|
:url => "#{protocol}://apilayer.net",
|
10
|
-
:params => {"access_key" =>
|
28
|
+
:params => {"access_key" => self.configs[:access_key]}
|
11
29
|
)
|
12
30
|
end
|
13
31
|
|
14
32
|
def protocol
|
15
|
-
|
33
|
+
self.configs[:https] ? "https" : "http"
|
16
34
|
end
|
17
35
|
##
|
18
36
|
# Resets the connection for the extended module. Used when the user needs to re-enter his/her access_key(s)
|
@@ -29,10 +47,10 @@ module Apilayer
|
|
29
47
|
|
30
48
|
##
|
31
49
|
# Makes a get-request to apilayer's service
|
32
|
-
def get_request(
|
50
|
+
def get_request(slug, params={})
|
33
51
|
# calls connection method on the extended module
|
34
52
|
connection.get do |req|
|
35
|
-
req.url "api/#{
|
53
|
+
req.url "api/#{slug}"
|
36
54
|
params.each_pair do |k,v|
|
37
55
|
req.params[k] = v
|
38
56
|
end
|
@@ -44,10 +62,6 @@ module Apilayer
|
|
44
62
|
# When errors are returned by apilayer, the 'info' and 'code' from its error will be used to raise an Apilayer::Error
|
45
63
|
def parse_response(resp)
|
46
64
|
body = JSON.parse(resp.body)
|
47
|
-
# According to documentation, currencylayer has a "success" field
|
48
|
-
# while vatlayer has a "valid" field to indicate whether the request was succesful or not.
|
49
|
-
# However, for both layers, an unsuccesful request would contain an "error" field.
|
50
|
-
# That's why the presence of "error" is chosen to determine whether we should raise an error or not.
|
51
65
|
if body['error']
|
52
66
|
raise Apilayer::Error.new(
|
53
67
|
body['error']['info'],
|
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:
|
4
|
+
version: 2.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-
|
11
|
+
date: 2016-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -190,7 +190,7 @@ dependencies:
|
|
190
190
|
- - ">="
|
191
191
|
- !ruby/object:Gem::Version
|
192
192
|
version: 2.0.1
|
193
|
-
description:
|
193
|
+
description: Acts as a dependency for the currency_layer and vat_layer gems. See https://apilayer.com/
|
194
194
|
for more details.
|
195
195
|
email:
|
196
196
|
- actfong@gmail.com
|
@@ -204,9 +204,7 @@ files:
|
|
204
204
|
- Rakefile
|
205
205
|
- lib/apilayer.rb
|
206
206
|
- lib/apilayer/connection_helper.rb
|
207
|
-
- lib/apilayer/currency.rb
|
208
207
|
- lib/apilayer/error.rb
|
209
|
-
- lib/apilayer/vat.rb
|
210
208
|
homepage: https://github.com/actfong/apilayer
|
211
209
|
licenses:
|
212
210
|
- MIT
|
@@ -230,6 +228,6 @@ rubyforge_project:
|
|
230
228
|
rubygems_version: 2.4.5
|
231
229
|
signing_key:
|
232
230
|
specification_version: 4
|
233
|
-
summary:
|
231
|
+
summary: Acts as a dependency for the currency_layer and vat_layer gems. See https://apilayer.com/
|
234
232
|
for more details.
|
235
233
|
test_files: []
|
data/lib/apilayer/currency.rb
DELETED
@@ -1,158 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# Ruby wrapper for currencylayer. See https://currencylayer.com/documentation for more info
|
3
|
-
module Apilayer
|
4
|
-
module Currency
|
5
|
-
extend ConnectionHelper
|
6
|
-
|
7
|
-
##
|
8
|
-
# The following constants determine which values in Apilayer.configs to be used
|
9
|
-
# in order to to make a connection to currencylayer
|
10
|
-
APILAYER_CONFIG_KEY = :currency_key
|
11
|
-
APILAYER_SECURE = :currency_https
|
12
|
-
|
13
|
-
INVALID_OPTIONS_MSG = "You have provided an invalid option. Allowed options are :currencies and :source"
|
14
|
-
INVALID_TIMEFRAME_MSG = "start_date and end_date must be either provided together or left out together."
|
15
|
-
LIVE_SLUG = "live"
|
16
|
-
HISTORICAL_SLUG = "historical"
|
17
|
-
CONVERT_SLUG = "convert"
|
18
|
-
TIMEFRAME_SLUG = "timeframe"
|
19
|
-
CHANGE_SLUG = "change"
|
20
|
-
LIST_SLUG = "list"
|
21
|
-
|
22
|
-
## Validations
|
23
|
-
#
|
24
|
-
def self.validate_options(options)
|
25
|
-
options.keys.each do |key|
|
26
|
-
unless [:currencies, :source].include? key
|
27
|
-
raise Apilayer::Error.new(INVALID_OPTIONS_MSG)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.validate_timeframe_completeness(start_date,end_date)
|
33
|
-
if [start_date,end_date].compact.size == 1
|
34
|
-
raise Apilayer::Error.new(INVALID_TIMEFRAME_MSG)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
### API methods
|
38
|
-
#
|
39
|
-
|
40
|
-
##
|
41
|
-
# Api-Method: Calls the /list endpoint to display all supported currencies
|
42
|
-
def self.list
|
43
|
-
get_and_parse LIST_SLUG
|
44
|
-
end
|
45
|
-
|
46
|
-
##
|
47
|
-
# Api-Method: Calls the /live endpoint to get real-time exchange rates.
|
48
|
-
# When no currency-codes are specified, it will return all exchange rates for your source-currency.
|
49
|
-
# Examples:
|
50
|
-
# Apilayer::Currency.live
|
51
|
-
# Apilayer::Currency.live(:currencies => %w[GBP, CHF])
|
52
|
-
# Apilayer::Currency.live(:source => "EUR")
|
53
|
-
# Apilayer::Currency.live(:source => "EUR", :currencies => %w[GBP, CHF])
|
54
|
-
def self.live(opts={})
|
55
|
-
validate_options(opts)
|
56
|
-
|
57
|
-
if opts.empty?
|
58
|
-
get_and_parse LIVE_SLUG
|
59
|
-
else
|
60
|
-
get_and_parse_with_options(LIVE_SLUG, opts)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
##
|
65
|
-
# Api-Method: Calls the /historical endpoint to get exchange rates for a specific date.
|
66
|
-
# When no currency-codes are specified, it will return all exchange rates for your source-currency.
|
67
|
-
# Examples:
|
68
|
-
# Apilayer::Currency.historical("2016-01-01")
|
69
|
-
# Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF])
|
70
|
-
# Apilayer::Currency.historical(:source => "EUR")
|
71
|
-
# Apilayer::Currency.historical("2016-01-01", :currencies => %w[GBP CHF], :source => "EUR")
|
72
|
-
def self.historical(date, opts={})
|
73
|
-
validate_options(opts)
|
74
|
-
params = {:date => date}
|
75
|
-
|
76
|
-
if opts.empty?
|
77
|
-
get_and_parse(HISTORICAL_SLUG, params)
|
78
|
-
else
|
79
|
-
get_and_parse_with_options(HISTORICAL_SLUG, opts, params)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
##
|
84
|
-
# Api-Method: Calls the /convert endpoint, requires :from, :to and :amount
|
85
|
-
# When :date hasn't been passed, the latest available exchange rates will be used for your conversion.
|
86
|
-
# Examples:
|
87
|
-
# Apilayer::Currency.convert("EUR", "CHF", 100)
|
88
|
-
# Apilayer::Currency.convert("EUR", "CHF", 100, "2015-03-01")
|
89
|
-
def self.convert(from, to, amount, date=nil)
|
90
|
-
params = {:from => from, :to => to, :amount => amount}
|
91
|
-
params.merge!(:date => date) if date
|
92
|
-
get_and_parse(CONVERT_SLUG, params)
|
93
|
-
end
|
94
|
-
|
95
|
-
##
|
96
|
-
# Api-Method: Calls the /timeframe endpoint. Requires :start_date and :end_date
|
97
|
-
# If :currencies hasn't been provided as an option, it will return all exchange-rates for that period of your source-currency
|
98
|
-
# :source can be provided as option to change the source-currency, which is USD by default
|
99
|
-
# The difference between start_date and end_date can be maximum 365 days
|
100
|
-
# Examples:
|
101
|
-
# Apilayer::Currency.timeframe("2016-01-01", "2016-03-01")
|
102
|
-
# Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
|
103
|
-
# Apilayer::Currency.timeframe("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF], :source => "EUR")
|
104
|
-
def self.timeframe(start_date, end_date, opts={})
|
105
|
-
validate_options(opts)
|
106
|
-
params = {:start_date => start_date, :end_date => end_date}
|
107
|
-
get_and_parse_with_options(TIMEFRAME_SLUG, opts, params)
|
108
|
-
end
|
109
|
-
|
110
|
-
##
|
111
|
-
# Api-Method: Calls the /change endpoint.
|
112
|
-
# start_date and end_date are optional, but can't provide one without the other
|
113
|
-
# :currencies and :source are optional
|
114
|
-
# Examples:
|
115
|
-
# Apilayer::Currency.change
|
116
|
-
# Apilayer::Currency.change("2016-01-01", "2016-03-01")
|
117
|
-
# Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR")
|
118
|
-
# Apilayer::Currency.change("2016-01-01", "2016-03-01", :currencies => %w[GBP CHF])
|
119
|
-
# Apilayer::Currency.change("2016-01-01", "2016-03-01", :source => "EUR", :currencies => %w[GBP CHF])
|
120
|
-
# Apilayer::Currency.change(nil, nil, {:source => "EUR"})
|
121
|
-
# Apilayer::Currency.change(nil, nil, {:currencies => %w[GBP CHF]})
|
122
|
-
# Apilayer::Currency.change(nil, nil, {:source => "EUR", :currencies => %w[GBP CHF]})
|
123
|
-
def self.change(start_date=nil, end_date=nil, opts={})
|
124
|
-
validate_options(opts)
|
125
|
-
validate_timeframe_completeness(start_date,end_date)
|
126
|
-
params = {:start_date => start_date,
|
127
|
-
:end_date => end_date
|
128
|
-
}.reject{ |k,v| v.nil? }
|
129
|
-
get_and_parse_with_options(CHANGE_SLUG, opts, params)
|
130
|
-
end
|
131
|
-
|
132
|
-
##
|
133
|
-
#
|
134
|
-
def self.get_and_parse_with_options(slug, opts, params={})
|
135
|
-
params = add_options_to_params(opts, params)
|
136
|
-
get_and_parse(slug, params)
|
137
|
-
end
|
138
|
-
|
139
|
-
##
|
140
|
-
# Adds currencies and source to query string if they have been provided with options-hash
|
141
|
-
def self.add_options_to_params(opts, params)
|
142
|
-
if opts[:currencies] && opts[:currencies].any?
|
143
|
-
params[:currencies] = join_by_commas(opts[:currencies])
|
144
|
-
end
|
145
|
-
if opts[:source]
|
146
|
-
params[:source] = opts[:source]
|
147
|
-
end
|
148
|
-
params
|
149
|
-
end
|
150
|
-
|
151
|
-
##
|
152
|
-
# Joins currencies in an array as a comma-separated-string
|
153
|
-
def self.join_by_commas(currencies)
|
154
|
-
currencies.map(&:strip).join(",")
|
155
|
-
end
|
156
|
-
|
157
|
-
end
|
158
|
-
end
|
data/lib/apilayer/vat.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
##
|
2
|
-
# Ruby wrapper for vatlayer. See https://vatlayer.com/documentation for more info
|
3
|
-
module Apilayer
|
4
|
-
module Vat
|
5
|
-
extend ConnectionHelper
|
6
|
-
|
7
|
-
COUNTRY_CRITERIA_MISSING_MSG = "You must provide either :country_code or :ip_address"
|
8
|
-
|
9
|
-
##
|
10
|
-
# The following constants determine which values in Apilayer.configs to be used
|
11
|
-
# in order to make a connection to vatlayer
|
12
|
-
APILAYER_CONFIG_KEY = :vat_key
|
13
|
-
APILAYER_SECURE = :vat_https
|
14
|
-
##
|
15
|
-
# Validates whether a supported criteria has been provided to .rate and .price
|
16
|
-
def self.validate_country_criteria(criteria)
|
17
|
-
unless [:country_code, :ip_address].include? criteria
|
18
|
-
raise Apilayer::Error.new COUNTRY_CRITERIA_MISSING_MSG
|
19
|
-
end
|
20
|
-
end
|
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")
|
29
|
-
def self.validate(vat_number)
|
30
|
-
params = {:vat_number => vat_number}
|
31
|
-
get_and_parse("validate", params)
|
32
|
-
end
|
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")
|
40
|
-
def self.rate(criteria, value)
|
41
|
-
validate_country_criteria(criteria)
|
42
|
-
params = {criteria.to_sym => value}
|
43
|
-
get_and_parse("rate", params)
|
44
|
-
end
|
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
|
51
|
-
def self.rate_list
|
52
|
-
get_and_parse("rate_list")
|
53
|
-
end
|
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")
|
61
|
-
def self.price(price, criteria, value)
|
62
|
-
validate_country_criteria(criteria)
|
63
|
-
params = {:amount => price, criteria.to_sym => value}
|
64
|
-
get_and_parse("price", params)
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
end
|