validates_as_vat_number 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,5 @@
1
+ require 'autotest/growl'
2
+ Autotest::Growl::show_modified_files = true #which changes prompted the autospec run
3
+ Autotest.add_hook :initialize do |at|
4
+ %w{.git .svn .hg .DS_Store ._* log}.each {|exception|at.add_exception(exception)}
5
+ end
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Develon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,34 @@
1
+ ## validates_as_vat_number
2
+
3
+ Now you can validate a VAT using the European service called VIES, to ensure it exists. Only require this gem and call validate_as_vat_number on your VAT field, in an Active Record object.
4
+
5
+ ## Install
6
+
7
+ Specify it in your Rails config.
8
+
9
+ config.gem 'validates_as_vat_number', :source => 'http://gemcutter.org'
10
+
11
+ Then install it.
12
+
13
+ rake gems:install
14
+
15
+ ## Example
16
+
17
+
18
+ class Company < ActiveRecord::Base
19
+ validates_as_vat_number :vat
20
+ end
21
+
22
+ ## Note on Patches/Pull Requests
23
+
24
+ * Fork the project.
25
+ * Make your feature addition or bug fix.
26
+ * Add tests for it. This is important so I don't break it in a
27
+ future version unintentionally.
28
+ * Commit, do not mess with rakefile, version, or history.
29
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
30
+ * Send me a pull request. Bonus points for topic branches.
31
+
32
+ ## Copyright
33
+
34
+ Copyright (c) 2009 Develon. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "validates_as_vat_number"
8
+ gem.summary = "Permits VAT validation within ActiveRecord using EU VIES Web Service."
9
+ gem.description = "Now you can validate a VAT using the European service called VIES,
10
+ to ensure it exists. Only require this gem and call validate_as_vat_number
11
+ on your VAT field, in an Active Record object."
12
+ gem.email = "lab@develon.com"
13
+ gem.homepage = "http://github.com/develon/validates_as_vat_number"
14
+ gem.authors = ["Alessandro Dal Grande", "Marco Sanson"]
15
+ gem.add_development_dependency "rspec", ">= 1.2.9"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "validates_as_vat_number #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,48 @@
1
+ module Develon
2
+ module ValidatesAsVatNumber
3
+ require 'soap/wsdlDriver'
4
+
5
+ def validates_as_vat_number(*attr_names)
6
+ configuration = {
7
+ :message => 'is an invalid VAT number',
8
+ :allow_nil => false
9
+ }
10
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
11
+
12
+ iso3661_country_codes = %w(AF AX AL DZ AS AD AO AI AQ AG AR AM AW AU AT AZ BS BH BD BB BY BE BZ BJ BM BT BO BA BW BV BR IO BN BG BF BI KH CM CA CV KY CF TD CL CN CX CC CO KM CG CD CK CR CI HR CU CY CZ DK DJ DM DO EC EG SV GQ ER EE ET FK FO FJ FI FR GF PF TF GA GM GE DE GH GI GR GL GD GP GU GT GG GN GW GY HT HM VA HN HK HU IS IN ID IR IQ IE IM IL IT JM JP JE JO KZ KE KI KP KR KW KG LA LV LB LS LR LY LI LT LU MO MK MG MW MY MV ML MT MH MQ MR MU YT MX FM MD MC MN ME MS MA MZ MM NA NR NP NL AN NC NZ NI NE NG NU NF MP NO OM PK PW PS PA PG PY PE PH PN PL PT PR QA RE RO RU RW BL SH KN LC MF PM VC WS SM ST SA SN RS SC SL SG SK SI SB SO ZA GS ES LK SD SR SJ SZ SE CH SY TW TJ TZ TH TL TG TK TO TT TN TR TM TC TV UG UA AE GB US UM UY UZ VU VE VN VG VI WF EH YE ZM ZW)
13
+
14
+ structures = { 'AT' => /^ATU\w{8}$/, 'BE' => /^BE0\d{9}$/, 'BG' => /^BG\d{9,10}$/, 'CY' => /^CY\w{8}L$/,
15
+ 'CZ' => /^CZ\d{8,10}$/, 'DE' => /^DE\d{9}$/, 'DK' => /^DK\d{2}\s\d{2}\s\d{2}\s\d{2}$/,
16
+ 'EE' => /^EE\d{9}$/, 'EL' => /^EL\d{9}$/, 'ES' => /^ESX\d{7}X$/, 'FI' => /^FI\d{8}$/, 'FR' => /^FR\w{2}\s\d{9}$/,
17
+ 'GB' => /^GB(\d{3}\s\d{4}\s\d{2}|\d{3}\s\d{4}\s\d{2}\s\d{3}|GD\d{3}|HA\d{3})$/,
18
+ 'HU' => /^HU\d{8}$/, 'IE' => /^IE\wS\w{5}L$/, 'IT' => /^IT\d{11}$/, 'LT' => /^LT(\d{9}|\d{12})$/,
19
+ 'LU' => /^LU\d{8}$/, 'LV' => /^LV\d{12}$/, 'MT' => /^MT\d{8}$/, 'NL' => /^NL\w{9}B\w{2}$/,
20
+ 'PL' => /^PL\d{10}$/, 'PT' => /^PT\d{9}/, 'RO' => /^RO\d{2,10}$/, 'SE' => /^SE\d{12}$/, 'SI' => /^SI\d{8}$/,
21
+ 'SK' =>/^SK\d{10}$/ }
22
+
23
+ validates_each(attr_names,configuration) do |record, attr_name, value|
24
+ if iso3661_country_codes.include?(value[0,2].upcase)
25
+ if structures[value[0,2].upcase].match(value).nil? || check_vat(value[0,2], value.gsub(/^\w\w/, '')) == false
26
+ record.errors.add(attr_name, :not_valid, :default => configuration[:message])
27
+ end
28
+ else
29
+ record.errors.add(attr_name, 'has an invalid country')
30
+ end
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def vies_driver
37
+ wsdl = "http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl"
38
+ @driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
39
+ end
40
+
41
+ def check_vat(country_code, vat_number)
42
+ @driver = vies_driver unless @driver
43
+ @driver.checkVat(:countryCode => country_code, :vatNumber => vat_number).valid == 'true'
44
+ end
45
+ end
46
+ end
47
+
48
+ ActiveRecord::Base.extend Develon::ValidatesAsVatNumber
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/lib"
2
+ require 'validates_as_vat_number'
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require File.dirname(__FILE__) + '/../lib/validates_as_vat_number'
6
+ require 'spec'
7
+ require 'spec/autorun'
8
+
9
+ require File.join(File.dirname(__FILE__), '..', 'init')
10
+
11
+ Spec::Runner.configure do |config|
12
+
13
+ end
14
+
15
+ class Company < ActiveRecord::Base
16
+ validates_as_vat_number :vat
17
+
18
+ def self.columns() @columns ||= []; end
19
+
20
+ def self.column(name, sql_type = nil, default = nil, null = true)
21
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
22
+ end
23
+
24
+ column :name, :string
25
+ column :vat, :string
26
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Develon::ValidatesAsVatNumber do
4
+ it "is included" do
5
+ class A < ActiveRecord::Base
6
+ validates_as_vat_number :vat
7
+ end
8
+ end
9
+
10
+ it "should validate Develon Company" do
11
+ develon = Company.new(:name => 'Develon', :vat => 'IT03018900245')
12
+ develon.valid?.should == true
13
+ end
14
+
15
+ it "should invalidate a fake company" do
16
+ fake_company = Company.new(:name => 'Fake Company', :vat => 'IT000003018')
17
+ fake_company.valid?.should == false
18
+ fake_company.errors.on('vat').should == 'is an invalid VAT number'
19
+ end
20
+
21
+ it "should invalidate locally if country is not valid" do
22
+ develon = Company.new(:name => 'Develon', :vat => 'KO03018900245')
23
+ develon.valid?.should == false
24
+ develon.errors.on('vat').should == 'has an invalid country'
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_as_vat_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Dal Grande
8
+ - Marco Sanson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-01-07 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.2.9
25
+ version:
26
+ description: |-
27
+ Now you can validate a VAT using the European service called VIES,
28
+ to ensure it exists. Only require this gem and call validate_as_vat_number
29
+ on your VAT field, in an Active Record object.
30
+ email: lab@develon.com
31
+ executables: []
32
+
33
+ extensions: []
34
+
35
+ extra_rdoc_files:
36
+ - LICENSE
37
+ - README.markdown
38
+ files:
39
+ - .autotest
40
+ - .document
41
+ - .gitignore
42
+ - LICENSE
43
+ - README.markdown
44
+ - Rakefile
45
+ - VERSION
46
+ - init.rb
47
+ - lib/validates_as_vat_number.rb
48
+ - rails/init.rb
49
+ - spec/spec.opts
50
+ - spec/spec_helper.rb
51
+ - spec/validates_as_vat_number_spec.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/develon/validates_as_vat_number
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.5
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Permits VAT validation within ActiveRecord using EU VIES Web Service.
80
+ test_files:
81
+ - spec/spec_helper.rb
82
+ - spec/validates_as_vat_number_spec.rb