vat_check 0.0.5

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 161ced06f8bbe69aa32234ea941633459c9d5c48
4
+ data.tar.gz: d214f05f0e53135fcd031ff70e95721895d29363
5
+ SHA512:
6
+ metadata.gz: 22c4797919d9ce95b01846ba5e5a2480878a3107bc42fb8a60b5aded16faa75180e50e0412efa45ba078454ab46451b367f58ba7c6d10ac8cf33ea7c16df5f08
7
+ data.tar.gz: bec7aaf559af0894e3d89871e00a74f25f751443d211f7d623ce100a78925b647f4ac4c86f95316bfeae9766845137f52fde1f76f3a1e9d6445f1a953009ee6c
@@ -0,0 +1,25 @@
1
+ class VatCheck
2
+ attr_reader :regex, :vies, :vies_available, :response
3
+
4
+ def initialize(vat)
5
+ @vat = vat
6
+ @regex = VatCheck::Format.valid?(@vat)
7
+ @response = @regex ? VatCheck::Request.lookup(@vat) : {}
8
+ @vies_available = @response.has_key?(:valid)
9
+ @vies = @vies_available ? @response[:valid] : false
10
+ end
11
+
12
+ def valid?
13
+ return false unless @regex
14
+ return @regex unless @vies_available
15
+ return @vies
16
+ end
17
+
18
+ def exists?
19
+ return @vies
20
+ end
21
+ end
22
+
23
+ require 'vat_check/utility'
24
+ require 'vat_check/format'
25
+ require 'vat_check/requests'
@@ -0,0 +1,43 @@
1
+ class VatCheck
2
+ module Format
3
+
4
+ def self.valid?(vat)
5
+ normalized = VatCheck::Utility.normalize(vat)
6
+ country_code, id = VatCheck::Utility.split(vat)
7
+ !!(self.patterns[country_code] && self.patterns[country_code] =~ normalized)
8
+ end
9
+
10
+ def self.patterns
11
+ @patterns ||= {
12
+ 'AT' => /\AATU[0-9]{8}\Z/, # Austria
13
+ 'BE' => /\ABE0[0-9]{9}\Z/, # Belgium
14
+ 'BG' => /\ABG[0-9]{9,10}\Z/, # Bulgaria
15
+ 'CY' => /\ACY[0-9]{8}[A-Z]\Z/, # Cyprus
16
+ 'CZ' => /\ACZ[0-9]{8,10}\Z/, # Czech Republic
17
+ 'DE' => /\ADE[0-9]{9}\Z/, # Germany
18
+ 'DK' => /\ADK[0-9]{8}\Z/, # Denmark
19
+ 'EE' => /\AEE[0-9]{9}\Z/, # Estonia
20
+ 'EL' => /\AEL[0-9]{9}\Z/, # Greece
21
+ 'ES' => /\AES([A-Z][0-9]{8}|[0-9]{8}[A-Z]|[A-Z][0-9]{7}[A-Z])\Z/, # Spain
22
+ 'FI' => /\AFI[0-9]{8}\Z/, # Finland
23
+ 'FR' => /\AFR[A-Z0-9]{2}[0-9]{9}\Z/, # France
24
+ 'GB' => /\AGB([0-9]{9}|[0-9]{12}|(HA|GD)[0-9]{3})\Z/, # United Kingdom
25
+ 'HR' => /\AHR[0-9]{11}\Z/, # Croatia
26
+ 'HU' => /\AHU[0-9]{8}\Z/, # Hungary
27
+ 'IE' => /\AIE([0-9][A-Z][0-9]{5}|[0-9]{7}[A-Z]?)[A-Z]\Z/, # Ireland
28
+ 'IT' => /\AIT[0-9]{11}\Z/, # Italy
29
+ 'LT' => /\ALT([0-9]{9}|[0-9]{12})\Z/, # Lithuania
30
+ 'LU' => /\ALU[0-9]{8}\Z/, # Luxembourg
31
+ 'LV' => /\ALV[0-9]{11}\Z/, # Latvia
32
+ 'MT' => /\AMT[0-9]{8}\Z/, # Malta
33
+ 'NL' => /\ANL[0-9]{9}B[0-9]{2}\Z/, # Netherlands
34
+ 'PL' => /\APL[0-9]{10}\Z/, # Poland
35
+ 'PT' => /\APT[0-9]{9}\Z/, # Portugal
36
+ 'RO' => /\ARO[1-9][0-9]{1,9}\Z/, # Romania
37
+ 'SE' => /\ASE[0-9]{12}\Z/, # Sweden
38
+ 'SI' => /\ASI[0-9]{8}\Z/, # Slovenia
39
+ 'SK' => /\ASK[0-9]{10}\Z/ # Slovakia
40
+ }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ require 'savon'
2
+
3
+ class VatCheck
4
+ module Request
5
+ def self.lookup(vat)
6
+ client = Savon.client(wsdl: 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', log: false, log_level: :debug, pretty_print_xml: false)
7
+ country_code, vat_number = VatCheck::Utility.split(vat)
8
+ begin
9
+ response = client.call(:check_vat, message: {country_code: country_code, vat_number: vat_number}, message_tag: :checkVat)
10
+ response.to_hash[:check_vat_response].reject { |key| key == :@xmlns }
11
+ rescue Savon::SOAPFault => e
12
+ if !!(e.message =~ /MS_UNAVAILABLE/)
13
+ return {error:'Service unavailable'}
14
+ else
15
+ return {error:"Unknown error: #{e.message}"}
16
+ end
17
+ rescue Timeout::Error
18
+ return {error:'Service timed out'}
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ class VatCheck
2
+ module Utility
3
+ EU_COUNTRIES = %w(AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU LV MT NL PL PT RO SE SI SK)
4
+ COUNTRY_PATTERN = /\A([A-Z]{2})(.+)\Z/
5
+ NORMALIZE_PATTERN = /[-\.:_\s,;]+/
6
+
7
+ def self.split(vat)
8
+ COUNTRY_PATTERN =~ vat
9
+ result = [$1, $2]
10
+ iso_country = vat_country_to_iso_country(result[0])
11
+ EU_COUNTRIES.include?(iso_country) ? result : [nil, nil]
12
+ end
13
+
14
+ def self.normalize(vat)
15
+ vat.to_s.upcase.gsub(NORMALIZE_PATTERN, "")
16
+ end
17
+
18
+ def self.vat_country_to_iso_country(vat_country)
19
+ vat_country == "EL" ? "GR" : vat_country
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vat_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - TaxJar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.11'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.11.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.11'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.11.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.4'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.4'
47
+ - !ruby/object:Gem::Dependency
48
+ name: vcr
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.1
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '3.0'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.0.1
67
+ - !ruby/object:Gem::Dependency
68
+ name: webmock
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.22'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.22.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.22'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 1.22.0
87
+ - !ruby/object:Gem::Dependency
88
+ name: mocha
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '1.1'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.1.0
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.1'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 1.1.0
107
+ description: A gem by TaxJar that can be used to validate VAT IDs
108
+ email: support@taxjar.com
109
+ executables: []
110
+ extensions: []
111
+ extra_rdoc_files: []
112
+ files:
113
+ - lib/vat_check.rb
114
+ - lib/vat_check/format.rb
115
+ - lib/vat_check/requests.rb
116
+ - lib/vat_check/utility.rb
117
+ homepage: http://taxjar.com
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: 2.1.6
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.8
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Validate VAT IDs
141
+ test_files: []