nifval 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nifval.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nifval (0.1.0)
5
+ activemodel
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.0.5)
11
+ activesupport (= 3.0.5)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.4)
14
+ activesupport (3.0.5)
15
+ builder (2.1.2)
16
+ diff-lcs (1.1.2)
17
+ i18n (0.5.0)
18
+ rspec (2.5.0)
19
+ rspec-core (~> 2.5.0)
20
+ rspec-expectations (~> 2.5.0)
21
+ rspec-mocks (~> 2.5.0)
22
+ rspec-core (2.5.1)
23
+ rspec-expectations (2.5.0)
24
+ diff-lcs (~> 1.1.2)
25
+ rspec-mocks (2.5.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ nifval!
32
+ rspec (~> 2.5)
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ NifVal
2
+ ======
3
+
4
+ Description
5
+ -----------
6
+
7
+ NifVal is a simple gem which adds a Spanish NIF/NIE/CIF validator to
8
+ ActiveModel. You simply have to validate ":nif" for "true" for a specific field.
9
+
10
+ Usage
11
+ -----
12
+
13
+ Let's see an example:
14
+
15
+ class Person
16
+ validates :nif, :nif => true
17
+ attr_accessor :nif
18
+ end
19
+
20
+ Then if we create an instance and see if it is valid, the validation
21
+ will be checked.
22
+
23
+ Let's see one example for each case.
24
+ A successful validation (correct control digit T for 00000000):
25
+
26
+ p = Person.new("00000000T")
27
+ p.valid? # will return true
28
+
29
+ And an unsuccessful one (mistaken control digit C for A2345678):
30
+
31
+ p = Person.new("A2345678C")
32
+ p.valid? # will return false
33
+
34
+ Installation
35
+ ------------
36
+
37
+ Simply add this gem to your Gemfile...
38
+
39
+ ...
40
+ gem "nifval"
41
+ ...
42
+
43
+ ...and execute "bundle".
44
+
45
+ Note: It appears that if we load <b>NifVal</b> after <b>Devise</b>, <b>Nifval</b> won't work. In order to make everything work, simply add <b>Nifval</b> before <b>Devise</b> in the Gemfile.
46
+
47
+ Documentation
48
+ -------------
49
+
50
+ Adapted from [here](http://compartecodigo.com/javascript/validar-nif-cif-nie-segun-ley-vigente-31.html).
51
+
52
+ You can also find more information [on Wikipedia](http://es.wikipedia.org/wiki/N%C3%BAmero_de_identificaci%C3%B3n_fiscal).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ module NifVal
2
+ VERSION = "0.1.0"
3
+ end
data/lib/nifval.rb ADDED
@@ -0,0 +1,57 @@
1
+ # Adapted from http://compartecodigo.com/javascript/validar-nif-cif-nie-segun-ley-vigente-31.html
2
+
3
+ require "active_model"
4
+
5
+ module NifVal
6
+ class NifValidator < ActiveModel::EachValidator
7
+ def validate_each(record, attribute, value)
8
+ if !is_valid_nif value
9
+ record.errors[attribute] << "ERROR!"
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def is_valid_nif nif
16
+ # NIF not provided
17
+ return false if nif.nil?
18
+
19
+ # Format
20
+ return false if
21
+ !nif.match(/[A-Z]{1}\d{7}[A-Z0-9]{1}/) && !nif.match(/[0-9]{8}[A-Z]{1}/)
22
+
23
+ if nif.match(/[0-9]{8}[A-Z]{1}/)
24
+ # Standard NIF
25
+ nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[nif[0..7].to_i % 23]
26
+ else
27
+ # CIF algorithm
28
+ sum = nif[2]-48 + nif[4]-48 + nif[6]-48
29
+ [1,3,5,7].each do |i|
30
+ t = (2*(nif[i]-48)).to_s
31
+ t1 = t[0]-48
32
+ t2 = t[1].nil? ? 0 : t[1]-48
33
+ sum += t1+t2
34
+ end
35
+ sumstr = sum.to_s
36
+ n = 10 - (sumstr[sumstr.length-1]-48)
37
+
38
+ if nif.match(/^[KLM]{1}/)
39
+ # Special NIFs (as CIFs)
40
+ nif[8] == (64+n).chr
41
+ elsif nif.match(/^[ABCDEFGHJNPQRSUVW]{1}/)
42
+ # CIFs
43
+ nstr = n.to_s
44
+ (nif[8] == (64+n).chr) || (nif[8] == nstr[nstr.length-1])
45
+ elsif nif.match(/^[XYZ]{1}/)
46
+ # NIE
47
+ niff = nif.gsub("X","0").gsub("Y","1").gsub("Z","2")
48
+ nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[niff[0..7].to_i % 23]
49
+ else
50
+ false
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ ActiveModel::Validations.send(:include, NifVal)
data/nifval.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "nifval/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "nifval"
7
+ s.version = NifVal::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Albert Bellonch"]
10
+ s.email = ["albert@itnig.net"]
11
+ s.homepage = "https://github.com/albertbellonch/nifval"
12
+ s.summary = %q{Validates a Spanish NIF/CIF/NIE}
13
+ s.description = %q{Validates a Spanish NIF/CIF/NIE by verifying that the digit control corresponds to the number.}
14
+
15
+ s.rubyforge_project = "nifval"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "rspec", "~> 2.5"
23
+ s.add_dependency "activemodel"
24
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ class Test
4
+ include ActiveModel::Validations
5
+
6
+ validates :testfield, :nif => true
7
+
8
+ attr_accessor :testfield
9
+
10
+ def initialize testfield
11
+ @testfield = testfield
12
+ end
13
+ end
14
+
15
+ describe NifVal do
16
+ def nif_validity nif, ok
17
+ test = Test.new(nif)
18
+ test.valid?.should == ok
19
+ end
20
+
21
+ # Correct NIFs
22
+ context "when we check valid NIFs" do
23
+ it "should return OK" do
24
+ nif_validity "00000000T", true
25
+ end
26
+ end
27
+
28
+ # Invalid NIFs
29
+ context "when we check invalid NIFs" do
30
+ it "should return ERROR" do
31
+ nif_validity "A2345678C", false
32
+ end
33
+ end
34
+
35
+ # Correct CIFs
36
+ context "when we check valid CIFs" do
37
+ it "should return OK" do
38
+ nif_validity "A12345674", true
39
+ end
40
+ end
41
+
42
+ # Invalid CIFs
43
+ context "when we check invalid CIFs" do
44
+ it "should return ERROR" do
45
+ nif_validity "12345678T", false
46
+ end
47
+ end
48
+
49
+ # Correct NIEs
50
+ context "when we check valid NIEs" do
51
+ it "should return OK" do
52
+ nif_validity "X1230123Z", true
53
+ end
54
+ end
55
+
56
+ # Incorrect NIEs
57
+ context "when we check invalid NIEs" do
58
+ it "should return ERROR" do
59
+ nif_validity "X1230123F", false
60
+ end
61
+ end
62
+
63
+ # Bad format
64
+ context "when we check for badly-formatted strings" do
65
+ it "should return ERROR" do
66
+ nif_validity nil, false
67
+ end
68
+
69
+ it "should return ERROR" do
70
+ nif_validity "cucamonga", false
71
+ end
72
+
73
+ it "should return ERROR" do
74
+ nif_validity "0000 0000 T", false
75
+ end
76
+
77
+ it "should return ERROR" do
78
+ nif_validity "123A123AA", false
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1 @@
1
+ require 'nifval'
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nifval
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Albert Bellonch
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-28 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 9
30
+ segments:
31
+ - 2
32
+ - 5
33
+ version: "2.5"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activemodel
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: Validates a Spanish NIF/CIF/NIE by verifying that the digit control corresponds to the number.
51
+ email:
52
+ - albert@itnig.net
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .rspec
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - README.md
64
+ - Rakefile
65
+ - lib/nifval.rb
66
+ - lib/nifval/version.rb
67
+ - nifval.gemspec
68
+ - spec/nifval_spec.rb
69
+ - spec/spec_helper.rb
70
+ has_rdoc: true
71
+ homepage: https://github.com/albertbellonch/nifval
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project: nifval
100
+ rubygems_version: 1.5.2
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Validates a Spanish NIF/CIF/NIE
104
+ test_files:
105
+ - spec/nifval_spec.rb
106
+ - spec/spec_helper.rb