nip_pesel_regon 1.0.0.rc2

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: f30d4c87a60d20d3acbd9fb0507e5238169cad5d
4
+ data.tar.gz: 270fc6bfd476bfaf6912217ad9633cd246bf996a
5
+ SHA512:
6
+ metadata.gz: d671933c3a82ab3a6680ebacbe2fddc161134a85718c4f088e77c5b00fb79d07a102394b939c6acf9fcffa9e2227494d6ef8f059f70d2e513dc45be32261ce2b
7
+ data.tar.gz: 5ee1a51fbc3531d088a2fafdecc35385e2c5f0e7771b300e38190b636970c8ed4656c83e71fb3aae8f52d9651a4220b120e7f52218c791b289798d2864f574e7
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1
6
+ - 2.2
7
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nip_pesel_regon.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 psagan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+
2
+ # NipPeselRegon - rc1
3
+
4
+ [![Build Status](https://travis-ci.org/psagan/nip_pesel_regon.svg?branch=master)](https://travis-ci.org/psagan/nip_pesel_regon)
5
+
6
+ Validates polish identification numbers NIP, PESEL, REGON. Can be used in any ruby script or integrated with Rails validation
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'nip_pesel_regon'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install nip_pesel_regon
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ class Company < ActiveRecord::Base
28
+ validates_nip_of :my_nip_field
29
+ validates_pesel_of :my_pesel_field
30
+ validates_regon_of :my_regon_field
31
+ end
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/psagan/nip_pesel_regon.
37
+
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nip_pesel_regon"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ require "nip_pesel_regon/version"
2
+ require 'nip_pesel_regon/calculator/checksum'
3
+ require 'nip_pesel_regon/validator/base'
4
+ require "nip_pesel_regon/validator/nip"
5
+ require "nip_pesel_regon/validator/regon"
6
+ require "nip_pesel_regon/validator/regon9"
7
+ require "nip_pesel_regon/validator/regon14"
8
+ require "nip_pesel_regon/validator/pesel"
9
+ require "nip_pesel_regon/validator/not_valid"
10
+
11
+
12
+ if defined? Rails
13
+ require "nip_pesel_regon/integration/rails"
14
+ end
15
+
16
+
17
+ module NipPeselRegon
18
+ # Your code goes here...
19
+ end
@@ -0,0 +1,17 @@
1
+ module NipPeselRegon
2
+ module Calculator
3
+
4
+ # This class is responsible
5
+ # for calculating checksum
6
+ class Checksum
7
+ def initialize(weights, number)
8
+ @weights = weights
9
+ @number = number
10
+ end
11
+
12
+ def calculate
13
+ @weights.each_with_index.inject(0) {|sum, (weight, i)| sum + @number[i].to_i * weight}
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,66 @@
1
+ require 'active_support/inflector'
2
+
3
+ module NipPeselRegon
4
+ module Integration
5
+ # class responsible for simple integration
6
+ # NIP/PESEL/REGON validation into active model validation
7
+ class Rails < ActiveModel::EachValidator
8
+
9
+ # hash with default options
10
+ DEFAULT_OPTIONS = {
11
+ save_normalized: true,
12
+ message: 'invalid %s'
13
+ }
14
+
15
+ def initialize(*attr)
16
+ super
17
+ handle_default_options
18
+ end
19
+
20
+ # method responsible for handling default options
21
+ # and merging them with custom options provided by developer
22
+ def handle_default_options
23
+ # before mergin check if custom :message provided by developer
24
+ message_provided = options[:message]
25
+
26
+ # do merge
27
+ @options = DEFAULT_OPTIONS.merge(options)
28
+
29
+ # handle default message template based on condition if
30
+ # custom message was provided by developer
31
+ @options[:message] = options[:message] % options[:validator].upcase unless message_provided
32
+ end
33
+
34
+
35
+ def validate_each(record, attribute, value)
36
+ # do validation
37
+ validator = "NipPeselRegon::Validator::#{options[:validator].capitalize}".constantize.new(value, options)
38
+
39
+ if validator.valid?
40
+ # if NIP/PESEL/REGON is valid and 'save_normalized' options is set to true
41
+ # then assign normalized value to attribute otherwise it will be
42
+ # saved as provided by user (without normalization)
43
+ record.send(attribute.to_s + '=', validator.to_s) if options[:save_normalized]
44
+ else
45
+ # add errors message to record's attribute
46
+ record.errors[attribute] << options[:message]
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ module ActiveModel
54
+ module Validations
55
+ module HelperMethods
56
+ %w{nip pesel regon}.each do |validator_name|
57
+ define_method("validates_#{validator_name}_of") do |*attr_names|
58
+ options = attr_names.extract_options!
59
+ options[:validator] = validator_name
60
+ attr_names << options
61
+ validates_with NipPeselRegon::Integration::Rails, _merge_attributes(attr_names)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,61 @@
1
+ module NipPeselRegon
2
+ module Validator
3
+ class Base
4
+
5
+ attr_reader :number, :original_number, :options
6
+
7
+ def initialize(number, options = {})
8
+ # need to make string from input number because
9
+ # whole functionality is based on string
10
+ @number = number.to_s
11
+ # keep original number
12
+ @original_number = number
13
+
14
+ @options = options
15
+
16
+ # normalize provided number
17
+ normalize
18
+ end
19
+
20
+ def valid?
21
+ # check if NIP provided has proper format
22
+ return false unless has_proper_format?
23
+ validate
24
+ end
25
+
26
+ def to_s
27
+ number
28
+ end
29
+
30
+ private
31
+
32
+ # method responsible for detection if number provided has proper format
33
+ def has_proper_format?
34
+ !number.nil? && matches_pattern?
35
+ end
36
+
37
+ # method responsible for pattern matching
38
+ def matches_pattern?
39
+ self.class::PATTERN =~ number
40
+ end
41
+
42
+ def validate
43
+ raise NoMethodError, "No validate method defined in #{self.class}"
44
+ end
45
+
46
+ def checksum
47
+ @checksum ||= calculate_checksum
48
+ end
49
+
50
+ def calculate_checksum
51
+ NipPeselRegon::Calculator::Checksum.new(self.class::WEIGHTS, number).calculate
52
+ end
53
+
54
+ def normalize
55
+ return number if options[:strict] && options[:strict] == true
56
+ @number = number.gsub(/[-\s]/, '')
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,35 @@
1
+ module NipPeselRegon
2
+ module Validator
3
+
4
+ # class responsible
5
+ # for nip validation (including standard normalization)
6
+ # please not only polish NIP's are valid with or without 'PL' prefix
7
+ # NIP's with other prefixes like eg. 'DE', 'EN' are invalid
8
+ class Nip < Base
9
+ # array with weights
10
+ WEIGHTS = [6, 5, 7, 2, 3, 4, 5, 6, 7] # @todo - candidate for sumcalculator object resonsible only for calculating sum
11
+
12
+ # pattern for NIP
13
+ PATTERN = /^\d{10}$/
14
+
15
+ private
16
+
17
+ # main validation method
18
+ def validate
19
+ # check if calculated checksum modulo 11 is equal to last digit of NIP
20
+ # if yes then validation is correct
21
+ (checksum % 11) == number[-1].to_i
22
+ end
23
+
24
+ # method responsible for
25
+ # input number normalization before validation
26
+ # 1. Makes input a string
27
+ # 2. Removes all '-' and whitespaces
28
+ # 3. remove 'PL' substring from the beginning if any
29
+ def normalize
30
+ @number = super.gsub(/^PL/i, '') # only pl NIP's are valid
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ module NipPeselRegon
2
+ module Validator
3
+
4
+ # Class responsible for
5
+ # acting as not valid in
6
+ # Null Object Pattern
7
+ class NotValid
8
+ def valid?
9
+ false
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module NipPeselRegon
2
+ module Validator
3
+ class Pesel < Base
4
+ # last digit is 1 - it's not official weight but
5
+ # but it will help to calculate all checksum like: a+3b+7c+9d+e+3f+7g+9h+i+3j+k
6
+ # en - https://en.wikipedia.org/wiki/PESEL
7
+ # pl - https://pl.wikipedia.org/wiki/PESEL
8
+ WEIGHTS = [1, 3, 7, 9, 1, 3 ,7, 9, 1, 3, 1].freeze
9
+
10
+ PATTERN = /^\d{11}$/
11
+
12
+ private
13
+
14
+ def validate
15
+ checksum % 10 == 0
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ module NipPeselRegon
2
+ module Validator
3
+
4
+ # Extends Abstract to meet common, public interface
5
+ class Regon < Base
6
+
7
+ attr_reader :regon_validator
8
+
9
+ def initialize(*attr)
10
+ super
11
+ case(number.length) # number is after normalization
12
+ when 9
13
+ @regon_validator = Regon9.new(number)
14
+ when 14
15
+ @regon_validator = Regon14.new(number)
16
+ else
17
+ @regon_validator = NotValid.new # Null Object Pattern
18
+ end
19
+ end
20
+
21
+ def valid?
22
+ regon_validator.valid?
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ module NipPeselRegon
2
+ module Validator
3
+ class Regon14 < Base
4
+ WEIGHTS = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]
5
+
6
+ PATTERN = /^\d{14}$/
7
+
8
+ private
9
+
10
+ def validate
11
+ # check if REGON with 14 digits is proper
12
+ # REGON with 9 digits by slicing
13
+ # first 9 digits and testing against Regon9
14
+ return false unless is_valid_regon9?
15
+
16
+ # check if calculated checksum modulo 11 is equal to last digit of REGON
17
+ # if yes then validation is correct
18
+ (checksum % 11) == number[-1].to_i
19
+ end
20
+
21
+ # method responsible
22
+ # for testing if first 9 digits of 14-digit REGON
23
+ # are proper 9-digits regon (Regon9 class)
24
+ def is_valid_regon9?
25
+ Regon9.new(number[0,9]).valid?
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ module NipPeselRegon
2
+ module Validator
3
+ class Regon9 < Base
4
+ WEIGHTS = [8, 9, 2, 3, 4, 5, 6, 7]
5
+
6
+ PATTERN = /^\d{9}$/
7
+
8
+ private
9
+
10
+ def validate
11
+ mod = checksum % 11
12
+
13
+ # if mod is equal to 10 then set it to 0
14
+ mod = 0 if mod == 10
15
+
16
+ # check if mod is equal to last digit of REGON
17
+ # if yes then validation is correct
18
+ mod == number[-1].to_i
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,3 @@
1
+ module NipPeselRegon
2
+ VERSION = "1.0.0.rc2"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nip_pesel_regon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nip_pesel_regon"
8
+ spec.version = NipPeselRegon::VERSION
9
+ spec.authors = ["Patryk Sagan"]
10
+ spec.email = ["patryk.sagan@icloud.com"]
11
+
12
+ spec.summary = %q{NIP, PESEL, REGON validator for Ruby, Rails}
13
+ spec.description = %q{Validates polish identification numbers NIP, PESEL, REGON. Can be used in any ruby script or integrated with Rails validation}
14
+ spec.homepage = "https://github.com/psagan/nip_pesel_regon"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "supermodel", "~> 0.1", ">= 0.1.6"
26
+ spec.add_development_dependency "activesupport", "~> 3.0", ">= 3.0.0"
27
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nip_pesel_regon
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc2
5
+ platform: ruby
6
+ authors:
7
+ - Patryk Sagan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: supermodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 0.1.6
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '0.1'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.1.6
75
+ - !ruby/object:Gem::Dependency
76
+ name: activesupport
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.0.0
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '3.0'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 3.0.0
95
+ description: Validates polish identification numbers NIP, PESEL, REGON. Can be used
96
+ in any ruby script or integrated with Rails validation
97
+ email:
98
+ - patryk.sagan@icloud.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - ".travis.yml"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - bin/console
110
+ - bin/setup
111
+ - lib/nip_pesel_regon.rb
112
+ - lib/nip_pesel_regon/calculator/checksum.rb
113
+ - lib/nip_pesel_regon/integration/rails.rb
114
+ - lib/nip_pesel_regon/validator/base.rb
115
+ - lib/nip_pesel_regon/validator/nip.rb
116
+ - lib/nip_pesel_regon/validator/not_valid.rb
117
+ - lib/nip_pesel_regon/validator/pesel.rb
118
+ - lib/nip_pesel_regon/validator/regon.rb
119
+ - lib/nip_pesel_regon/validator/regon14.rb
120
+ - lib/nip_pesel_regon/validator/regon9.rb
121
+ - lib/nip_pesel_regon/version.rb
122
+ - nip_pesel_regon.gemspec
123
+ homepage: https://github.com/psagan/nip_pesel_regon
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">"
139
+ - !ruby/object:Gem::Version
140
+ version: 1.3.1
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.4.8
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: NIP, PESEL, REGON validator for Ruby, Rails
147
+ test_files: []