nis_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a5c672cafa49e9c6810c5bd47e1eb13896385b34
4
+ data.tar.gz: 5f41fd5c9592259a44ce971d35e91cd5b03362b2
5
+ SHA512:
6
+ metadata.gz: 16a7cc448e4ae6a6d3aea8506707732d4376329a7513f34a3558db8f915f6b692ad063d909a6eca2d54514e98acfd0929b95c848b0451dfd720e175f4cc9a935
7
+ data.tar.gz: c7c8d656f5d27a2e584735256c1e51cb303e6f7fa8cc8f2219c8931df645bc5bca1e61f545b3ffd9d7ab145cfad7e672fc9394a7ee11aa7733a8ee750b4d34aa
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --order random
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cnpj_validator.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 William Weckl
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,17 @@
1
+ # nis_validator
2
+
3
+ NIS validation for ActiveModel
4
+
5
+ ## Installation
6
+
7
+ gem "nis_validator"
8
+
9
+ ## Usage
10
+
11
+ class User < ActiveRecord::Base
12
+ validates :nis, :nis => true
13
+ end
14
+
15
+ ## License
16
+
17
+ NIS Validator is released under the MIT license
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ class NisValidator < ActiveModel::EachValidator
2
+ autoload :Nis, 'nis_validator/nis'
3
+
4
+ def validate_each(record, attribute, value)
5
+ message = options[:message].presence || :invalid
6
+ record.errors.add(attribute, message) unless Nis.valid?(value)
7
+ end
8
+ end
@@ -0,0 +1,63 @@
1
+ class NisValidator::Nis
2
+ def self.valid?(number)
3
+ new(number).valid?
4
+ end
5
+
6
+ attr_accessor :number, :x
7
+
8
+ def initialize(number = '')
9
+ @number = number.to_s
10
+ end
11
+
12
+ def digits
13
+ @digits ||= number.scan(/\d/).map(&:to_i)
14
+ end
15
+
16
+ def plain
17
+ @plain ||= digits.join
18
+ end
19
+
20
+ def digit
21
+ @digit ||= digits.last
22
+ end
23
+
24
+ def identifier
25
+ last_eleven = digits.last(11)
26
+ @identifier ||= last_eleven.join[0..-2]
27
+ end
28
+
29
+ def identifier_digits
30
+ @identifier_digits ||= identifier.scan(/\d/).map(&:to_i)
31
+ end
32
+
33
+ def valid?
34
+ return false if digits.size < 11
35
+ return false if black_list.include?(number)
36
+ result = (11 - (sum_products % 11))
37
+ true if ((result == digit) || (result == 10 && digit == 0) || (result == 11 && digit == 0))
38
+ end
39
+
40
+ private
41
+
42
+ def multiply_weights
43
+ @x = []
44
+ i = 0
45
+ [3, 2, 9, 8, 7, 6, 5, 4, 3, 2].each do |weight|
46
+ @x << identifier_digits[i] * weight
47
+ i += 1
48
+ end
49
+ end
50
+
51
+ def sum_products
52
+ multiply_weights
53
+ @sum = 0
54
+ 10.times do |n|
55
+ @sum += x[n]
56
+ end
57
+ @sum
58
+ end
59
+
60
+ def black_list
61
+ %w(000.0000.000-0)
62
+ end
63
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "nis_validator"
4
+ s.version = "0.0.1"
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ["William Weckl"]
7
+ s.email = ["william.weckl@gmail.com"]
8
+ s.homepage = "https://github.com/williamweckl/nis_validator"
9
+ s.summary = %q{NIS validation for ActiveModel}
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ s.require_paths = ["lib"]
15
+
16
+ s.add_dependency 'activemodel', '>= 3.0'
17
+ s.add_development_dependency 'rake', '>= 0.8.7'
18
+ s.add_development_dependency "rspec", '~> 3.1.0'
19
+ end
@@ -0,0 +1,75 @@
1
+ require 'nis_validator/nis'
2
+
3
+ describe NisValidator do
4
+
5
+ subject { NisValidator::Nis.new }
6
+
7
+ context 'with valid nis' do
8
+
9
+ it do
10
+ subject.number = '125.8702.350-7'
11
+ should be_valid
12
+ end
13
+
14
+ it do
15
+ subject.number = '125.1582.032-0'
16
+ should be_valid
17
+ end
18
+
19
+ it do
20
+ subject.number = '125.3666.079-8'
21
+ should be_valid
22
+ end
23
+
24
+ it do
25
+ subject.number = '125.2145.147-0'
26
+ should be_valid
27
+ end
28
+
29
+ it do
30
+ subject.number = '125.5785.452-4'
31
+ should be_valid
32
+ end
33
+
34
+ it do
35
+ subject.number = '125.2733.324-0'
36
+ should be_valid
37
+ end
38
+
39
+ it do
40
+ subject.number = '125.6523.422-0'
41
+ should be_valid
42
+ end
43
+
44
+ it do
45
+ subject.number = '1125.8702.350-7'
46
+ should be_valid
47
+ end
48
+
49
+ it do
50
+ subject.number = '1125.1582.032-0'
51
+ should be_valid
52
+ end
53
+
54
+ end
55
+
56
+ context 'with invalid nis' do
57
+
58
+ it do
59
+ subject.number = '000.0000.000-0'
60
+ should_not be_valid
61
+ end
62
+
63
+ it do
64
+ subject.number = '111.1111.111-1'
65
+ should_not be_valid
66
+ end
67
+
68
+ it do
69
+ subject.number = '222.1111.111-1'
70
+ should_not be_valid
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'active_model'
5
+ require 'nis_validator'
6
+ require 'ostruct'
7
+
8
+ RSpec.configure do |config|
9
+
10
+ end
11
+
12
+ class User < OpenStruct
13
+ include ActiveModel::Validations
14
+ validates :nis, :nis => true
15
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nis_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - William Weckl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.7
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ description:
56
+ email:
57
+ - william.weckl@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - MIT-LICENSE
66
+ - README.markdown
67
+ - Rakefile
68
+ - lib/nis_validator.rb
69
+ - lib/nis_validator/nis.rb
70
+ - nis_validator.gemspec
71
+ - spec/nis_validator_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/williamweckl/nis_validator
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.4
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: NIS validation for ActiveModel
96
+ test_files: []