valvat-core 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ class Valvat
2
+ module Syntax
3
+
4
+ VAT_PATTERNS = {
5
+ 'AT' => /\AATU[0-9]{8}\Z/, # Austria
6
+ 'BE' => /\ABE0[0-9]{9}\Z/, # Belgium
7
+ 'BG' => /\ABG[0-9]{9,10}\Z/, # Bulgaria
8
+ 'CY' => /\ACY[0-9]{8}[A-Z]\Z/, # Cyprus
9
+ 'CZ' => /\ACZ[0-9]{8,10}\Z/, # Czech Republic
10
+ 'DE' => /\ADE[0-9]{9}\Z/, # Germany
11
+ 'DK' => /\ADK[0-9]{8}\Z/, # Denmark
12
+ 'EE' => /\AEE[0-9]{9}\Z/, # Estonia
13
+ 'GR' => /\AEL[0-9]{9}\Z/, # Greece
14
+ 'ES' => /\AES([A-Z][0-9]{8}|[0-9]{8}[A-Z]|[A-Z][0-9]{7}[A-Z])\Z/, # Spain
15
+ 'FI' => /\AFI[0-9]{8}\Z/, # Finland
16
+ 'FR' => /\AFR[A-Z0-9]{2}[0-9]{9}\Z/, # France
17
+ 'GB' => /\AGB([0-9]{9}|[0-9]{12}|(HA|GD)[0-9]{3})\Z/, # United Kingdom
18
+ 'HR' => /\AHR[0-9]{11}\Z/, # Croatia
19
+ 'HU' => /\AHU[0-9]{8}\Z/, # Hungary
20
+ 'IE' => /\AIE([0-9][A-Z][0-9]{5}|[0-9]{7}[A-Z]?)[A-Z]\Z/, # Ireland
21
+ 'IT' => /\AIT[0-9]{11}\Z/, # Italy
22
+ 'LT' => /\ALT([0-9]{9}|[0-9]{12})\Z/, # Lithuania
23
+ 'LU' => /\ALU[0-9]{8}\Z/, # Luxembourg
24
+ 'LV' => /\ALV[0-9]{11}\Z/, # Latvia
25
+ 'MT' => /\AMT[0-9]{8}\Z/, # Malta
26
+ 'NL' => /\ANL[0-9]{9}B[0-9]{2}\Z/, # Netherlands
27
+ 'PL' => /\APL[0-9]{10}\Z/, # Poland
28
+ 'PT' => /\APT[0-9]{9}\Z/, # Portugal
29
+ 'RO' => /\ARO[1-9][0-9]{1,9}\Z/, # Romania
30
+ 'SE' => /\ASE[0-9]{10}01\Z/, # Sweden
31
+ 'SI' => /\ASI[0-9]{8}\Z/, # Slovenia
32
+ 'SK' => /\ASK[0-9]{10}\Z/ # Slovakia
33
+ }
34
+
35
+ def self.validate(vat)
36
+ vat = Valvat(vat)
37
+ pattern = VAT_PATTERNS[vat.iso_country_code]
38
+ !!(pattern && pattern =~ vat.to_s)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ class Valvat
2
+ module Utils
3
+
4
+ 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)
5
+ COUNTRY_PATTERN = /\A([A-Z]{2})(.+)\Z/
6
+ NORMALIZE_PATTERN = /[-\.:_\s,;]+/
7
+
8
+ def self.split(vat)
9
+ COUNTRY_PATTERN =~ vat
10
+ result = [$1, $2]
11
+ iso_country = vat_country_to_iso_country(result[0])
12
+ EU_COUNTRIES.include?(iso_country) ? result : [nil, nil]
13
+ end
14
+
15
+ def self.normalize(vat)
16
+ vat.to_s.upcase.gsub(NORMALIZE_PATTERN, "")
17
+ end
18
+
19
+ def self.vat_country_to_iso_country(vat_country)
20
+ vat_country == "EL" ? "GR" : vat_country
21
+ end
22
+
23
+ def self.iso_country_to_vat_country(iso_country)
24
+ iso_country == "GR" ? "EL" : iso_country
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'rspec'
2
+
3
+ require File.dirname(__FILE__) + '/../lib/valvat.rb'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ config.filter_run :focus => true
8
+ config.run_all_when_everything_filtered = true
9
+ config.backtrace_exclusion_patterns = [/rspec\/(core|expectations)/]
10
+ end
11
+
@@ -0,0 +1,287 @@
1
+ require 'spec_helper'
2
+
3
+ describe Valvat::Syntax do
4
+ describe "#validate" do
5
+ it "validates a DE vat number" do
6
+ expect(subject.validate("DE345889003")).to eql(true)
7
+
8
+ expect(subject.validate("DE34588900")).to eql(false)
9
+ expect(subject.validate("DE3458890090")).to eql(false)
10
+ expect(subject.validate("DE34588900C")).to eql(false)
11
+ end
12
+
13
+ it "validates a AT vat number" do
14
+ expect(subject.validate("ATU03458890")).to eql(true)
15
+
16
+ expect(subject.validate("ATU034588908")).to eql(false)
17
+ expect(subject.validate("ATU0345908")).to eql(false)
18
+ expect(subject.validate("ATU0345889Y")).to eql(false)
19
+ end
20
+
21
+ it "should validate BE vat number" do
22
+ expect(subject.validate("BE0817331995")).to eql(true)
23
+
24
+ expect(subject.validate("BE081733199")).to eql(false)
25
+ expect(subject.validate("BE08173319944")).to eql(false)
26
+ expect(subject.validate("BE081733199H")).to eql(false)
27
+ expect(subject.validate("BE1817331999")).to eql(false)
28
+ end
29
+
30
+ it "validates a BG vat number" do
31
+ expect(subject.validate("BG468134789")).to eql(true)
32
+ expect(subject.validate("BG4681347897")).to eql(true)
33
+
34
+ expect(subject.validate("BG46813478979")).to eql(false)
35
+ expect(subject.validate("BG4681347897C")).to eql(false)
36
+ expect(subject.validate("BG46813478G")).to eql(false)
37
+ expect(subject.validate("BG46813478")).to eql(false)
38
+ end
39
+
40
+ it "validates a CY vat number" do
41
+ expect(subject.validate("CY36579347A")).to eql(true)
42
+ expect(subject.validate("CY36579347C")).to eql(true)
43
+
44
+ expect(subject.validate("CY36579347")).to eql(false)
45
+ expect(subject.validate("CY365793478")).to eql(false)
46
+ expect(subject.validate("CY365793478A")).to eql(false)
47
+ expect(subject.validate("CY365793G")).to eql(false)
48
+ end
49
+
50
+ it "validates a DK vat number" do
51
+ expect(subject.validate("DK67893463")).to eql(true)
52
+
53
+ expect(subject.validate("DK678934637")).to eql(false)
54
+ expect(subject.validate("DK6789346")).to eql(false)
55
+ expect(subject.validate("DK6789346H")).to eql(false)
56
+ end
57
+
58
+ it "validates a ES vat number" do
59
+ expect(subject.validate("ESX67345987")).to eql(true)
60
+ expect(subject.validate("ESA6734598B")).to eql(true)
61
+ expect(subject.validate("ES56734598C")).to eql(true)
62
+
63
+ expect(subject.validate("ES167345987")).to eql(false)
64
+ expect(subject.validate("ESX6734598")).to eql(false)
65
+ expect(subject.validate("ESX673459890")).to eql(false)
66
+ end
67
+
68
+ it "validates a EE vat number" do
69
+ expect(subject.validate("EE678678456")).to eql(true)
70
+ expect(subject.validate("EE6786784560")).to eql(false)
71
+ expect(subject.validate("EE67867845")).to eql(false)
72
+ expect(subject.validate("EE67867845K")).to eql(false)
73
+ end
74
+
75
+ it "validates a FI vat number" do
76
+ expect(subject.validate("FI67845638")).to eql(true)
77
+
78
+ expect(subject.validate("FI678456389")).to eql(false)
79
+ expect(subject.validate("FI6784563")).to eql(false)
80
+ expect(subject.validate("FI6784563K")).to eql(false)
81
+ end
82
+
83
+ it "validates a FR vat number" do
84
+ expect(subject.validate("FR99123543267")).to eql(true)
85
+ expect(subject.validate("FRBB123543267")).to eql(true)
86
+ expect(subject.validate("FR9B123543267")).to eql(true)
87
+ expect(subject.validate("FRB9123543267")).to eql(true)
88
+
89
+ expect(subject.validate("FR99123543267B")).to eql(false)
90
+ expect(subject.validate("FRBB12354326")).to eql(false)
91
+ expect(subject.validate("FR9B123543F67")).to eql(false)
92
+ expect(subject.validate("FRB9123543")).to eql(false)
93
+ end
94
+
95
+ it "validates a EL vat number" do
96
+ expect(subject.validate("EL678456345")).to eql(true)
97
+
98
+ expect(subject.validate("EL67845634")).to eql(false)
99
+ expect(subject.validate("EL6784563459")).to eql(false)
100
+ expect(subject.validate("EL67845634P")).to eql(false)
101
+ end
102
+
103
+ it "validates a HU vat number" do
104
+ expect(subject.validate("HU67894595")).to eql(true)
105
+
106
+ expect(subject.validate("HU6789459")).to eql(false)
107
+ expect(subject.validate("HU67894595L")).to eql(false)
108
+ expect(subject.validate("HU6789459J")).to eql(false)
109
+ end
110
+
111
+ it "validates a HR vat number" do
112
+ expect(subject.validate("HR12345678912")).to eql(true)
113
+
114
+ expect(subject.validate("HR6789459")).to eql(false)
115
+ expect(subject.validate("HR67894595L")).to eql(false)
116
+ expect(subject.validate("HR6789459J")).to eql(false)
117
+ end
118
+
119
+ it "validates a IE vat number" do
120
+ expect(subject.validate("IE1B12345J")).to eql(true)
121
+ expect(subject.validate("IE1234567B")).to eql(true)
122
+ expect(subject.validate("IE1234567XX")).to eql(true)
123
+
124
+ expect(subject.validate("IE1B123459")).to eql(false)
125
+ expect(subject.validate("IE19123450")).to eql(false)
126
+ expect(subject.validate("IEA9123450")).to eql(false)
127
+ expect(subject.validate("IE1B12345XX")).to eql(false)
128
+ expect(subject.validate("IE1X34567XX")).to eql(false)
129
+ end
130
+
131
+ it "validates a IT vat number" do
132
+ expect(subject.validate("IT45875359285")).to eql(true)
133
+
134
+ expect(subject.validate("IT458753592859")).to eql(false)
135
+ expect(subject.validate("IT4587535928G")).to eql(false)
136
+ expect(subject.validate("IT4587535928")).to eql(false)
137
+ end
138
+
139
+ it "validates a LV vat number" do
140
+ expect(subject.validate("LV85678368906")).to eql(true)
141
+
142
+ expect(subject.validate("LV8567836890")).to eql(false)
143
+ expect(subject.validate("LV856783689066")).to eql(false)
144
+ expect(subject.validate("LV8567836890S")).to eql(false)
145
+ end
146
+
147
+ it "validates a LT vat number" do
148
+ expect(subject.validate("LT678678987")).to eql(true)
149
+ expect(subject.validate("LT678678987956")).to eql(true)
150
+
151
+ expect(subject.validate("LT67867898")).to eql(false)
152
+ expect(subject.validate("LT6786789870")).to eql(false)
153
+ expect(subject.validate("LT678678987K")).to eql(false)
154
+ expect(subject.validate("LT67867898709")).to eql(false)
155
+ expect(subject.validate("LT6786789870C")).to eql(false)
156
+ expect(subject.validate("LT67867898795H")).to eql(false)
157
+ end
158
+
159
+ it "validates a LU vat number" do
160
+ expect(subject.validate("LU45679456")).to eql(true)
161
+
162
+ expect(subject.validate("LU4567945")).to eql(false)
163
+ expect(subject.validate("LU456794560")).to eql(false)
164
+ expect(subject.validate("LU4567945J")).to eql(false)
165
+ end
166
+
167
+ it "validates a MT vat number" do
168
+ expect(subject.validate("MT64569367")).to eql(true)
169
+
170
+ expect(subject.validate("MT6456936")).to eql(false)
171
+ expect(subject.validate("MT645693679")).to eql(false)
172
+ expect(subject.validate("MT6456936L")).to eql(false)
173
+ end
174
+
175
+ it "validates a NL vat number" do
176
+ expect(subject.validate("NL673739491B77")).to eql(true)
177
+
178
+ expect(subject.validate("NL673739491977")).to eql(false)
179
+ expect(subject.validate("NL673739491A77")).to eql(false)
180
+ expect(subject.validate("NL673739491B771")).to eql(false)
181
+ expect(subject.validate("NL673739491B7")).to eql(false)
182
+ expect(subject.validate("NL6737394917B7")).to eql(false)
183
+ end
184
+
185
+ it "validates a PL vat number" do
186
+ expect(subject.validate("PL6784567284")).to eql(true)
187
+
188
+ expect(subject.validate("PL678456728")).to eql(false)
189
+ expect(subject.validate("PL67845672849")).to eql(false)
190
+ expect(subject.validate("PL678456728K")).to eql(false)
191
+ end
192
+
193
+ it "validates a PT vat number" do
194
+ expect(subject.validate("PT346296476")).to eql(true)
195
+
196
+ expect(subject.validate("PT34629647")).to eql(false)
197
+ expect(subject.validate("PT3462964769")).to eql(false)
198
+ end
199
+
200
+ it "validates a GB vat number" do
201
+ expect(subject.validate("GB123456789")).to eql(true)
202
+ expect(subject.validate("GB123456789012")).to eql(true)
203
+ expect(subject.validate("GBGD123")).to eql(true)
204
+ expect(subject.validate("GBHA123")).to eql(true)
205
+
206
+ expect(subject.validate("GB12345678")).to eql(false)
207
+ expect(subject.validate("GB1234567890")).to eql(false)
208
+ expect(subject.validate("GB12345678901")).to eql(false)
209
+ expect(subject.validate("GB1234567890123")).to eql(false)
210
+ expect(subject.validate("GBAB123")).to eql(false)
211
+ expect(subject.validate("GBAA123")).to eql(false)
212
+ expect(subject.validate("GBHH123")).to eql(false)
213
+ expect(subject.validate("GBGD1234")).to eql(false)
214
+ expect(subject.validate("GBGD12")).to eql(false)
215
+ end
216
+
217
+ it "validates a RO vat number" do
218
+ expect(subject.validate("RO1234567890")).to eql(true)
219
+ expect(subject.validate("RO123456789")).to eql(true)
220
+ expect(subject.validate("RO12345678")).to eql(true)
221
+ expect(subject.validate("RO1234567")).to eql(true)
222
+ expect(subject.validate("RO123456")).to eql(true)
223
+ expect(subject.validate("RO12345")).to eql(true)
224
+ expect(subject.validate("RO1234")).to eql(true)
225
+ expect(subject.validate("RO123")).to eql(true)
226
+ expect(subject.validate("RO12")).to eql(true)
227
+
228
+ expect(subject.validate("RO1")).to eql(false)
229
+ expect(subject.validate("RO0234567890")).to eql(false)
230
+ expect(subject.validate("RO023456789")).to eql(false)
231
+ expect(subject.validate("RO02345678")).to eql(false)
232
+ expect(subject.validate("RO0234567")).to eql(false)
233
+ expect(subject.validate("RO023456")).to eql(false)
234
+ expect(subject.validate("RO02345")).to eql(false)
235
+ expect(subject.validate("RO0234")).to eql(false)
236
+ expect(subject.validate("RO023")).to eql(false)
237
+ expect(subject.validate("RO02")).to eql(false)
238
+ expect(subject.validate("RO12345678901")).to eql(false)
239
+ end
240
+
241
+ it "validates a SK vat number" do
242
+ expect(subject.validate("SK5683075682")).to eql(true)
243
+
244
+ expect(subject.validate("SK56830756821")).to eql(false)
245
+ expect(subject.validate("SK568307568")).to eql(false)
246
+ end
247
+
248
+ it "validates a SI vat number" do
249
+ expect(subject.validate("SI74357893")).to eql(true)
250
+
251
+ expect(subject.validate("SI743578931")).to eql(false)
252
+ expect(subject.validate("SI7435789")).to eql(false)
253
+ end
254
+
255
+ it "validates a SE vat number" do
256
+ expect(subject.validate("SE567396352701")).to eql(true)
257
+
258
+ expect(subject.validate("SE56739635201")).to eql(false)
259
+ expect(subject.validate("SE5673963527701")).to eql(false)
260
+ expect(subject.validate("SE567396352702")).to eql(false)
261
+ expect(subject.validate("SE567396352711")).to eql(false)
262
+ end
263
+
264
+ it "validates a CZ vat number" do
265
+ expect(subject.validate("CZ56389267")).to eql(true)
266
+ expect(subject.validate("CZ563892670")).to eql(true)
267
+ expect(subject.validate("CZ5638926790")).to eql(true)
268
+
269
+ expect(subject.validate("CZ5638926")).to eql(false)
270
+ expect(subject.validate("CZ56389268901")).to eql(false)
271
+ end
272
+
273
+ it "returns false on blank/non-sense vat number" do
274
+ expect(subject.validate("")).to eql(false)
275
+ expect(subject.validate(" ")).to eql(false)
276
+ expect(subject.validate("DE")).to eql(false)
277
+ expect(subject.validate("atu123xyz")).to eql(false)
278
+ expect(subject.validate("xxxxxxxxxx")).to eql(false)
279
+ expect(subject.validate("BEFR")).to eql(false)
280
+ end
281
+
282
+ it "allows Valvat instance as input" do
283
+ expect(subject.validate(Valvat.new("DE345889003"))).to eql(true)
284
+ expect(subject.validate(Valvat.new("DE34588900X"))).to eql(false)
285
+ end
286
+ end
287
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Valvat::Utils do
4
+ describe "#split" do
5
+ it "returns country and rest on vat number as array" do
6
+ expect(Valvat::Utils.split("DE345889003")).to eql(["DE", "345889003"])
7
+ expect(Valvat::Utils.split("ESX4588900X")).to eql(["ES", "X4588900X"])
8
+ end
9
+
10
+ it "returns two nils on non-european iso codes as array" do
11
+ expect(Valvat::Utils.split("US345889003")).to eql([nil, nil])
12
+ expect(Valvat::Utils.split("RUX4588900X")).to eql([nil, nil])
13
+ end
14
+
15
+ it "returns two nils on non-sense input as array" do
16
+ expect(Valvat::Utils.split("DE")).to eql([nil, nil])
17
+ expect(Valvat::Utils.split("X345889003")).to eql([nil, nil])
18
+ expect(Valvat::Utils.split("")).to eql([nil, nil])
19
+ expect(Valvat::Utils.split("1234")).to eql([nil, nil])
20
+ expect(Valvat::Utils.split(" ")).to eql([nil, nil])
21
+ end
22
+
23
+ it "returns EL (language iso code) on greek vat" do
24
+ expect(Valvat::Utils.split("EL999999999")).to eql(["EL", "999999999"])
25
+ end
26
+ end
27
+
28
+ describe "#normalize" do
29
+ it "returns vat number with upcase chars" do
30
+ expect(Valvat::Utils.normalize("de345889003")).to eql("DE345889003")
31
+ expect(Valvat::Utils.normalize("EsX4588900y")).to eql("ESX4588900Y")
32
+ end
33
+
34
+ it "returns trimmed vat number" do
35
+ expect(Valvat::Utils.normalize(" DE345889003")).to eql("DE345889003")
36
+ expect(Valvat::Utils.normalize(" DE345889003 ")).to eql("DE345889003")
37
+ expect(Valvat::Utils.normalize("DE345889003 ")).to eql("DE345889003")
38
+ end
39
+
40
+ it "does not change already normalized vat numbers" do
41
+ expect(Valvat::Utils.normalize("DE345889003")).to eql("DE345889003")
42
+ expect(Valvat::Utils.normalize("ESX4588900X")).to eql("ESX4588900X")
43
+ end
44
+
45
+ it "removes spaces" do
46
+ expect(Valvat::Utils.normalize("DE 345889003")).to eql("DE345889003")
47
+ expect(Valvat::Utils.normalize("ESX 458 8900 X")).to eql("ESX4588900X")
48
+ end
49
+
50
+ it "removes special chars" do
51
+ expect(Valvat::Utils.normalize("DE.345-889_00:3,;")).to eql("DE345889003")
52
+ end
53
+ end
54
+
55
+ describe "#vat_country_to_iso_country" do
56
+ it "returns iso country code on greek iso language 'EL'" do
57
+ expect(Valvat::Utils.vat_country_to_iso_country("EL")).to eql("GR")
58
+ end
59
+
60
+ Valvat::Utils::EU_COUNTRIES.each do |iso|
61
+ it "returns unchanged iso country code '#{iso}'" do
62
+ expect(Valvat::Utils.vat_country_to_iso_country(iso)).to eql(iso)
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#iso_country_to_vat_country" do
68
+ it "returns vat country on greek iso country code 'GR'" do
69
+ expect(Valvat::Utils.iso_country_to_vat_country("GR")).to eql("EL")
70
+ end
71
+
72
+ Valvat::Utils::EU_COUNTRIES.reject{|c| c == "GR"}.each do |c|
73
+ it "returns unchanged vat country code '#{c}'" do
74
+ expect(Valvat::Utils.iso_country_to_vat_country(c)).to eql(c)
75
+ end
76
+ end
77
+ end
78
+ end