valvat 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,9 @@
1
1
  require 'rspec'
2
- require 'active_model'
2
+ begin
3
+ require 'active_model'
4
+ rescue LoadError => err
5
+ puts "Running specs without active_model extension"
6
+ end
3
7
  require 'fakeweb'
4
8
 
5
9
  require File.dirname(__FILE__) + '/../lib/valvat.rb'
@@ -14,18 +18,20 @@ RSpec.configure do |config|
14
18
  config.backtrace_exclusion_patterns = [/rspec\/(core|expectations)/]
15
19
  end
16
20
 
17
- class ModelBase
18
- include ActiveModel::Serialization
19
- include ActiveModel::Validations
21
+ if defined?(ActiveModel)
22
+ class ModelBase
23
+ include ActiveModel::Serialization
24
+ include ActiveModel::Validations
20
25
 
21
- attr_accessor :attributes
26
+ attr_accessor :attributes
22
27
 
23
- def initialize(attributes = {})
24
- @attributes = attributes
25
- end
28
+ def initialize(attributes = {})
29
+ @attributes = attributes
30
+ end
26
31
 
27
- def read_attribute_for_validation(key)
28
- @attributes[key]
32
+ def read_attribute_for_validation(key)
33
+ @attributes[key]
34
+ end
29
35
  end
30
36
  end
31
37
 
@@ -5,17 +5,17 @@ describe Valvat::Lookup do
5
5
  context "existing vat number" do
6
6
 
7
7
  it "returns true" do
8
- Valvat::Lookup.validate("BE0817331995").should eql(true)
8
+ Valvat::Lookup.validate("DE259597697").should eql(true)
9
9
  end
10
10
 
11
11
  it "allows Valvat instance as input" do
12
- Valvat::Lookup.validate(Valvat.new("BE0817331995")).should eql(true)
12
+ Valvat::Lookup.validate(Valvat.new("DE259597697")).should eql(true)
13
13
  end
14
14
  end
15
15
 
16
16
  context "not existing vat number" do
17
17
  it "returns false" do
18
- Valvat::Lookup.validate("BE08173319921").should eql(false)
18
+ Valvat::Lookup.validate("DE259597696").should eql(false)
19
19
  end
20
20
  end
21
21
 
@@ -30,41 +30,52 @@ describe Valvat::Lookup do
30
30
 
31
31
  context "with details" do
32
32
  it "returns hash of details instead of true" do
33
- Valvat::Lookup.validate("IE6388047V", :detail => true).should eql({
34
- :country_code=>"IE",
35
- :vat_number=>"6388047V",
36
- :request_date=> Date.today,
37
- :name=>"GOOGLE IRELAND LIMITED",
38
- :address=>"1ST & 2ND FLOOR ,GORDON HOUSE ,BARROW STREET ,DUBLIN 4"
39
- })
33
+ result = Valvat::Lookup.validate("IE6388047V", :detail => true)
40
34
 
41
- Valvat::Lookup.validate("LU21416127", :detail => true).should eql({
42
- :country_code=>"LU",
43
- :vat_number=>"21416127",
44
- :request_date=> Date.today,
45
- :name=>"EBAY EUROPE S.A R.L.",
46
- :address=>"22, BOULEVARD ROYAL\nL-2449 LUXEMBOURG"
47
- })
35
+ if result
36
+ result.delete(:request_date).should be_kind_of(Date)
37
+ result.should eql({
38
+ :country_code=>"IE",
39
+ :vat_number=>"6388047V",
40
+ :name=>"GOOGLE IRELAND LIMITED",
41
+ :address=>"1ST & 2ND FLOOR ,GORDON HOUSE ,BARROW STREET ,DUBLIN 4"
42
+ })
43
+ else
44
+ puts "Skipping IE vies lookup spec; result = #{result.inspect}"
45
+ end
46
+
47
+ result = Valvat::Lookup.validate("LU21416127", :detail => true)
48
+
49
+ if result
50
+ result.delete(:request_date).should be_kind_of(Date)
51
+ result.should eql({
52
+ :country_code=>"LU",
53
+ :vat_number=>"21416127",
54
+ :name=>"EBAY EUROPE S.A R.L.",
55
+ :address=>"22, BOULEVARD ROYAL\nL-2449 LUXEMBOURG"
56
+ })
57
+ else
58
+ puts "Skipping LU vies lookup spec; result = #{result.inspect}"
59
+ end
48
60
  end
