valvat 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.md +8 -1
- data/README.md +65 -30
- data/lib/valvat.rb +38 -1
- data/lib/valvat/active_model.rb +6 -6
- data/lib/valvat/lookup.rb +7 -7
- data/lib/valvat/syntax.rb +5 -4
- data/lib/valvat/utils.rb +7 -4
- data/lib/valvat/version.rb +2 -2
- data/spec/spec_helper.rb +10 -0
- data/spec/valvat/lookup_spec.rb +5 -3
- data/spec/valvat/syntax_spec.rb +5 -0
- data/spec/valvat/utils_spec.rb +14 -2
- data/spec/valvat_spec.rb +244 -0
- data/valvat.gemspec +2 -2
- metadata +5 -5
data/CHANGES.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
### dev
|
2
2
|
|
3
|
-
[full changelog](http://github.com/yolk/valvat/compare/v0.
|
3
|
+
[full changelog](http://github.com/yolk/valvat/compare/v0.2.0...master)
|
4
|
+
|
5
|
+
### 0.2.0 / 2011-01-07
|
6
|
+
|
7
|
+
[full changelog](http://github.com/yolk/valvat/compare/v0.1.1...v0.2.0)
|
8
|
+
|
9
|
+
* Rewrote Valvat module to a vat number class for convenience + internal use of Valvat instances
|
10
|
+
* I18n: Default error messages in german
|
4
11
|
|
5
12
|
### 0.1.1 / 2011-01-07
|
6
13
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
## valvat
|
2
2
|
|
3
|
-
Validates european vat numbers. Standalone or as
|
3
|
+
Validates european vat numbers. Standalone or as a ActiveModel validator.
|
4
4
|
|
5
5
|
### Features
|
6
6
|
|
@@ -14,72 +14,107 @@ valvat is tested and works with ruby 1.8.7/1.9.2 and ActiveModel 3.0.
|
|
14
14
|
|
15
15
|
### Installation
|
16
16
|
|
17
|
-
|
17
|
+
gem install valvat
|
18
18
|
|
19
19
|
### Basic Usage
|
20
20
|
|
21
21
|
To verify the syntax of a vat number:
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
Valvat.new("DE345789003").valid?
|
24
|
+
=> true or false
|
25
25
|
|
26
|
-
To check if the given vat number exists:
|
26
|
+
To check if the given vat number exists via the VIES web service:
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
Valvat.new("DE345789003").exists?
|
29
|
+
=> true or false or nil
|
30
30
|
|
31
|
-
Keep in mind that the VIES
|
31
|
+
Keep in mind that the VIES web service might be offline at some time for some countries. If this happens `Valvat::Lookup.validate` returns `nil`.
|
32
32
|
|
33
|
-
|
33
|
+
Visit [http://ec.europa.eu/taxation_customs/vies/viesspec.do](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.
|
34
|
+
|
35
|
+
It is also possible to bypass initializing a Valvat instance and check the syntax of a var number string directly with:
|
36
|
+
|
37
|
+
Valvat::Syntax.validate("DE345789003")
|
38
|
+
=> true or false
|
39
|
+
|
40
|
+
Or to lookup a vat number string directly via VIES web service:
|
41
|
+
|
42
|
+
Valvat::Lookup.validate("DE345789003")
|
43
|
+
=> true or false or nil
|
34
44
|
|
35
45
|
### ActiveModel/Rails3 Usage
|
36
46
|
|
37
47
|
When the valvat gem is required and ActiveModel is already loaded, everything will work fine out of the box. If your load order differs just add
|
38
48
|
|
39
|
-
|
49
|
+
require 'valvat/active_model'
|
40
50
|
|
41
51
|
after ActiveModel has been loaded.
|
42
52
|
|
43
|
-
To validate the attribute
|
53
|
+
To validate the attribute `vat_number` add this to your model:
|
44
54
|
|
45
|
-
|
46
|
-
|
47
|
-
|
55
|
+
class MyModel < ActiveRecord::Base
|
56
|
+
validates :vat_number, :valvat => true
|
57
|
+
end
|
48
58
|
|
49
59
|
To additionally perform a lookup via VIES:
|
50
60
|
|
51
|
-
|
61
|
+
validates :vat_number, :valvat => {:lookup => true}
|
52
62
|
|
53
|
-
By default this will validate to true if the VIES web service is down. To fail in this case simply add the
|
63
|
+
By default this will validate to true if the VIES web service is down. To fail in this case simply add the `:fail_if_down` option:
|
54
64
|
|
55
|
-
|
65
|
+
validates :vat_number, :valvat => {:lookup => :fail_if_down}
|
56
66
|
|
57
|
-
By default blank vat numbers validate to false
|
67
|
+
By default blank vat numbers validate to false. To change this add the `:allow_blank` option:
|
68
|
+
|
69
|
+
validates :vat_number, :valvat => {:allow_blank => true}
|
70
|
+
|
71
|
+
To allow vat numbers from outside of europe, add something like this to your model (country_code should return a upcase iso country code):
|
58
72
|
|
59
|
-
|
73
|
+
class MyModel < ActiveRecord::Base
|
74
|
+
validates :vat_number, :valvat => true, :if => :eu?
|
75
|
+
|
76
|
+
def eu?
|
77
|
+
Valvat::Utils::EU_COUNTRIES.include?(country_code)
|
78
|
+
end
|
79
|
+
end
|
60
80
|
|
61
81
|
### Utilities
|
62
82
|
|
63
|
-
To split a vat number into the
|
83
|
+
To split a vat number into the country code and the remaining chars:
|
64
84
|
|
65
|
-
|
66
|
-
|
85
|
+
Valvat::Utils.split("ATU345789003")
|
86
|
+
=> ["AT", "U345789003"]
|
87
|
+
|
88
|
+
or
|
89
|
+
|
90
|
+
Valvat.new("ATU345789003").to_a
|
91
|
+
=> ["AT", "U345789003"]
|
67
92
|
|
68
|
-
|
93
|
+
Both methods always return an array. If it can not detect the country or the given country is located outside of europe it returns `[nil, nil]`. Please note that this does not strictly return the iso country code: for greek vat numbers this returns the iso language code 'EL' instead of the iso country code 'GR'.
|
94
|
+
|
95
|
+
To extract the iso country code of a given vat number:
|
96
|
+
|
97
|
+
Valvat.new("EL7345789003").iso_country_code
|
98
|
+
=> "GR"
|
99
|
+
|
100
|
+
To extract the vat country code (first two chars in every european vat number):
|
101
|
+
|
102
|
+
Valvat.new("EL7345789003").vat_country_code
|
103
|
+
=> "EL"
|
69
104
|
|
70
105
|
To normalize a vat number:
|
71
106
|
|
72
|
-
|
73
|
-
|
107
|
+
Valvat::Utils.normalize("atu345789003")
|
108
|
+
=> "ATU345789003"
|
74
109
|
|
75
|
-
This basically just removes trailing spaces and ensures all chars are
|
110
|
+
This basically just removes trailing spaces and ensures all chars are uppercase.
|
76
111
|
|
77
112
|
### Links
|
78
113
|
|
79
|
-
* http://ec.europa.eu/taxation_customs/vies
|
80
|
-
* http://bzst.de/DE/Steuern_International/USt_Identifikationsnummer/Merkblaetter/Aufbau_USt_IdNr.html
|
81
|
-
* http://en.wikipedia.org/wiki/European_Union_Value_Added_Tax
|
82
|
-
* http://isvat.appspot.com/
|
114
|
+
* [VIES web service](http://ec.europa.eu/taxation_customs/vies)
|
115
|
+
* [European vat number formats (german)](http://bzst.de/DE/Steuern_International/USt_Identifikationsnummer/Merkblaetter/Aufbau_USt_IdNr.html)
|
116
|
+
* [European vat number formats on Wikipedia](http://en.wikipedia.org/wiki/European_Union_Value_Added_Tax)
|
117
|
+
* [isvat VIES REST wrapper](http://isvat.appspot.com/)
|
83
118
|
|
84
119
|
### BlaBla
|
85
120
|
|
data/lib/valvat.rb
CHANGED
@@ -1,5 +1,42 @@
|
|
1
|
-
|
1
|
+
class Valvat
|
2
|
+
def initialize(raw)
|
3
|
+
@raw = raw || ""
|
4
|
+
@vat_country_code, @to_s_wo_country = to_a
|
5
|
+
end
|
2
6
|
|
7
|
+
attr_reader :raw, :vat_country_code, :to_s_wo_country
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
Valvat::Syntax.validate(self)
|
11
|
+
end
|
12
|
+
|
13
|
+
def exists?
|
14
|
+
Valvat::Lookup.validate(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
def iso_country_code
|
18
|
+
Valvat::Utils.vat_country_to_iso_country(vat_country_code)
|
19
|
+
end
|
20
|
+
|
21
|
+
def european?
|
22
|
+
Valvat::Utils::EU_COUNTRIES.include?(iso_country_code)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_a
|
26
|
+
Valvat::Utils.split(raw)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
raw
|
31
|
+
end
|
32
|
+
|
33
|
+
def inspect
|
34
|
+
"#<Valvat #{[raw, iso_country_code].compact.join(" ")}>"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def Valvat(vat)
|
39
|
+
vat.is_a?(Valvat) ? vat : Valvat.new(vat)
|
3
40
|
end
|
4
41
|
|
5
42
|
require 'valvat/utils'
|
data/lib/valvat/active_model.rb
CHANGED
@@ -7,12 +7,10 @@ module ActiveModel
|
|
7
7
|
class ValvatValidator < ::ActiveModel::EachValidator
|
8
8
|
|
9
9
|
def validate_each(record, attribute, value)
|
10
|
-
|
10
|
+
vat = Valvat(value)
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
is_valid.nil? && is_valid = (options[:lookup] != :fail_if_down)
|
15
|
-
end
|
12
|
+
is_valid = options[:lookup] ? vat.valid? && vat.exists? : vat.valid?
|
13
|
+
is_valid.nil? && is_valid = (options[:lookup] != :fail_if_down)
|
16
14
|
|
17
15
|
unless is_valid
|
18
16
|
record.errors.add(attribute, :invalid_vat,
|
@@ -28,4 +26,6 @@ module ActiveModel
|
|
28
26
|
end
|
29
27
|
end
|
30
28
|
|
31
|
-
|
29
|
+
%w(en de).each do |locale|
|
30
|
+
I18n.load_path << "#{File.dirname(__FILE__)}/../locale/#{locale}.yml"
|
31
|
+
end
|
data/lib/valvat/lookup.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
require 'valvat
|
1
|
+
require 'valvat'
|
2
2
|
require 'net/http'
|
3
3
|
require 'yaml'
|
4
4
|
|
5
|
-
|
5
|
+
class Valvat
|
6
6
|
module Lookup
|
7
7
|
def self.validate(vat)
|
8
|
-
|
9
|
-
return false unless
|
10
|
-
|
8
|
+
vat = Valvat(vat)
|
9
|
+
return false unless vat.european?
|
11
10
|
result = begin
|
12
11
|
YAML.load(Net::HTTP.start("isvat.appspot.com", 80) {|http|
|
13
|
-
http.get("/#{
|
12
|
+
http.get("/#{vat.to_a.join("/")}/")
|
14
13
|
}.body)
|
15
|
-
rescue
|
14
|
+
rescue => err
|
15
|
+
raise if FakeWeb::NetConnectNotAllowedError === err
|
16
16
|
nil
|
17
17
|
end
|
18
18
|
|
data/lib/valvat/syntax.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require 'valvat
|
1
|
+
require 'valvat'
|
2
2
|
|
3
|
-
|
3
|
+
class Valvat
|
4
4
|
module Syntax
|
5
5
|
|
6
6
|
VAT_PATTERNS = {
|
@@ -34,8 +34,9 @@ module Valvat
|
|
34
34
|
}
|
35
35
|
|
36
36
|
def self.validate(vat)
|
37
|
-
|
38
|
-
|
37
|
+
vat = Valvat(vat)
|
38
|
+
pattern = VAT_PATTERNS[vat.iso_country_code]
|
39
|
+
!!(pattern && pattern =~ vat.to_s)
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
data/lib/valvat/utils.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
class Valvat
|
2
2
|
module Utils
|
3
3
|
|
4
4
|
EU_COUNTRIES = %w(AT BE BG CY CZ DE DK EE ES FI FR GB GR HU IE IT LT LU LV MT NL PL PT RO SE SI SK)
|
@@ -7,13 +7,16 @@ module Valvat
|
|
7
7
|
def self.split(vat)
|
8
8
|
COUNTRY_PATTERN =~ vat
|
9
9
|
result = [$1, $2]
|
10
|
-
|
11
|
-
|
12
|
-
result
|
10
|
+
iso_country = vat_country_to_iso_country(result[0])
|
11
|
+
EU_COUNTRIES.include?(iso_country) ? result : [nil, nil]
|
13
12
|
end
|
14
13
|
|
15
14
|
def self.normalize(vat)
|
16
15
|
vat.upcase.gsub(/\A\s+|\s+\Z/, "")
|
17
16
|
end
|
17
|
+
|
18
|
+
def self.vat_country_to_iso_country(vat_country)
|
19
|
+
vat_country == "EL" ? "GR" : vat_country
|
20
|
+
end
|
18
21
|
end
|
19
22
|
end
|
data/lib/valvat/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.
|
1
|
+
class Valvat
|
2
|
+
VERSION = "0.2.0"
|
3
3
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,4 +19,14 @@ class ModelBase
|
|
19
19
|
def read_attribute_for_validation(key)
|
20
20
|
@attributes[key]
|
21
21
|
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def without_any_web_requests!
|
25
|
+
before(:all) do
|
26
|
+
FakeWeb.clean_registry
|
27
|
+
FakeWeb.allow_net_connect = false
|
28
|
+
end
|
29
|
+
after(:all) do
|
30
|
+
FakeWeb.allow_net_connect = true
|
31
|
+
end
|
22
32
|
end
|
data/spec/valvat/lookup_spec.rb
CHANGED
@@ -10,6 +10,10 @@ describe Valvat::Lookup do
|
|
10
10
|
it "returns true" do
|
11
11
|
Valvat::Lookup.validate("BE0817331995").should eql(true)
|
12
12
|
end
|
13
|
+
|
14
|
+
it "allows Valvat instance as input" do
|
15
|
+
Valvat::Lookup.validate(Valvat.new("BE0817331995")).should eql(true)
|
16
|
+
end
|
13
17
|
end
|
14
18
|
|
15
19
|
context "not existing vat number" do
|
@@ -23,9 +27,7 @@ describe Valvat::Lookup do
|
|
23
27
|
end
|
24
28
|
|
25
29
|
context "invalid country code / input" do
|
26
|
-
|
27
|
-
FakeWeb.register_uri(:get, "http://isvat.appspot.com/AE/259597697/", :body => "false")
|
28
|
-
end if $fakeweb
|
30
|
+
without_any_web_requests!
|
29
31
|
|
30
32
|
it "returns false" do
|
31
33
|
Valvat::Lookup.validate("AE259597697").should eql(false)
|
data/spec/valvat/syntax_spec.rb
CHANGED
@@ -266,5 +266,10 @@ describe Valvat::Syntax do
|
|
266
266
|
Valvat::Syntax.validate("xxxxxxxxxx").should eql(false)
|
267
267
|
Valvat::Syntax.validate("BEFR").should eql(false)
|
268
268
|
end
|
269
|
+
|
270
|
+
it "allows Valvat instance as input" do
|
271
|
+
Valvat::Syntax.validate(Valvat.new("DE345889003")).should eql(true)
|
272
|
+
Valvat::Syntax.validate(Valvat.new("DE34588900X")).should eql(false)
|
273
|
+
end
|
269
274
|
end
|
270
275
|
end
|
data/spec/valvat/utils_spec.rb
CHANGED
@@ -20,8 +20,8 @@ describe Valvat::Utils do
|
|
20
20
|
Valvat::Utils.split(" ").should eql([nil, nil])
|
21
21
|
end
|
22
22
|
|
23
|
-
it "returns
|
24
|
-
Valvat::Utils.split("EL999999999").should eql(["
|
23
|
+
it "returns EL (language iso code) on greek vat" do
|
24
|
+
Valvat::Utils.split("EL999999999").should eql(["EL", "999999999"])
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -42,4 +42,16 @@ describe Valvat::Utils do
|
|
42
42
|
Valvat::Utils.normalize("ESX4588900X").should eql("ESX4588900X")
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
context "#vat_country_to_iso_country" do
|
47
|
+
it "returns iso country code on greek iso language 'EL'" do
|
48
|
+
Valvat::Utils.vat_country_to_iso_country("EL").should eql("GR")
|
49
|
+
end
|
50
|
+
|
51
|
+
Valvat::Utils::EU_COUNTRIES.each do |iso|
|
52
|
+
it "returns unchanged iso country code '#{iso}'" do
|
53
|
+
Valvat::Utils.vat_country_to_iso_country(iso).should eql(iso)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
45
57
|
end
|
data/spec/valvat_spec.rb
CHANGED
@@ -1,5 +1,249 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
require 'spec_helper'
|
4
|
+
|
3
5
|
describe Valvat do
|
6
|
+
context "#new" do
|
7
|
+
it "demands one and only one argument" do
|
8
|
+
lambda{ Valvat.new }.should raise_error(ArgumentError)
|
9
|
+
lambda{ Valvat.new("a", "b") }.should raise_error(ArgumentError)
|
10
|
+
lambda{ Valvat.new("a") }.should_not raise_error
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "Valvat()" do
|
15
|
+
it "initializes new Valvat instance on string" do
|
16
|
+
Valvat("abc").should be_kind_of(Valvat)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns same Valvat instance on Valvat instance" do
|
20
|
+
vat = Valvat.new("abc")
|
21
|
+
Valvat(vat).should be_kind_of(Valvat)
|
22
|
+
Valvat(vat).object_id.should eql(vat.object_id)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "on european vat number" do
|
27
|
+
let(:de_vat) { Valvat.new("DE259597697") } # valid & exists
|
28
|
+
let(:at_vat) { Valvat.new("ATU458890031") } # invalid
|
29
|
+
let(:gr_vat) { Valvat.new("EL999943280") } # valid & exists
|
30
|
+
|
31
|
+
context "#valid?" do
|
32
|
+
it "returns true on valid numbers" do
|
33
|
+
de_vat.should be_valid
|
34
|
+
gr_vat.should be_valid
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns false on invalid numbers" do
|
38
|
+
at_vat.should_not be_valid
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "#exist?" do
|
43
|
+
context "on existing vat numbers" do
|
44
|
+
before do
|
45
|
+
Valvat::Lookup.stub(:validate => true)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns true" do
|
49
|
+
de_vat.should be_exist
|
50
|
+
gr_vat.should be_exist
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "on not existing vat numbers" do
|
55
|
+
before do
|
56
|
+
Valvat::Lookup.stub(:validate => false)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "returns false" do
|
60
|
+
at_vat.should_not be_exist
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "#iso_country_code" do
|
66
|
+
it "returns iso country code on iso_country_code" do
|
67
|
+
de_vat.iso_country_code.should eql("DE")
|
68
|
+
at_vat.iso_country_code.should eql("AT")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns GR iso country code on greek vat number" do
|
72
|
+
gr_vat.iso_country_code.should eql("GR")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "#vat_country_code" do
|
77
|
+
it "returns iso country code on iso_country_code" do
|
78
|
+
de_vat.vat_country_code.should eql("DE")
|
79
|
+
at_vat.vat_country_code.should eql("AT")
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns EL iso language code on greek vat number" do
|
83
|
+
gr_vat.vat_country_code.should eql("EL")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "#european?" do
|
88
|
+
it "returns true" do
|
89
|
+
de_vat.should be_european
|
90
|
+
at_vat.should be_european
|
91
|
+
gr_vat.should be_european
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "#to_s" do
|
96
|
+
it "returns full vat number" do
|
97
|
+
de_vat.to_s.should eql("DE259597697")
|
98
|
+
at_vat.to_s.should eql("ATU458890031")
|
99
|
+
gr_vat.to_s.should eql("EL999943280")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context "#inspect" do
|
104
|
+
it "returns vat number with iso country code" do
|
105
|
+
de_vat.inspect.should eql("#<Valvat DE259597697 DE>")
|
106
|
+
at_vat.inspect.should eql("#<Valvat ATU458890031 AT>")
|
107
|
+
gr_vat.inspect.should eql("#<Valvat EL999943280 GR>")
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "#to_a" do
|
112
|
+
it "calls Valvat::Utils.split with raw vat number and returns result" do
|
113
|
+
de_vat # initialize
|
114
|
+
Valvat::Utils.should_receive(:split).once.with("DE259597697").and_return(["a", "b"])
|
115
|
+
de_vat.to_a.should eql(["a", "b"])
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "on vat number from outside of europe" do
|
121
|
+
let(:us_vat) { Valvat.new("US345889003") }
|
122
|
+
let(:ch_vat) { Valvat.new("CH445889003") }
|
123
|
+
|
124
|
+
context "#valid?" do
|
125
|
+
it "returns false" do
|
126
|
+
us_vat.should_not be_valid
|
127
|
+
ch_vat.should_not be_valid
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "#exist?" do
|
132
|
+
without_any_web_requests!
|
133
|
+
|
134
|
+
it "returns false" do
|
135
|
+
us_vat.should_not be_exist
|
136
|
+
ch_vat.should_not be_exist
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "#iso_country_code" do
|
141
|
+
it "returns nil" do
|
142
|
+
us_vat.iso_country_code.should eql(nil)
|
143
|
+
ch_vat.iso_country_code.should eql(nil)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "#vat_country_code" do
|
148
|
+
it "returns nil" do
|
149
|
+
us_vat.vat_country_code.should eql(nil)
|
150
|
+
ch_vat.vat_country_code.should eql(nil)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
context "#european?" do
|
155
|
+
it "returns false" do
|
156
|
+
us_vat.should_not be_european
|
157
|
+
ch_vat.should_not be_european
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context "#to_s" do
|
162
|
+
it "returns full given vat number" do
|
163
|
+
us_vat.to_s.should eql("US345889003")
|
164
|
+
ch_vat.to_s.should eql("CH445889003")
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context "#inspect" do
|
169
|
+
it "returns vat number without iso country code" do
|
170
|
+
us_vat.inspect.should eql("#<Valvat US345889003>")
|
171
|
+
ch_vat.inspect.should eql("#<Valvat CH445889003>")
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
4
176
|
|
177
|
+
context "on non-sense/empty vat number" do
|
178
|
+
let(:only_iso_vat) { Valvat.new("DE") }
|
179
|
+
let(:num_vat) { Valvat.new("12445889003") }
|
180
|
+
let(:empty_vat) { Valvat.new("") }
|
181
|
+
let(:nil_vat) { Valvat.new("") }
|
182
|
+
|
183
|
+
context "#valid?" do
|
184
|
+
it "returns false" do
|
185
|
+
only_iso_vat.should_not be_valid
|
186
|
+
num_vat.should_not be_valid
|
187
|
+
empty_vat.should_not be_valid
|
188
|
+
nil_vat.should_not be_valid
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
context "#exist?" do
|
193
|
+
without_any_web_requests!
|
194
|
+
|
195
|
+
it "returns false" do
|
196
|
+
only_iso_vat.should_not be_exist
|
197
|
+
num_vat.should_not be_exist
|
198
|
+
empty_vat.should_not be_exist
|
199
|
+
nil_vat.should_not be_exist
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context "#iso_country_code" do
|
204
|
+
it "returns nil" do
|
205
|
+
only_iso_vat.iso_country_code.should eql(nil)
|
206
|
+
num_vat.iso_country_code.should eql(nil)
|
207
|
+
empty_vat.iso_country_code.should eql(nil)
|
208
|
+
nil_vat.iso_country_code.should eql(nil)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "#vat_country_code" do
|
213
|
+
it "returns nil" do
|
214
|
+
only_iso_vat.vat_country_code.should eql(nil)
|
215
|
+
num_vat.vat_country_code.should eql(nil)
|
216
|
+
empty_vat.vat_country_code.should eql(nil)
|
217
|
+
nil_vat.vat_country_code.should eql(nil)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "#european?" do
|
222
|
+
it "returns false" do
|
223
|
+
only_iso_vat.should_not be_european
|
224
|
+
num_vat.should_not be_european
|
225
|
+
empty_vat.should_not be_european
|
226
|
+
nil_vat.should_not be_european
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context "#to_s" do
|
231
|
+
it "returns full given vat number" do
|
232
|
+
only_iso_vat.to_s.should eql("DE")
|
233
|
+
num_vat.to_s.should eql("12445889003")
|
234
|
+
empty_vat.to_s.should eql("")
|
235
|
+
nil_vat.to_s.should eql("")
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
context "#inspect" do
|
240
|
+
it "returns vat number without iso country code" do
|
241
|
+
only_iso_vat.inspect.should eql("#<Valvat DE>")
|
242
|
+
num_vat.inspect.should eql("#<Valvat 12445889003>")
|
243
|
+
empty_vat.inspect.should eql("#<Valvat >")
|
244
|
+
nil_vat.inspect.should eql("#<Valvat >")
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
5
249
|
end
|
data/valvat.gemspec
CHANGED
@@ -9,9 +9,9 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Sebastian Munz"]
|
10
10
|
s.email = ["sebastian@yo.lk"]
|
11
11
|
s.homepage = "https://github.com/yolk/valvat"
|
12
|
-
s.summary = %q{Validates european vat numbers. Standalone or as
|
12
|
+
s.summary = %q{Validates european vat numbers. Standalone or as a ActiveModel validator.}
|
13
13
|
s.description = <<-END
|
14
|
-
Validates european vat numbers. Standalone or as
|
14
|
+
Validates european vat numbers. Standalone or as a ActiveModel validator.
|
15
15
|
|
16
16
|
* Simple syntax verification
|
17
17
|
* Lookup via the VIES web service
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Sebastian Munz
|
@@ -104,7 +104,7 @@ dependencies:
|
|
104
104
|
version: "0"
|
105
105
|
type: :development
|
106
106
|
version_requirements: *id006
|
107
|
-
description: " Validates european vat numbers. Standalone or as
|
107
|
+
description: " Validates european vat numbers. Standalone or as a ActiveModel validator.\n \n * Simple syntax verification\n * Lookup via the VIES web service\n * Works standalone without any gem dependencies\n * (Optional) ActiveModel/Rails3 integration\n * I18n locales for country specific error messages\n"
|
108
108
|
email:
|
109
109
|
- sebastian@yo.lk
|
110
110
|
executables: []
|
@@ -168,7 +168,7 @@ rubyforge_project:
|
|
168
168
|
rubygems_version: 1.3.7
|
169
169
|
signing_key:
|
170
170
|
specification_version: 3
|
171
|
-
summary: Validates european vat numbers. Standalone or as
|
171
|
+
summary: Validates european vat numbers. Standalone or as a ActiveModel validator.
|
172
172
|
test_files:
|
173
173
|
- spec/spec_helper.rb
|
174
174
|
- spec/valvat/active_model_spec.rb
|