validate_spanish_vat 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +1 -0
- data/lib/validate_spanish_vat.rb +53 -0
- data/lib/validate_spanish_vat/version.rb +3 -0
- data/validate_spanish_vat.gemspec +20 -0
- metadata +74 -0
data/Gemfile
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "validate_spanish_vat/version"
|
2
|
+
|
3
|
+
module ValidateSpanishVat
|
4
|
+
|
5
|
+
def self.validate(value)
|
6
|
+
case
|
7
|
+
when value.match(/[0-9]{8}[a-z]/i)
|
8
|
+
return validate_nif(value)
|
9
|
+
when value.match(/[a-wyz][0-9]{7}[0-9a-z]/i)
|
10
|
+
return validate_cif(value)
|
11
|
+
when value.match(/[x][0-9]{7,8}[a-z]/i)
|
12
|
+
return validate_nie(value)
|
13
|
+
end
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.validate_nif(value)
|
18
|
+
letters = "TRWAGMYFPDXBNJZSQVHLCKE"
|
19
|
+
check = value.slice!(value.length - 1..value.length - 1).upcase
|
20
|
+
calculated_letter = letters[value.to_i % 23].chr
|
21
|
+
|
22
|
+
return check === calculated_letter
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.validate_cif(value)
|
26
|
+
letter = value.slice!(0).chr.upcase
|
27
|
+
check = value.slice!(value.length - 1).chr.upcase
|
28
|
+
|
29
|
+
n1 = n2 = 0
|
30
|
+
for idx in 0..value.length - 1
|
31
|
+
number = value.slice!(0).chr.to_i
|
32
|
+
if (idx % 2) != 0
|
33
|
+
n1 += number
|
34
|
+
else
|
35
|
+
n2 += ((2*number) % 10) + ((2 * number) / 10)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
calculated_number = (10 - ((n1 + n2) % 10)) % 10
|
39
|
+
calculated_letter = (64 + calculated_number).chr
|
40
|
+
|
41
|
+
if letter.match(/[QPNS]/)
|
42
|
+
return check.to_s == calculated_letter.to_s
|
43
|
+
else
|
44
|
+
return check.to_i == calculated_number.to_i
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.validate_nie(value)
|
49
|
+
value[0] = '0'
|
50
|
+
value.slice(0) if value.size > 9
|
51
|
+
validate_nif(value)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "validate_spanish_vat/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "validate_spanish_vat"
|
7
|
+
s.version = ValidateSpanishVat::VERSION
|
8
|
+
s.authors = ["Antonio Pardo"]
|
9
|
+
s.email = ["apardo@alabs.es"]
|
10
|
+
s.homepage = "https://github.com/alabs/validate_spanish_vat"
|
11
|
+
s.summary = %q{Valida NIF, CIf y NIE españoles}
|
12
|
+
s.description = %q{Valida NIF, CIF y NIE españoles}
|
13
|
+
|
14
|
+
s.rubyforge_project = "validate_spanish_vat"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate_spanish_vat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Antonio Pardo
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-20 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: "Valida NIF, CIF y NIE espa\xC3\xB1oles"
|
23
|
+
email:
|
24
|
+
- apardo@alabs.es
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README
|
35
|
+
- Rakefile
|
36
|
+
- lib/validate_spanish_vat.rb
|
37
|
+
- lib/validate_spanish_vat/version.rb
|
38
|
+
- validate_spanish_vat.gemspec
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: https://github.com/alabs/validate_spanish_vat
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: validate_spanish_vat
|
69
|
+
rubygems_version: 1.6.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: "Valida NIF, CIf y NIE espa\xC3\xB1oles"
|
73
|
+
test_files: []
|
74
|
+
|