49
61
 
50
62
  it "still returns false on not existing vat number" do
51
- Valvat::Lookup.validate("BE08173319921", :detail => true).should eql(false)
63
+ Valvat::Lookup.validate("DE259597696", :detail => true).should eql(false)
52
64
  end
53
65
  end
54
66
 
55
67
  context "with request identifier" do
56
68
  it "returns hash of details instead of true" do
57
- response = Valvat::Lookup.validate("LU21416127", :requester_vat => "IE6388047V")
69
+ response = Valvat::Lookup.validate("DE259597697", :requester_vat => "IE6388047V")
58
70
  response[:request_identifier].size.should eql(16)
59
- response[:request_identifier] = "some_uniq_string"
71
+ request_identifier = response[:request_identifier]
72
+ response.delete(:request_date).should be_kind_of(Date)
60
73
  response.should eql({
61
- :country_code=>"LU",
62
- :vat_number=>"21416127",
63
- :request_date=> Date.today,
64
- :name => "EBAY EUROPE S.A R.L.",
65
- :address => "22, BOULEVARD ROYAL\nL-2449 LUXEMBOURG",
66
- :company_type => nil,
67
- :request_identifier => "some_uniq_string"
74
+ :country_code=>"DE",
75
+ :vat_number=>"259597697",
76
+ :name => nil,
77
+ :company_type=>nil,
78
+ :request_identifier=> request_identifier
68
79
  })
69
80
  end
70
81
  end
@@ -119,10 +119,13 @@ describe Valvat::Syntax do
119
119
  it "validates a IE vat number" do
120
120
  subject.validate("IE1B12345J").should eql(true)
121
121
  subject.validate("IE1234567B").should eql(true)
122
+ subject.validate("IE1234567XX").should eql(true)
122
123
 
123
124
  subject.validate("IE1B123459").should eql(false)
124
125
  subject.validate("IE19123450").should eql(false)
125
126
  subject.validate("IEA9123450").should eql(false)
127
+ subject.validate("IE1B12345XX").should eql(false)
128
+ subject.validate("IE1X34567XX").should eql(false)
126
129
  end
127
130
 
128
131
  it "validates a IT vat number" do
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
29
29
  s.require_paths = ["lib"]
30
30
 
31
- s.add_dependency 'savon', '>=2.2.0'
31
+ s.add_dependency 'savon', '>=2.3.0'
32
32
 
33
33
  s.add_development_dependency 'rspec', '>= 2.4.0'
34
34
  s.add_development_dependency 'guard-rspec', '>=0.1.9'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valvat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Munz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-18 00:00:00.000000000 Z
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: savon
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 2.2.0
19
+ version: 2.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 2.2.0
26
+ version: 2.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -125,14 +125,18 @@ files:
125
125
  - .gitignore
126
126
  - .rbenv-gemsets
127
127
  - .rbenv-version
128
+ - .travis.yml
128
129
  - CHANGES.md
129
130
  - Gemfile
130
131
  - Guardfile
131
132
  - MIT-LICENSE
132
133
  - README.md
133
134
  - Rakefile
135
+ - gemfiles/activemodel-3-2
136
+ - gemfiles/activemodel-4
137
+ - gemfiles/standalone
138
+ - lib/active_model/validations/valvat_validator.rb
134
139
  - lib/valvat.rb
135
- - lib/valvat/active_model.rb
136
140
  - lib/valvat/checksum.rb
