nif 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +13 -0
- data/README.md +41 -0
- data/features/nif.feature +19 -0
- data/features/step_definitions/nif_steps.rb +23 -0
- data/lib/nif.rb +60 -0
- data/lib/nif/version.rb +3 -0
- data/spec/nif_spec.rb +24 -0
- data/test/nif_test.rb +24 -0
- metadata +77 -0
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright © 2011 Pedro Carrico
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
10
|
+
|
11
|
+
website: http://pedrocarrico.net
|
12
|
+
blog: http://blog.pedrocarrico.net
|
13
|
+
twitter: @pedro_carrico
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Nif - Portuguese NIF generator and validator
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/pedrocarrico/nif.png)](http://travis-ci.org/pedrocarrico/nif)
|
4
|
+
|
5
|
+
### Instalation
|
6
|
+
Just:
|
7
|
+
# gem install nif
|
8
|
+
|
9
|
+
### How does it work?
|
10
|
+
irb> require 'nif'
|
11
|
+
=> true
|
12
|
+
# generate a nif
|
13
|
+
irb> Nif::Generator.generate
|
14
|
+
=> "943935784"
|
15
|
+
# generate an unique nif (it will not be generated anymore during this session)
|
16
|
+
irb> Nif::Generator.generate_unique
|
17
|
+
=> "812627318"
|
18
|
+
# validate a nif
|
19
|
+
irb> Nif::Validator.validate '812627318'
|
20
|
+
=> true
|
21
|
+
|
22
|
+
### Description
|
23
|
+
|
24
|
+
This is a simple project used to learn and teach the basics of ruby syntax, unit testing and code coverage.
|
25
|
+
It covers the generation and validation of the Portuguese Fiscal Code (or NIF).
|
26
|
+
If you want to contribute be sure to make a test covering your changes.
|
27
|
+
|
28
|
+
Have fun!
|
29
|
+
|
30
|
+
### Testing
|
31
|
+
# test unit
|
32
|
+
rake test
|
33
|
+
# rspec
|
34
|
+
rake spec
|
35
|
+
# cucumber features
|
36
|
+
rake cucumber
|
37
|
+
# check coverage
|
38
|
+
rake rcov # open coverage/index.html for the results
|
39
|
+
|
40
|
+
### TODO
|
41
|
+
Make a CustomValidator for rails.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Feature: NifGenerator
|
2
|
+
In order to start validating nifs
|
3
|
+
As a user of the NifGenerator
|
4
|
+
I want a NifGenerator to start validating
|
5
|
+
|
6
|
+
Scenario: NifGenerator validates a valid nif
|
7
|
+
Given a NifGenerator
|
8
|
+
When I validate a nif like "502874210"
|
9
|
+
Then I should see "true"
|
10
|
+
|
11
|
+
Scenario: NifGenerator validates an invalid nif
|
12
|
+
Given a NifGenerator
|
13
|
+
When I validate a nif like "502874211"
|
14
|
+
Then I should see "false"
|
15
|
+
|
16
|
+
Scenario: NifGenerator generates a valid nif
|
17
|
+
Given a NifGenerator
|
18
|
+
When I generate a valid nif
|
19
|
+
Then I should see "true"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'lib/nif'
|
2
|
+
|
3
|
+
Given /^a NifGenerator$/ do
|
4
|
+
#TODO: get a valid instance?
|
5
|
+
end
|
6
|
+
|
7
|
+
When /^I validate a valid nif$/ do
|
8
|
+
valid_nif = '502874210'
|
9
|
+
result = Nif::Validator.validate(valid_nif)
|
10
|
+
end
|
11
|
+
|
12
|
+
When /^I validate a nif like "([^"]*)"$/ do |nif|
|
13
|
+
result = Nif::Validator.validate(nif)
|
14
|
+
end
|
15
|
+
|
16
|
+
When /^I generate a valid nif$/ do
|
17
|
+
nif = Nif::Generator.generate
|
18
|
+
result = Nif::Validator.validate(nif)
|
19
|
+
end
|
20
|
+
|
21
|
+
Then /^I should see "([^"]*)"$/ do |result|
|
22
|
+
result.should == "#{result}"
|
23
|
+
end
|
data/lib/nif.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Nif
|
2
|
+
class Generator
|
3
|
+
def self.generate
|
4
|
+
generated_nif = (1 + rand(9)).to_s
|
5
|
+
|
6
|
+
(1..7).to_a.each { generated_nif += (rand(10)).to_s }
|
7
|
+
|
8
|
+
generated_nif += NifGenerator.calculate_check_digit(generated_nif).to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.generate_unique
|
12
|
+
if NifGenerator.instance.nifs_generated.nil?
|
13
|
+
NifGenerator.instance.nifs_generated = []
|
14
|
+
end
|
15
|
+
|
16
|
+
generated_nif = generate
|
17
|
+
|
18
|
+
while NifGenerator.instance.nifs_generated.include? generated_nif
|
19
|
+
generated_nif = generate
|
20
|
+
end
|
21
|
+
|
22
|
+
NifGenerator.instance.nifs_generated << generated_nif
|
23
|
+
|
24
|
+
generated_nif
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Validator
|
29
|
+
def self.validate value
|
30
|
+
return false unless NifGenerator.is_integer? value
|
31
|
+
return false if value.length != 9
|
32
|
+
NifGenerator.calculate_check_digit(value) == value[8,1].to_i
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class NifGenerator
|
37
|
+
@@instance = new
|
38
|
+
private_class_method :new
|
39
|
+
|
40
|
+
attr_accessor :nifs_generated
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def self.instance
|
45
|
+
@@instance
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.is_integer? value
|
49
|
+
value =~ /\A(?:0|[1-9]\d*)\z/
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.calculate_check_digit value
|
53
|
+
control_sum = 9*value[0,1].to_i + 8*value[1,1].to_i + 7*value[2,1].to_i + 6*value[3,1].to_i + 5*value[4,1].to_i + 4*value[5,1].to_i + 3*value[6,1].to_i + 2*value[7,1].to_i
|
54
|
+
control_mod = control_sum % 11
|
55
|
+
check_number = 11 - control_mod
|
56
|
+
check_number > 9 ? 0 : check_number
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/lib/nif/version.rb
ADDED
data/spec/nif_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'lib/nif'
|
2
|
+
|
3
|
+
describe "NifGenerator" do
|
4
|
+
it "should validate a valid nif" do
|
5
|
+
valid_nif = '502874210'
|
6
|
+
result = Nif::Validator.validate(valid_nif)
|
7
|
+
|
8
|
+
result.should == true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should not validate an invalid nif" do
|
12
|
+
invalid_nif = '502874211'
|
13
|
+
result = Nif::Validator.validate(invalid_nif)
|
14
|
+
|
15
|
+
result.should == false
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should generate a valid nif" do
|
19
|
+
nif = Nif::Generator.generate
|
20
|
+
result = Nif::Validator.validate(nif)
|
21
|
+
|
22
|
+
result.should == true
|
23
|
+
end
|
24
|
+
end
|
data/test/nif_test.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'lib/nif'
|
3
|
+
|
4
|
+
class NifTest < Test::Unit::TestCase
|
5
|
+
def test_should_validate_correct_nif
|
6
|
+
valid_nif = '502874210'
|
7
|
+
assert_equal true, Nif::Validator.validate(valid_nif), "Nif #{valid_nif} should be valid"
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_should_not_validate_incorrect_nif
|
11
|
+
invalid_nif = '2024230'
|
12
|
+
assert_equal false, Nif::Validator.validate(invalid_nif), "Nif #{invalid_nif} should not be valid"
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_generate_valid_nif
|
16
|
+
generated_nif = Nif::Generator.generate
|
17
|
+
assert_equal true, Nif::Validator.validate(generated_nif), "Nif #{generated_nif} should be valid"
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_generate_valid_unique_nif
|
21
|
+
generated_nif = Nif::Generator.generate_unique
|
22
|
+
assert_equal true, Nif::Validator.validate(generated_nif), "Nif #{generated_nif} should be valid"
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nif
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Pedro Carri\xC3\xA7o"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-04-22 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: This is a simple project that covers the generation and validation of the Portuguese Fiscal Code (or NIF).
|
23
|
+
email: pedro.carrico@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/nif.rb
|
32
|
+
- lib/nif/version.rb
|
33
|
+
- README.md
|
34
|
+
- LICENSE
|
35
|
+
- features/nif.feature
|
36
|
+
- features/step_definitions/nif_steps.rb
|
37
|
+
- spec/nif_spec.rb
|
38
|
+
- test/nif_test.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/pedrocarrico/nif
|
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:
|
69
|
+
rubygems_version: 1.6.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Nif - Portuguese NIF generator and validator
|
73
|
+
test_files:
|
74
|
+
- features/nif.feature
|
75
|
+
- features/step_definitions/nif_steps.rb
|
76
|
+
- spec/nif_spec.rb
|
77
|
+
- test/nif_test.rb
|