Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yolk committed Jan 6, 2011
0 parents commit fa3e9d6
Show file tree
Hide file tree
Showing 19 changed files with 604 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg/*
*.gem
.bundle
1 change: 1 addition & 0 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm use ruby-1.9.2@valvat
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in valvat.gemspec
gemspec
56 changes: 56 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
PATH
remote: .
specs:
valvat (0.0.1)
savon (>= 0.8.2)

GEM
remote: http://rubygems.org/
specs:
builder (3.0.0)
configuration (1.2.0)
crack (0.1.8)
diff-lcs (1.1.2)
growl (1.0.3)
guard (0.2.2)
open_gem (~> 1.4.2)
thor (~> 0.14.3)
guard-rspec (0.1.9)
guard (>= 0.2.2)
gyoku (0.1.1)
builder (>= 2.1.2)
httpi (0.7.7)
rack
launchy (0.3.7)
configuration (>= 0.0.5)
rake (>= 0.8.1)
open_gem (1.4.2)
launchy (~> 0.3.5)
rack (1.2.1)
rake (0.8.7)
rb-fsevent (0.3.9)
rspec (2.4.0)
rspec-core (~> 2.4.0)
rspec-expectations (~> 2.4.0)
rspec-mocks (~> 2.4.0)
rspec-core (2.4.0)
rspec-expectations (2.4.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.4.0)
savon (0.8.2)
builder (>= 2.1.2)
crack (~> 0.1.8)
gyoku (>= 0.1.1)
httpi (>= 0.7.5)
thor (0.14.6)

PLATFORMS
ruby

DEPENDENCIES
growl (>= 1.0.3)
guard-rspec (>= 0.1.9)
rb-fsevent (>= 0.3.9)
rspec (>= 2.4.0)
savon (>= 0.8.2)
valvat!
5 changes: 5 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
guard 'rspec', :version => 2 do
watch('^spec/(.*)_spec.rb')
watch('^lib/(.*)\.rb') { |m| "spec/#{m[1]}_spec.rb" }
watch('^spec/spec_helper.rb') { "spec" }
end
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2011 Yolk Sebastian Munz & Julia Soergel GbR

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 changes: 33 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
== valvat

Validates european vat numbers. Supports simple syntax verification and lookup via the VIES web service.

== Installation

gem install valvat

== Basic Usage

To verify the syntax of an vat number:

Valvat::Syntax.validate("DE345789003")
=> true or false

To check if the given vat number exists:

Valvat::Lookup.validate("DE345789003")
=> true or false or nil

Keep in mind that the VIES webservice might be offline at some time for some countries. If this happens Valvat::Lookup.validate returns nil.

See http://ec.europa.eu/taxation_customs/vies/viesspec.do for more accurate information at what time the service for a specific country will be down.

== Links

* http://ec.europa.eu/taxation_customs/vies
* http://www.bzst.de/DE/Steuern_International/USt_Identifikationsnummer/Merkblaetter/Aufbau_USt_IdNr.html (german)
* http://en.wikipedia.org/wiki/European_Union_Value_Added_Tax

== BlaBla

Copyright (c) 2011 Yolk Sebastian Munz & Julia Soergel GbR
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
8 changes: 8 additions & 0 deletions lib/valvat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Valvat

end

require 'valvat/utils'
require 'valvat/syntax'
require 'valvat/lookup'
require 'valvat/version'
33 changes: 33 additions & 0 deletions lib/valvat/lookup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'valvat/utils'

module Valvat
module Lookup

def self.validate(vat)
country_code, vat_number = Valvat::Utils.split(vat)
response = client.request "n1", "checkVat" do
soap.body = {"n1:countryCode" => country_code, "n1:vatNumber" => vat_number}
soap.namespaces["xmlns:n1"] = "urn:ec.europa.eu:taxud:vies:services:checkVat:types"
end
response.to_hash[:check_vat_response][:valid]
rescue => err
false if err.to_hash[:fault] && err.to_hash[:faultstring] = 'INVALID_INPUT'
nil
end

def self.client
@client ||= begin
# Require Savon only if really needed!
require 'savon' unless defined?(Savon)

# Quiet down Savon and HTTPI
Savon.logger.level = Logger::WARN
HTTPI.logger.level = Logger::WARN

Savon::Client.new do
wsdl.document = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/valvat/syntax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'valvat/utils'

module Valvat
class InvalidSyntax < StandardError;end

module Syntax

VAT_PATTERNS = {
'AT' => /\AATU[0-9]{8}\Z/, # Austria
'BE' => /\ABE[0-9]{10}\Z/, # Belgium
'BG' => /\ABG[0-9]{9,10}\Z/, # Bulgaria
'CY' => /\ACY[0-9]{8}[A-Z]\Z/, # Cyprus
'CZ' => /\ACZ[0-9]{8,10}\Z/, # Czech Republic
'DE' => /\ADE[0-9]{9}\Z/, # Germany
'DK' => /\ADK[0-9]{8}\Z/, # Denmark
'EE' => /\AEE[0-9]{9}\Z/, # Estonia
'EL' => /\AEL[0-9]{9}\Z/, # Greece
'ES' => /\AES([A-Z][0-9]{8}|[0-9]{8}[A-Z]|[A-Z][0-9]{7}[A-Z])\Z/, # Spain
'FI' => /\AFI[0-9]{8}\Z/, # Finland
'FR' => /\AFR[A-Z0-9]{2}[0-9]{9}\Z/, # France
'GB' => /\AGB([0-9]{9}|[0-9]{12}|(HA|GD)[0-9]{3})\Z/, # United Kingdom
'HU' => /\AHU[0-9]{8}\Z/, # Hungary
'IE' => /\AIE([0-9][A-Z][0-9]{5}|[0-9]{7})[A-Z]\Z/, # Ireland
'IT' => /\AIT[0-9]{11}\Z/, # Italy
'LT' => /\ALT([0-9]{9}|[0-9]{12})\Z/, # Lithuania
'LU' => /\ALU[0-9]{8}\Z/, # Luxembourg
'LV' => /\ALV[0-9]{11}\Z/, # Latvia
'MT' => /\AMT[0-9]{8}\Z/, # Malta
'NL' => /\ANL[0-9]{9}B[0-9]{2}\Z/, # Netherlands
'PL' => /\APL[0-9]{10}\Z/, # Poland
'PT' => /\APT[0-9]{9}\Z/, # Portugal
'RO' => /\ARO[1-9][0-9]{1,9}\Z/, # Romania
'SE' => /\ASE[0-9]{10}01\Z/, # Sweden
'SI' => /\ASI[0-9]{8}\Z/, # Slovenia
'SK' => /\ASK[0-9]{10}\Z/ # Slovakia
}

def self.validate(vat)
pattern = VAT_PATTERNS[Valvat::Utils.split(vat)[0]]
!!(pattern && pattern =~ vat)
end

def self.validate!(vat)
validate(vat) || raise(Valvat::InvalidSyntax.new("#{vat.inspect} is an invalid vat number!"))
end
end

end
15 changes: 15 additions & 0 deletions lib/valvat/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Valvat
module Utils

COUNTRY_PATTERN = /\A([A-Z]{2})(.+)\Z/

def self.split(vat)
COUNTRY_PATTERN =~ vat
[$1, $2]
end

def self.normalize(vat)
vat.upcase.gsub(/\A\s+|\s+\Z/, "")
end
end
end
3 changes: 3 additions & 0 deletions lib/valvat/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Valvat
VERSION = "0.0.1"
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rspec'

require File.dirname(__FILE__) + '/../lib/valvat.rb'
17 changes: 17 additions & 0 deletions spec/valvat/lookup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe Valvat::Lookup do
context "#validate" do
it "returns true on existing vat number" do
Valvat::Lookup.validate("BE0817331995").should eql(true)
end

it "returns false on not existing vat number" do
Valvat::Lookup.validate("BE0817331994").should eql(false)
end

it "returns nil on invalid input" do
Valvat::Lookup.validate("AE259597697").should eql(nil)
end
end
end
Loading

0 comments on commit fa3e9d6

Please sign in to comment.