Module: Apilayer::Vat

Extended by:
ConnectionHelper
Defined in:
lib/apilayer/vat.rb

Constant Summary collapse

COUNTRY_CRITERIA_MISSING_MSG =
"You must provide either :country_code or :ip_address"
APILAYER_CONFIG_KEY =

Determines which access_key in Apilayer.configs to use in order to make a connection to vatlayer

:vat_key

Class Method Summary collapse

Methods included from ConnectionHelper

connection, get_and_parse, get_request, parse_response, reset_connection

Class Method Details

.price(price, criteria, value) ⇒ Object

Api-Method: Calls the /price endpoint to get price including and excluding VAT for a given price and country. It also returns the VAT rate for that country Example:

Apilayer::Vat.price(100, :country, "NL")
Apilayer::Vat.price(100, :ip_address, "176.249.153.36")

61
62
63
64
65
# File 'lib/apilayer/vat.rb', line 61

def self.price(price, criteria, value)
  validate_country_criteria(criteria)
  params = {:amount => price, criteria.to_sym => value}
  get_and_parse("price", params)
end

.rate(criteria, value) ⇒ Object

Api-Method: Calls the /rate endpoint to get the VAT-rate of a given country, based on country-code or ip-address. Example:

Apilayer::Vat.rate(:country_code, "NL")
Apilayer::Vat.rate(:ip_address, "176.249.153.36")

40
41
42
43
44
# File 'lib/apilayer/vat.rb', line 40

def self.rate(criteria, value)
  validate_country_criteria(criteria)
  params = {criteria.to_sym => value}
  get_and_parse("rate", params)
end

.rate_listObject

Api-Method: Calls the /rate_list endpoint to get the standard and reduced VAT-rates for all EU countries. Example:

Apilayer::Vat.rate_list

51
52
53
# File 'lib/apilayer/vat.rb', line 51

def self.rate_list
  get_and_parse("rate_list")
end

.validate(vat_number) ⇒ Object

Api-Method: Calls the /validate endpoint to validate a given VAT-number. Example:

Apilayer::Vat.validate("LU26375245")

29
30
31
32
# File 'lib/apilayer/vat.rb', line 29

def self.validate(vat_number)
  params = {:vat_number => vat_number}
  get_and_parse("validate", params)
end

.validate_country_criteria(criteria) ⇒ Object

Validates whether a supported criteria has been provided to .rate and .price


16
17
18
19
20
# File 'lib/apilayer/vat.rb', line 16

def self.validate_country_criteria(criteria)
  unless [:country_code, :ip_address].include? criteria
    raise Apilayer::Error.new COUNTRY_CRITERIA_MISSING_MSG
  end
end