137
141
  - lib/valvat/checksum/at.rb
138
142
  - lib/valvat/checksum/be.rb
@@ -165,8 +169,8 @@ files:
165
169
  - lib/valvat/syntax.rb
166
170
  - lib/valvat/utils.rb
167
171
  - lib/valvat/version.rb
172
+ - spec/active_model/validations/valvat_validator_spec.rb
168
173
  - spec/spec_helper.rb
169
- - spec/valvat/active_model_spec.rb
170
174
  - spec/valvat/checksum/at_spec.rb
171
175
  - spec/valvat/checksum/be_spec.rb
172
176
  - spec/valvat/checksum/bg_spec.rb
@@ -214,8 +218,8 @@ signing_key:
214
218
  specification_version: 4
215
219
  summary: Validates european vat numbers. Standalone or as a ActiveModel validator.
216
220
  test_files:
221
+ - spec/active_model/validations/valvat_validator_spec.rb
217
222
  - spec/spec_helper.rb
218
- - spec/valvat/active_model_spec.rb
219
223
  - spec/valvat/checksum/at_spec.rb
220
224
  - spec/valvat/checksum/be_spec.rb
221
225
  - spec/valvat/checksum/bg_spec.rb
@@ -1,45 +0,0 @@
1
- require 'active_model'
2
- require 'valvat/syntax'
3
- require 'valvat/lookup'
4
-
5
- module ActiveModel
6
- module Validations
7
- class ValvatValidator < ::ActiveModel::EachValidator
8
-
9
- def validate_each(record, attribute, value)
10
- vat = Valvat(value)
11
- iso_country_code = vat.iso_country_code
12
- is_valid = true
13
-
14
- if options[:match_country]
15
- iso_country_code = (record.send(options[:match_country]) || "").upcase
16
- is_valid = iso_country_code == vat.iso_country_code
17
- end
18
-
19
- if is_valid
20
- is_valid = vat.valid?
21
-
22
- is_valid = vat.valid_checksum? if is_valid && options[:checksum]
23
- is_valid = vat.exists? if is_valid && options[:lookup]
24
-
25
- if is_valid.nil?
26
- is_valid = options[:lookup] != :fail_if_down
27
- end
28
- end
29
-
30
- unless is_valid
31
- iso_country_code = "eu" if iso_country_code.blank?
32
- record.errors.add(attribute, :invalid_vat,
33
- :message => options[:message],
34
- :country_adjective => I18n.t(
35
- :"valvat.country_adjectives.#{iso_country_code.downcase}",
36
- :default => [:"valvat.country_adjectives.eu", "european"]
37
- )
38
- )
39
- end
40
- end
41
- end
42
- end
43
- end
44
-
45
- I18n.load_path += Dir["#{File.dirname(__FILE__)}/locales/*.yml"]
@@ -1,240 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class Invoice < ModelBase
4
- validates :vat_number, :valvat => true
5
- end
6
-
7
- class InvoiceWithLookup < ModelBase
8
- validates :vat_number, :valvat => {:lookup => true}
9
- end
10
-
11
- class InvoiceWithLookupAndFailIfDown < ModelBase
12
- validates :vat_number, :valvat => {:lookup => :fail_if_down}
13
- end
14
-
15
- class InvoiceAllowBlank < ModelBase
16
- validates :vat_number, :valvat => {:allow_blank => true}
17
- end
18
-
19
- class InvoiceAllowBlankOnAll < ModelBase
20
- validates :vat_number, :valvat => true, :allow_blank => true
21
- end
22
-
23
- class InvoiceCheckCountry < ModelBase
24
- validates :vat_number, :valvat => {:match_country => :country}
25
-
26
- def country
27
- @attributes[:country]
28
- end
29
- end
30
-
31
- class InvoiceCheckCountryWithLookup < ModelBase
32
- validates :vat_number, :valvat => {:match_country => :country, :lookup => true}
33
-
34
- def country
35
- @attributes[:country]
36
- end
37
- end
38
-
39
- class InvoiceWithChecksum < ModelBase
40
- validates :vat_number, :valvat => {:checksum => true}
41
- end
42
-
43
- describe Invoice do
44
- context "with valid vat number" do
45
- it "should be valid" do
46
- Invoice.new(:vat_number => "DE259597697").should be_valid
47
- end
48
- end
49
-
50
- context "with invalid vat number" do
51
- let(:invoice) { Invoice.new(:vat_number => "DE259597697123") }
52
-
53
- it "should not be valid" do
54
- invoice.should_not be_valid
55
- end
56
-
57
- it "should add default (country specific) error message" do
58
- invoice.valid?
59
- invoice.errors[:vat_number].should eql(["is not a valid German vat number"])
60
- end
61
-
62
- context "with i18n translation in place" do
63
- before do
64
- I18n.backend.store_translations(:en, :activemodel => {
65
- :errors => {:models => {:invoice => {:invalid_vat => "is ugly."}}}
66
- })
67
- end
68
-
69
- after { I18n.reload! }
70
-
71
- it "should use translation" do
72
- invoice.valid?
73
- invoice.errors[:vat_number].should eql(["is ugly."])
74
- end
75
- end
76
-
77
- context "with i18n translation with country adjective placeholder in place" do
78
- before do
79
- I18n.backend.store_translations(:en, :activemodel => {
80
- :errors => {:models => {:invoice => {:invalid_vat => "is not a %{country_adjective} vat"}}}
81
- })
82
- end
83
-
84
- after { I18n.reload! }
85
-
86
- it "should replace country adjective placeholder" do
87
- invoice = Invoice.new(:vat_number => "IE123")
88
- invoice.valid?
89
- invoice.errors[:vat_number].should eql(["is not a Irish vat"])
90
- end
91
-
92
- it "should fall back to 'European' if country is missing" do
93
- invoice = Invoice.new(:vat_number => "XX123")
94
- invoice.valid?
95
- invoice.errors[:vat_number].should eql(["is not a European vat"])
96
- end
97
- end
98
- end
99
-
100
- context "with blank vat number" do
101
- it "should not be valid" do
102
- Invoice.new(:vat_number => "").should_not be_valid
103
- Invoice.new(:vat_number => nil).should_not be_valid
104
- end
105
- end
106
- end
107
-
108
- describe InvoiceWithLookup do
109
- context "with valid but not existing vat number" do
110
- before do
111
- Valvat::Syntax.stub(:validate => true)
112
- Valvat::Lookup.stub(:validate => false)
113
- end
114
-
115
- it "should not be valid" do
116
- InvoiceWithLookup.new(:vat_number => "DE123").should_not be_valid
117
- end
118
- end
119
-
120
- context "with valid and existing vat number" do
121
- before do
122
- Valvat::Syntax.stub(:validate => true)
123
- Valvat::Lookup.stub(:validate => true)
124
- end
125
-
126
- it "should be valid" do
127
- InvoiceWithLookup.new(:vat_number => "DE123").should be_valid
128
- end
129
- end
130
-
131
- context "with valid vat number and VIES country service down" do
132
- before do
133
- Valvat::Syntax.stub(:validate => true)
134
- Valvat::Lookup.stub(:validate => nil)
135
- end
136
-
137
- it "should be valid" do
138
- InvoiceWithLookup.new(:vat_number => "DE123").should be_valid
139
- end
140
- end
141
- end
142
-
143
- describe InvoiceWithLookupAndFailIfDown do
144
- context "with valid vat number and VIES country service down" do
145
- before do
146
- Valvat::Syntax.stub(:validate => true)
147
- Valvat::Lookup.stub(:validate => nil)
148
- end
149
-
150
- it "should not be valid" do
151
- InvoiceWithLookupAndFailIfDown.new(:vat_number => "DE123").should_not be_valid
152
- end
153
- end
154
- end
155
-
156
- describe InvoiceAllowBlank do
157
- context "with blank vat number" do
158
- it "should be valid" do
159
- InvoiceAllowBlank.new(:vat_number => "").should be_valid
160
- InvoiceAllowBlank.new(:vat_number => nil).should be_valid
161
- end
162
- end
163
- end
164
-
165
- describe InvoiceAllowBlankOnAll do
166
- context "with blank vat number" do
167
- it "should be valid" do
168
- InvoiceAllowBlankOnAll.new(:vat_number => "").should be_valid
169
- InvoiceAllowBlankOnAll.new(:vat_number => nil).should be_valid
170
- end
171
- end
172
- end
173
-
174
- describe InvoiceCheckCountry do
175
- it "should be not valid on blank country" do
176
- InvoiceCheckCountry.new(:country => nil, :vat_number => "DE259597697").should_not be_valid
177
- InvoiceCheckCountry.new(:country => "", :vat_number => "DE259597697").should_not be_valid
178
- end
179
-
180
- it "should be not valid on wired country" do
181
- InvoiceCheckCountry.new(:country => "XAXXX", :vat_number => "DE259597697").should_not be_valid
182
- InvoiceCheckCountry.new(:country => "ZO", :vat_number => "DE259597697").should_not be_valid
183
- end
184
-
185
- it "should be not valid on mismatching (eu) country" do
186
- InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697").should_not be_valid
187
- InvoiceCheckCountry.new(:country => "AT", :vat_number => "DE259597697").should_not be_valid
188
- InvoiceCheckCountry.new(:country => "DE", :vat_number => "ATU65931334").should_not be_valid
189
- end
190
-
191
- it "should be valid on matching country" do
192
- InvoiceCheckCountry.new(:country => "DE", :vat_number => "DE259597697").should be_valid
193
- InvoiceCheckCountry.new(:country => "AT", :vat_number => "ATU65931334").should be_valid
194
- end
195
-
196
- it "should give back error message with country from :country_match" do
197
- invoice = InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697")
198
- invoice.valid?
199
- invoice.errors[:vat_number].should eql(["is not a valid French vat number"])
200
- end
201
-
202
- it "should give back error message with country from :country_match even on invalid vat number" do
203
- invoice = InvoiceCheckCountry.new(:country => "FR", :vat_number => "DE259597697123")
204
- invoice.valid?
205
- invoice.errors[:vat_number].should eql(["is not a valid French vat number"])
206
- end
207
- end
208
-
209
- describe InvoiceCheckCountryWithLookup do
210
- before do
211
- Valvat::Syntax.stub(:validate => true)
212
- Valvat::Lookup.stub(:validate => true)
213
- end
214
-
215
- it "avoids lookup or syntax check on failed because of mismatching country" do
216
- Valvat::Syntax.should_not_receive(:validate)
217
- Valvat::Lookup.should_not_receive(:validate)
218
- InvoiceCheckCountryWithLookup.new(:country => "FR", :vat_number => "DE259597697").valid?
219
- end
220
-
221
- it "check syntax and looup on matching country" do
222
- Valvat::Syntax.should_receive(:validate).and_return(true)
223
- Valvat::Lookup.should_receive(:validate).and_return(true)
224
- InvoiceCheckCountryWithLookup.new(:country => "DE", :vat_number => "DE259597697").valid?
225
- end
226
- end
227
-
228
- describe InvoiceWithChecksum do
229
- context "with valid vat number" do
230
- it "should be valid" do
231
- InvoiceWithChecksum.new(:vat_number => "DE259597697").should be_valid
232
- end
233
- end
234
-
235
- context "with invalid vat number" do
236
- it "should not be valid" do
237
- InvoiceWithChecksum.new(:vat_number => "DE259597687").should_not be_valid
238
- end
239
- end
240
- end