vat_validator 1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -4
- data/lib/exceptions.rb +3 -0
- data/lib/vat_validator.rb +29 -14
- data/lib/vies_checker.rb +41 -13
- data/spec/spec_helper.rb +7 -3
- data/spec/vat_validator/validations_spec.rb +2 -0
- data/spec/vat_validator/vies_checker_spec.rb +10 -0
- metadata +31 -13
data/Rakefile
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
-
require 'spec/rake/spectask'
|
4
3
|
|
5
4
|
desc "Run specs"
|
6
|
-
|
7
|
-
|
8
|
-
t.spec_opts = ["-c"]
|
5
|
+
task :spec do
|
6
|
+
puts `rspec spec/vat_validator/*_spec.rb`
|
9
7
|
end
|
10
8
|
|
11
9
|
task :default => :spec
|
data/lib/exceptions.rb
ADDED
data/lib/vat_validator.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_model'
|
2
|
-
require '
|
2
|
+
require 'exceptions'
|
3
|
+
require 'vies_checker' rescue nil
|
3
4
|
|
4
5
|
module VatValidator
|
5
6
|
|
@@ -41,23 +42,37 @@ module VatValidator
|
|
41
42
|
def validate_each(record, attribute, value)
|
42
43
|
format_valid = true
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
format_valid = false
|
49
|
-
end
|
50
|
-
else
|
51
|
-
unless value =~ VAT_PATTERNS.values.detect { |p| value.to_s =~ p }
|
52
|
-
record.errors.add(attribute, options[:message])
|
53
|
-
format_valid = false
|
54
|
-
end
|
45
|
+
country_code = options[:country_method] ? record.send(options[:country_method]).to_s : nil
|
46
|
+
unless VatNumber.new(value, country_code).valid?
|
47
|
+
record.errors.add(attribute, options[:message])
|
48
|
+
format_valid = false
|
55
49
|
end
|
56
50
|
|
57
51
|
if format_valid && options[:vies]
|
58
|
-
|
59
|
-
|
52
|
+
if options[:vies_host]
|
53
|
+
valid = ViesChecker.check(value, options[:vies_host])
|
54
|
+
else
|
55
|
+
valid = ViesChecker.check(value)
|
60
56
|
end
|
57
|
+
|
58
|
+
unless valid
|
59
|
+
record.errors.add(attribute, options[:message])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class VatNumber
|
66
|
+
def initialize(number, country_code = nil)
|
67
|
+
@number = number
|
68
|
+
@country_code = country_code
|
69
|
+
end
|
70
|
+
|
71
|
+
def valid?
|
72
|
+
if @country_code
|
73
|
+
VAT_PATTERNS.has_key?(@country_code) && @number.to_s =~ VAT_PATTERNS[@country_code]
|
74
|
+
else
|
75
|
+
VAT_PATTERNS.values.detect { |p| @number.to_s =~ p }
|
61
76
|
end
|
62
77
|
end
|
63
78
|
end
|
data/lib/vies_checker.rb
CHANGED
@@ -1,20 +1,48 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require '
|
2
|
+
require 'savon'
|
3
|
+
|
4
|
+
# Errors:
|
5
|
+
# from http://blog.riff.org/2007_04_08_soap_box_accessing_vies_from_phpgtk
|
6
|
+
# 'INVALID_INPUT' => 'The provided CountryCode is invalid or the VAT number is empty',
|
7
|
+
# 'SERVICE_UNAVAILABLE' => 'The SOAP service is unavailable, try again later',
|
8
|
+
# 'MS_UNAVAILABLE' => 'The Member State service is unavailable, try again later or with another Member State',
|
9
|
+
# 'TIMEOUT' => 'The Member State service could not be reached in time, try again later or with another Member State',
|
10
|
+
# 'SERVER_BUSY' => 'The service cannot process your request. Try again later.'
|
3
11
|
|
4
12
|
module VatValidator
|
5
13
|
class ViesChecker
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def self.check(complete_vat_number, vies_host='http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')
|
15
|
+
|
16
|
+
country_code = complete_vat_number[0..1]
|
17
|
+
vat_number = complete_vat_number[2..15]
|
18
|
+
|
19
|
+
client = Savon::Client.new(vies_host)
|
20
|
+
|
21
|
+
begin
|
22
|
+
response = client.request :check_vat do
|
23
|
+
soap.body = {
|
24
|
+
:country_code => country_code,
|
25
|
+
:vat_number => vat_number
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
rescue Savon::SOAP::Fault => fault
|
30
|
+
# https://github.com/rubiii/savon/blob/master/lib/savon/soap/fault.rb
|
31
|
+
if fault.to_hash[:fault][:faultstring] == "{ 'INVALID_INPUT' }"
|
32
|
+
return false
|
33
|
+
else
|
34
|
+
raise ViesContactError
|
35
|
+
end
|
36
|
+
|
37
|
+
rescue
|
38
|
+
raise ViesContactError
|
39
|
+
end
|
40
|
+
|
41
|
+
if response.success?
|
42
|
+
return response[:check_vat_response][:valid]
|
43
|
+
else
|
44
|
+
raise ViesContactError
|
45
|
+
end
|
18
46
|
end
|
19
47
|
end
|
20
48
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require '
|
2
|
+
require 'rspec'
|
3
3
|
require 'active_model'
|
4
4
|
|
5
5
|
require File.dirname(__FILE__) + '/../lib/vat_validator.rb'
|
@@ -48,6 +48,10 @@ class CountryCheckedInvoice < BaseTestModel
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
class ViesCheckedInvoice < BaseTestModel
|
51
|
+
class ViesCheckedInvoice < BaseTestModel
|
52
52
|
validates :vat_number, :vat => {:vies => true}
|
53
|
-
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class WrongViesCheckedInvoice < BaseTestModel
|
56
|
+
validates :vat_number, :vat => {:vies => true, :vies_host => 'http://agilitic.com/wrong.wsdl'}
|
57
|
+
end
|
@@ -5,10 +5,20 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
|
5
5
|
|
6
6
|
describe ViesCheckedInvoice do
|
7
7
|
it "should be valid if vat number is correct" do
|
8
|
+
VatValidator::ViesChecker.check('BE0817331995').should be_true
|
8
9
|
ViesCheckedInvoice.new(:vat_number => 'BE0817331995').should be_valid
|
9
10
|
end
|
10
11
|
|
11
12
|
it "should not be valid if vat number is correct" do
|
13
|
+
VatValidator::ViesChecker.check('BE000678345').should_not be_true
|
12
14
|
ViesCheckedInvoice.new(:vat_number => 'BE000678345').should_not be_valid
|
13
15
|
end
|
16
|
+
|
17
|
+
it "should not be valid if the input is not valid" do
|
18
|
+
VatValidator::ViesChecker.check('blop').should_not be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should raise an exception if VIES is unreachable" do
|
22
|
+
lambda {WrongViesCheckedInvoice.new(:vat_number => 'BE0817331995').valid?}.should raise_error(VatValidator::ViesContactError)
|
23
|
+
end
|
14
24
|
end
|
metadata
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vat_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 11
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: "1.
|
8
|
+
- 2
|
9
|
+
version: "1.2"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
|
-
-
|
12
|
+
- Aurelien Malisart
|
13
|
+
- Francois Stephany
|
13
14
|
autorequire:
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
-
default_executable:
|
18
|
+
date: 2011-09-30 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: activemodel
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
@@ -32,19 +32,37 @@ dependencies:
|
|
32
32
|
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
|
-
name:
|
35
|
+
name: savon
|
36
36
|
prerelease: false
|
37
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
38
|
none: false
|
39
39
|
requirements:
|
40
40
|
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
hash:
|
42
|
+
hash: 53
|
43
43
|
segments:
|
44
44
|
- 0
|
45
|
-
|
45
|
+
- 9
|
46
|
+
- 7
|
47
|
+
version: 0.9.7
|
46
48
|
type: :runtime
|
47
49
|
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 15
|
59
|
+
segments:
|
60
|
+
- 2
|
61
|
+
- 0
|
62
|
+
- 0
|
63
|
+
version: 2.0.0
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
48
66
|
description: Validator for european countries VAT numbers for ActiveModel
|
49
67
|
email: aurelien.malisart@gmail.com
|
50
68
|
executables: []
|
@@ -54,6 +72,7 @@ extensions: []
|
|
54
72
|
extra_rdoc_files:
|
55
73
|
- README.rdoc
|
56
74
|
files:
|
75
|
+
- lib/exceptions.rb
|
57
76
|
- lib/vat_validator.rb
|
58
77
|
- lib/vies_checker.rb
|
59
78
|
- spec/spec_helper.rb
|
@@ -61,7 +80,6 @@ files:
|
|
61
80
|
- spec/vat_validator/vies_checker_spec.rb
|
62
81
|
- README.rdoc
|
63
82
|
- Rakefile
|
64
|
-
has_rdoc: true
|
65
83
|
homepage: http://github.com/aurels/vat_validator
|
66
84
|
licenses: []
|
67
85
|
|
@@ -93,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
111
|
requirements: []
|
94
112
|
|
95
113
|
rubyforge_project:
|
96
|
-
rubygems_version: 1.
|
114
|
+
rubygems_version: 1.8.5
|
97
115
|
signing_key:
|
98
116
|
specification_version: 3
|
99
117
|
summary: Validator for european countries VAT numbers for ActiveModel
|