valvat 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,6 +1,14 @@
1
1
  ### dev
2
2
 
3
- [full changelog](http://github.com/yolk/valvat/compare/v0.1.0...master)
3
+ [full changelog](http://github.com/yolk/valvat/compare/v0.1.1...master)
4
+
5
+ ### 0.1.1 / 2011-01-07
6
+
7
+ [full changelog](http://github.com/yolk/valvat/compare/v0.1.0...v0.1.1)
8
+
9
+ * Fixed issue with country web service down and added spec
10
+ * Stubbed web service specs with fakeweb
11
+ * Added documentation for ActiveModel support
4
12
 
5
13
  ### 0.1.0 / 2011-01-07
6
14
 
data/README.md CHANGED
@@ -1,8 +1,16 @@
1
1
  ## valvat
2
2
 
3
- Validates european vat numbers. Supports simple syntax verification and lookup via the VIES web service.
3
+ Validates european vat numbers. Standalone or as an ActiveModel validator.
4
4
 
5
- valvat is tested and works with ruby 1.8.7 and 1.9.2.
5
+ ### Features
6
+
7
+ * Simple syntax verification
8
+ * Lookup via the VIES web service
9
+ * Works standalone without any gem dependencies
10
+ * (Optional) ActiveModel/Rails3 integration
11
+ * I18n locales for country specific error messages
12
+
13
+ valvat is tested and works with ruby 1.8.7/1.9.2 and ActiveModel 3.0.
6
14
 
7
15
  ### Installation
8
16
 
@@ -24,6 +32,32 @@ Keep in mind that the VIES webservice might be offline at some time for some cou
24
32
 
25
33
  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.
26
34
 
35
+ ### ActiveModel/Rails3 Usage
36
+
37
+ 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
+
39
+ require 'valvat/active_model'
40
+
41
+ after ActiveModel has been loaded.
42
+
43
+ To validate the attribute _vat_number_ add this to your model:
44
+
45
+ class MyModel < ActiveRecord::Base
46
+ validates :vat_number, :valvat => true
47
+ end
48
+
49
+ To additionally perform a lookup via VIES:
50
+
51
+ validates :vat_number, :valvat => {:lookup => true}
52
+
53
+ 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
+
55
+ validates :vat_number, :valvat => {:lookup => :fail_if_down}
56
+
57
+ By default blank vat numbers validate to false, to change this add the _:allow_blank_ option:
58
+
59
+ validates :vat_number, :valvat => {:allow_blank => true}
60
+
27
61
  ### Utilities
28
62
 
29
63
  To split a vat number into the ISO country code and the remaining chars:
data/lib/valvat/lookup.rb CHANGED
@@ -8,11 +8,15 @@ module Valvat
8
8
  parts = Valvat::Utils.split(vat)
9
9
  return false unless parts[0]
10
10
 
11
- YAML.load(Net::HTTP.start("isvat.appspot.com", 80) {|http|
12
- http.get("/#{parts.join("/")}/")
13
- }.body)
14
- rescue
15
- nil
11
+ result = begin
12
+ YAML.load(Net::HTTP.start("isvat.appspot.com", 80) {|http|
13
+ http.get("/#{parts.join("/")}/")
14
+ }.body)
15
+ rescue
16
+ nil
17
+ end
18
+
19
+ result.is_a?(Hash) && result["error_code"] == 1 ? nil : result
16
20
  end
17
21
  end
18
22
  end
@@ -1,3 +1,3 @@
1
1
  module Valvat
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'rspec'
2
2
  require 'active_model'
3
+ require 'fakeweb'
3
4
 
4
5
  require File.dirname(__FILE__) + '/../lib/valvat.rb'
5
6
 
7
+ $fakeweb = true
8
+
6
9
  class ModelBase
7
10
  include ActiveModel::Serialization
8
11
  include ActiveModel::Validations
@@ -2,17 +2,46 @@ require 'spec_helper'
2
2
 
3
3
  describe Valvat::Lookup do
4
4
  context "#validate" do
5
- it "returns true on existing vat number" do
6
- Valvat::Lookup.validate("BE0817331995").should eql(true)
5
+ context "existing vat number" do
6
+ before do
7
+ FakeWeb.register_uri(:get, "http://isvat.appspot.com/BE/0817331995/", :body => "true")
8
+ end if $fakeweb
9
+
10
+ it "returns true" do
11
+ Valvat::Lookup.validate("BE0817331995").should eql(true)
12
+ end
7
13
  end
8
14
 
9
- it "returns false on not existing vat number" do
10
- Valvat::Lookup.validate("BE0817331994").should eql(false)
15
+ context "not existing vat number" do
16
+ before do
17
+ FakeWeb.register_uri(:get, "http://isvat.appspot.com/BE/0817331995/", :body => "false")
18
+ end if $fakeweb
19
+
20
+ it "returns false" do
21
+ Valvat::Lookup.validate("BE0817331995").should eql(false)
22
+ end
11
23
  end
12
24
 
13
- it "returns false on invalid country code / input" do
14
- Valvat::Lookup.validate("AE259597697").should eql(false)
15
- Valvat::Lookup.validate("").should eql(false)
25
+ context "invalid country code / input" do
26
+ before do
27
+ FakeWeb.register_uri(:get, "http://isvat.appspot.com/AE/259597697/", :body => "false")
28
+ end if $fakeweb
29
+
30
+ it "returns false" do
31
+ Valvat::Lookup.validate("AE259597697").should eql(false)
32
+ Valvat::Lookup.validate("").should eql(false)
33
+ end
34
+ end
35
+
36
+ context "country web service down" do
37
+ before do
38
+ json = "{\"error_message\": \"Member State service unavailable.\", \"error_code\": 1, \"error\": true}"
39
+ FakeWeb.register_uri(:get, "http://isvat.appspot.com/DE/259597697/", :body => json)
40
+ end if $fakeweb
41
+
42
+ it "returns nil" do
43
+ Valvat::Lookup.validate("DE259597697").should eql(nil)
44
+ end
16
45
  end
17
46
  end
18
47
  end
data/valvat.gemspec CHANGED
@@ -9,9 +9,19 @@ 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. Supports simple syntax verification and lookup via the VIES web service.}
13
- s.description = %q{Validates european vat numbers. Supports simple syntax verification and lookup via the VIES web service.}
14
-
12
+ s.summary = %q{Validates european vat numbers. Standalone or as an ActiveModel validator.}
13
+ s.description = <<-END
14
+ Validates european vat numbers. Standalone or as an ActiveModel validator.
15
+
16
+ * Simple syntax verification
17
+ * Lookup via the VIES web service
18
+ * Works standalone without any gem dependencies
19
+ * (Optional) ActiveModel/Rails3 integration
20
+ * I18n locales for country specific error messages
21
+ END
22
+
23
+
24
+
15
25
  s.files = `git ls-files`.split("\n")
16
26
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
27
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -22,4 +32,5 @@ Gem::Specification.new do |s|
22
32
  s.add_development_dependency 'growl', '>=1.0.3'
23
33
  s.add_development_dependency 'rb-fsevent', '>=0.3.9'
24
34
  s.add_development_dependency 'activemodel', '>=3.0'
35
+ s.add_development_dependency 'fakeweb'
25
36
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sebastian Munz
@@ -91,7 +91,20 @@ dependencies:
91
91
  version: "3.0"
92
92
  type: :development
93
93
  version_requirements: *id005
94
- description: Validates european vat numbers. Supports simple syntax verification and lookup via the VIES web service.
94
+ - !ruby/object:Gem::Dependency
95
+ name: fakeweb
96
+ prerelease: false
97
+ requirement: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ type: :development
106
+ version_requirements: *id006
107
+ description: " Validates european vat numbers. Standalone or as an 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"
95
108
  email:
96
109
  - sebastian@yo.lk
97
110
  executables: []
@@ -155,7 +168,7 @@ rubyforge_project:
155
168
  rubygems_version: 1.3.7
156
169
  signing_key:
157
170
  specification_version: 3
158
- summary: Validates european vat numbers. Supports simple syntax verification and lookup via the VIES web service.
171
+ summary: Validates european vat numbers. Standalone or as an ActiveModel validator.
159
172
  test_files:
160
173
  - spec/spec_helper.rb
161
174
  - spec/valvat/active_model_spec.rb