uy-id 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uy-id.gemspec
4
+ gemspec
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ uy-id (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.4)
10
+ rake (10.1.0)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.5)
16
+ rspec-expectations (2.14.2)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.3)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.3)
25
+ rake
26
+ rspec (~> 2.14.1)
27
+ uy-id!
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alfonso Cora
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,44 @@
1
+ # Uy::Id
2
+
3
+ This gem includes the necessary helper methods to validate Uruguayan identification numbers ("Cédula de identidad").
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'uy-id'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install uy-id
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'uy/id'
23
+
24
+ # to validate an ID:
25
+ Uy::Id.new('94475265').valid?
26
+ # => true
27
+
28
+ # to generate valid ID for testing purposes:
29
+ Uy::Id.new.id
30
+ # => "63228586"
31
+
32
+ # note that ID numbers lower than 1000000 must contain the trailing zeros
33
+ # this limitation might change in the future with the introduction of sanitizing features
34
+ Uy::Id.new('1234561').valid? # => false
35
+ Uy::Id.new('01234561').valid? # => true
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ If you want to contribute to this project, just fork it, make your changes, run the tests and create a pull request. Also, feel free to report issues on the [issues section](issues).
41
+
42
+ ## Credits
43
+
44
+ Thanks to @cschmeichel for facilitating the algorithms!
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new do |task|
7
+ task.rspec_opts = ["-c", "-f progress"]
8
+ task.pattern = 'spec/**/*_spec.rb'
9
+ end
@@ -0,0 +1,43 @@
1
+ module Uy
2
+ class Id
3
+ FACTORS = [2,9,8,7,6,3,4]
4
+
5
+ attr_accessor :id
6
+
7
+ def initialize(id=nil)
8
+ @id = id || Id.generate
9
+ end
10
+
11
+ def is_valid?
12
+ return false unless @id =~ /\d{8}/
13
+ return false unless Id.calculate_check_digit(@id) == @id[7].to_i
14
+
15
+ true
16
+ end
17
+
18
+ alias valid? is_valid?
19
+
20
+ protected
21
+
22
+ def self.calculate_check_digit(id)
23
+ id = id.length == 8 ? id.chop : id
24
+ checksum =
25
+ id
26
+ .chars
27
+ .to_a
28
+ .map(&:to_i)
29
+ .zip(FACTORS)
30
+ .map {|d| d.reduce(&:*)}
31
+ .reduce(:+)
32
+ check_digit = (10 - (checksum % 10)) % 10
33
+ check_digit
34
+ end
35
+
36
+ def self.generate
37
+ id = ''
38
+ 7.times { id += rand(0..9).to_s }
39
+ id += calculate_check_digit(id).to_s
40
+ id
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,18 @@
1
+ require 'uy/id'
2
+
3
+ describe Uy::Id do
4
+ describe 'validations' do
5
+ it { expect(Uy::Id.new('')).not_to be_valid }
6
+ it { expect(Uy::Id.new('asdf')).not_to be_valid }
7
+ it { expect(Uy::Id.new('1122222')).not_to be_valid }
8
+ it { expect(Uy::Id.new('03555770')).not_to be_valid }
9
+ it { expect(Uy::Id.new('21922316')).to be_valid }
10
+ it { expect(Uy::Id.new('10253720')).to be_valid }
11
+ end
12
+
13
+ describe 'generation' do
14
+ it 'generates valid numbers' do
15
+ 25.times { expect(Uy::Id.new).to be_valid }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "uy-id"
7
+ spec.version = '0.0.1'
8
+ spec.authors = ["Alfonso Cora"]
9
+ spec.email = ["acora6@gmail.com"]
10
+ spec.description = %q{Includes helper methods to validate Uruguayan identification numbers}
11
+ spec.summary = %q{Uy::Id is a class that allows to validate ID numbers or generate fake ones for testing purposes}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rspec", "~> 2.14.1"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uy-id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alfonso Cora
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.14.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.14.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Includes helper methods to validate Uruguayan identification numbers
63
+ email:
64
+ - acora6@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/uy/id.rb
76
+ - spec/id_spec.rb
77
+ - uy-id.gemspec
78
+ homepage: ''
79
+ licenses:
80
+ - MIT
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.25
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Uy::Id is a class that allows to validate ID numbers or generate fake ones
103
+ for testing purposes
104
+ test_files:
105
+ - spec/id_spec.rb
106
+ has_rdoc: