vat_validator 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Use this plugin to validate VAT numbers for european countries.
4
4
 
5
- = Usage
5
+ = Basic Usage
6
6
 
7
7
  To validate a model field as a valid intracom VAT number :
8
8
 
@@ -39,6 +39,16 @@ Example :
39
39
  end
40
40
  end
41
41
 
42
+ == Validating the Number at VIES
43
+
44
+ It's also possible to have the VAT number validated against Europa's VIES
45
+ webservice. Simply use the :vies option like this :
46
+
47
+ validates :vat_number, :vat => { :vies => true }
48
+
49
+ Keep in mind that the webservice might be offline at some time for some
50
+ countries. Check the VIES FAQ for more information about that.
51
+
42
52
  = Installation
43
53
 
44
54
  In your project's Gemfile :
data/lib/vat_validator.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'active_model'
2
+ require 'vies_checker'
2
3
 
3
4
  module VatValidator
4
5
 
@@ -38,14 +39,24 @@ module VatValidator
38
39
 
39
40
  class VatValidator < ActiveModel::EachValidator
40
41
  def validate_each(record, attribute, value)
42
+ format_valid = true
43
+
41
44
  if options[:country_method]
42
45
  country_code = record.send(options[:country_method]).to_s
43
46
  unless VAT_PATTERNS.has_key?(country_code) && value.to_s =~ VAT_PATTERNS[country_code]
44
47
  record.errors.add(attribute, options[:message])
48
+ format_valid = false
45
49
  end
46
50
  else
47
51
  unless value =~ VAT_PATTERNS.values.detect { |p| value.to_s =~ p }
48
52
  record.errors.add(attribute, options[:message])
53
+ format_valid = false
54
+ end
55
+ end
56
+
57
+ if format_valid && options[:vies]
58
+ unless ViesChecker.check(value.to_s[0..1], value.to_s[2..15])
59
+ record.errors.add(attribute, options[:message])
49
60
  end
50
61
  end
51
62
  end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'soap/wsdlDriver'
3
+
4
+ module VatValidator
5
+ class ViesChecker
6
+
7
+ WSDL = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'
8
+
9
+ class << self
10
+ def check(country_code, vat_number)
11
+ begin
12
+ proxy = SOAP::WSDLDriverFactory.new(WSDL).create_rpc_driver
13
+ return proxy.checkVat(:countryCode => country_code, :vatNumber => vat_number).valid
14
+ rescue
15
+ false
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/spec/spec_helper.rb CHANGED
@@ -46,4 +46,8 @@ class CountryCheckedInvoice < BaseTestModel
46
46
  else nil
47
47
  end
48
48
  end
49
+ end
50
+
51
+ class ViesCheckedInvoice < BaseTestModel
52
+ validates :vat_number, :vat => {:vies => true}
49
53
  end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ # Note : this SPEC collection makes direct calls to the VIES webservice.
4
+ # Maybe I need to use a mock ?
5
+
6
+ describe ViesCheckedInvoice do
7
+ it "should be valid if vat number is correct" do
8
+ ViesCheckedInvoice.new(:vat_number => 'BE0817331995').should be_valid
9
+ end
10
+
11
+ it "should not be valid if vat number is correct" do
12
+ ViesCheckedInvoice.new(:vat_number => 'BE000678345').should_not be_valid
13
+ end
14
+ end
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vat_validator
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 13
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 0
8
- version: "1.0"
8
+ - 1
9
+ version: "1.1"
9
10
  platform: ruby
10
11
  authors:
11
12
  - "Aur\xC3\xA9lien Malisart"
@@ -13,10 +14,37 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2010-05-19 00:00:00 +02:00
17
+ date: 2010-06-25 00:00:00 +02:00
17
18
  default_executable:
18
- dependencies: []
19
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: soap
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: soap4r
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
20
48
  description: Validator for european countries VAT numbers for ActiveModel
21
49
  email: aurelien.malisart@gmail.com
22
50
  executables: []
@@ -27,8 +55,10 @@ extra_rdoc_files:
27
55
  - README.rdoc
28
56
  files:
29
57
  - lib/vat_validator.rb
58
+ - lib/vies_checker.rb
30
59
  - spec/spec_helper.rb
31
60
  - spec/vat_validator/validations_spec.rb
61
+ - spec/vat_validator/vies_checker_spec.rb
32
62
  - README.rdoc
33
63
  - Rakefile
34
64
  has_rdoc: true
@@ -41,16 +71,20 @@ rdoc_options: []
41
71
  require_paths:
42
72
  - lib
43
73
  required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
44
75
  requirements:
45
76
  - - ">="
46
77
  - !ruby/object:Gem::Version
78
+ hash: 3
47
79
  segments:
48
80
  - 0
49
81
  version: "0"
50
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
51
84
  requirements:
52
85
  - - ">="
53
86
  - !ruby/object:Gem::Version
87
+ hash: 23
54
88
  segments:
55
89
  - 1
56
90
  - 3
@@ -59,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
93
  requirements: []
60
94
 
61
95
  rubyforge_project:
62
- rubygems_version: 1.3.6
96
+ rubygems_version: 1.3.7
63
97
  signing_key:
64
98
  specification_version: 3
65
99
  summary: Validator for european countries VAT numbers for ActiveModel