spanish_vat_validators 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spanish_vat_validators.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Javier Toledo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # SpanishVatValidators
2
+
3
+ I've adapted the code from the ValidateSpanishVAT plugin from https://github.com/lleirborras/ValidateSpanishVAT to be distributed as a gem for ease of use with Rails 3.
4
+
5
+ Also added I18n support for error messages, which you can change by adding this to your locale yml:
6
+
7
+ es:
8
+ errors:
9
+ messages:
10
+ not_valid_spanish_vat: El número de identificación fiscal no es válido
11
+ not_valid_nif: El NIF no es válido
12
+ not_valid_cif: El CIF no es válido
13
+ not_valid_nie: El NIE no es válido
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'spanish_vat_validators'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install spanish_vat_validators
28
+
29
+ ## Usage
30
+
31
+ Just use any of the following validators.
32
+
33
+ # A person id
34
+ class Person < ActiveRecord::Base
35
+ validates :dni, :valid_nif => true
36
+ end
37
+
38
+ # A company id
39
+ class Company < ActiveRecord::Base
40
+ validates :cif, :valid_cif => true
41
+ end
42
+
43
+ # A foreigner id
44
+ class Alien < ActiveRecord::Base
45
+ validates :nie, :valid_nie => true
46
+ end
47
+
48
+ # Any kind of id is valid
49
+ class SpanishSubject < ActiveRecord::Base
50
+ validates :id, :valid_spanish_vat => true
51
+ end
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'spanish_vat_validators'
2
+
3
+ module ActiveRecord::Base
4
+ include SpanishVatValidators
5
+ end
@@ -0,0 +1,92 @@
1
+ require "spanish_vat_validators/version"
2
+
3
+ module ActiveModel::Validations
4
+
5
+ module SpanishVatValidatorsHelpers
6
+ def message(kind='vat')
7
+ I18n.translate!("errors.messages.not_valid_#{kind}") rescue 'is invalid'
8
+ end
9
+
10
+ # Validates NIF
11
+ def validate_nif(value)
12
+ return false unless value.match(/[0-9]{8}[a-z]/i)
13
+ letters = "TRWAGMYFPDXBNJZSQVHLCKE"
14
+ check = value.slice!(value.length - 1..value.length - 1).upcase
15
+ calculated_letter = letters[value.to_i % 23].chr
16
+ return check === calculated_letter
17
+ end
18
+
19
+ # Validates CIF
20
+ def validate_cif(value)
21
+ return false unless value.match(/[a-wyz][0-9]{7}[0-9a-z]/i)
22
+ pares = 0
23
+ impares = 0
24
+ uletra = ["J", "A", "B", "C", "D", "E", "F", "G", "H", "I"]
25
+ texto = value.upcase
26
+ regular = /^[ABCDEFGHKLMNPQRS]\d{7}[0-9,A-J]$/#g);
27
+ if regular.match(value).blank?
28
+ false
29
+ else
30
+ ultima = texto[8,1]
31
+
32
+ [1,3,5,7].collect do |cont|
33
+ xxx = (2 * texto[cont,1].to_i).to_s + "0"
34
+ impares += xxx[0,1].to_i + xxx[1,1].to_i
35
+ pares += texto[cont+1,1].to_i
36
+ end
37
+
38
+ xxx = (2 * texto[8,1].to_i).to_s + "0"
39
+ impares += xxx[0,1].to_i + xxx[1,1].to_i
40
+
41
+ suma = (pares + impares).to_s
42
+ unumero = suma.last.to_i
43
+ unumero = (10 - unumero).to_s
44
+ unumero = 0 if(unumero == 10)
45
+
46
+ ((ultima == unumero) || (ultima == uletra[unumero.to_i]))
47
+ end
48
+ end
49
+
50
+ # Validates NIE, in fact is a fake, a NIE is really a NIF with first number changed to capital 'X' letter, so we change the first X to a 0 and then try to
51
+ # pass the nif validator
52
+ def validate_nie(value)
53
+ return false unless value.match(/[x][0-9]{7,8}[a-z]/i)
54
+ value[0] = '0'
55
+ value.slice(0) if value.size > 9
56
+ validate_nif(value)
57
+ end
58
+ end
59
+
60
+ # Validates any Spanish VAT number
61
+ class ValidSpanishVatValidator < ActiveModel::EachValidator
62
+ include SpanishVatValidatorsHelpers
63
+ def validate_each(record, attribute, value)
64
+ record.errors[attribute] = message unless validate_nif(value.clone) or validate_cif(value.clone) or validate_nie(value.clone)
65
+ end
66
+ end
67
+
68
+ # Validates NIF number only
69
+ class ValidNifValidator < ActiveModel::EachValidator
70
+ include SpanishVatValidatorsHelpers
71
+ def validate_each(record, attribute,value)
72
+ record.errors[attribute] = message('nif') unless validate_nif(value.clone)
73
+ end
74
+ end
75
+
76
+ # Validates CIF number only
77
+ class ValidCifValidator < ActiveModel::EachValidator
78
+ include SpanishVatValidatorsHelpers
79
+ def validate_each(record, attribute,value)
80
+ record.errors[attribute] = message('cif') unless validate_cif(value.clone)
81
+ end
82
+ end
83
+
84
+ # Validates NIE number only
85
+ class ValidNieValidator < ActiveModel::EachValidator
86
+ include SpanishVatValidatorsHelpers
87
+ def validate_each(record, attribute,value)
88
+ record.errors[attribute] = message('nie') unless validate_cif(value.clone)
89
+ end
90
+ end
91
+
92
+ end
@@ -0,0 +1,3 @@
1
+ module SpanishVatValidators
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/spanish_vat_validators/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Javier Toledo"]
6
+ gem.email = ["javier@theagilemonkeys.com"]
7
+ gem.description = %q{Provides validators for spanish VAT numbers (NIF, CIF and NIE)}
8
+ gem.summary = %q{Provides Rails3 compatible validators for spanish VAT numbers (NIF, CIF and NIE), with support for I18n}
9
+ gem.homepage = "https://github.com/agilemonkeys/spanish_vat_validators"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "spanish_vat_validators"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SpanishVatValidators::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spanish_vat_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Javier Toledo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Provides validators for spanish VAT numbers (NIF, CIF and NIE)
15
+ email:
16
+ - javier@theagilemonkeys.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - init.rb
27
+ - lib/spanish_vat_validators.rb
28
+ - lib/spanish_vat_validators/version.rb
29
+ - spanish_vat_validators.gemspec
30
+ homepage: https://github.com/agilemonkeys/spanish_vat_validators
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Provides Rails3 compatible validators for spanish VAT numbers (NIF, CIF and
54
+ NIE), with support for I18n
55
+ test_files: []
56
+ has_rdoc: