nip 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README +41 -0
- data/Rakefile +2 -0
- data/lib/nip.rb +43 -0
- data/lib/version.rb +3 -0
- data/nip.gemspec +21 -0
- data/test/nip_test.rb +26 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
Nip
|
2
|
+
===
|
3
|
+
|
4
|
+
This gem provides the Nip class to check the NIP number.
|
5
|
+
|
6
|
+
NIP (Numer Identyfikacji Podatkowej) is the Polish Tax Identification Number.
|
7
|
+
|
8
|
+
More information (in Polish language):
|
9
|
+
http://pl.wikipedia.org/wiki/NIP
|
10
|
+
|
11
|
+
If you are looking for the Nip as a plugin:
|
12
|
+
http://github.com/macuk/nip-plugin
|
13
|
+
|
14
|
+
Installation
|
15
|
+
============
|
16
|
+
|
17
|
+
gem install nip
|
18
|
+
|
19
|
+
Example
|
20
|
+
=======
|
21
|
+
|
22
|
+
Standalone class:
|
23
|
+
|
24
|
+
nip = Nip.new('845-167-08-82')
|
25
|
+
nip.valid? # true
|
26
|
+
nip.to_s # '8451670882'
|
27
|
+
|
28
|
+
ActiveRecord:
|
29
|
+
|
30
|
+
class Company < ActiveRecord::Base
|
31
|
+
validate :check_nip
|
32
|
+
|
33
|
+
private
|
34
|
+
def check_nip
|
35
|
+
n = Nip.new(nip)
|
36
|
+
errors[:nip] << 'Invalid NIP number' unless n.valid?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
Copyright (c) 2010 Piotr Macuk, released under the MIT license
|
data/Rakefile
ADDED
data/lib/nip.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
class Nip
|
2
|
+
def initialize(number)
|
3
|
+
@number = number.to_s.gsub(/-/, '')
|
4
|
+
@valid = validate
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
@number
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
@valid
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def validate
|
17
|
+
has_proper_length and \
|
18
|
+
contains_only_digits and \
|
19
|
+
has_proper_control_digit
|
20
|
+
end
|
21
|
+
|
22
|
+
def has_proper_length
|
23
|
+
@number.size == 10
|
24
|
+
end
|
25
|
+
|
26
|
+
def contains_only_digits
|
27
|
+
@number =~ /^\d+$/
|
28
|
+
end
|
29
|
+
|
30
|
+
def weights
|
31
|
+
[6, 5, 7, 2, 3, 4, 5, 6, 7]
|
32
|
+
end
|
33
|
+
|
34
|
+
def has_proper_control_digit
|
35
|
+
digits = @number.split('').map { |i| i.to_i }
|
36
|
+
i = 0; sum = 0
|
37
|
+
digits[0..-2].each do |d|
|
38
|
+
sum += (d * weights[i])
|
39
|
+
i += 1
|
40
|
+
end
|
41
|
+
(sum % 11) % 10 == digits[-1]
|
42
|
+
end
|
43
|
+
end
|
data/lib/version.rb
ADDED
data/nip.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "nip"
|
7
|
+
s.version = Nip::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Piotr Macuk"]
|
10
|
+
s.email = ["piotr@macuk.pl"]
|
11
|
+
s.homepage = "http://github.com/macuk/nip"
|
12
|
+
s.summary = %q{NIP validation}
|
13
|
+
s.description = %q{Nip class to check NIP number}
|
14
|
+
|
15
|
+
s.rubyforge_project = "nip"
|
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
|
+
end
|
data/test/nip_test.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'nip'
|
3
|
+
|
4
|
+
class NipTest < Test::Unit::TestCase
|
5
|
+
Valid = %w(845-167-08-82 583-10-23-182 5840955985)
|
6
|
+
Invalid = %w(123 845-167-00-00 000-111-22-33)
|
7
|
+
|
8
|
+
def test_number_and_to_s_methods
|
9
|
+
n = Nip.new('845-167-08-82')
|
10
|
+
assert '8451670882', n.to_s
|
11
|
+
n = Nip.new(8451670882)
|
12
|
+
assert '8451670882', n.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_valid_method
|
16
|
+
Valid.each do |number|
|
17
|
+
n = Nip.new(number)
|
18
|
+
assert n.valid?
|
19
|
+
end
|
20
|
+
|
21
|
+
Invalid.each do |number|
|
22
|
+
n = Nip.new(number)
|
23
|
+
assert_equal false, n.valid?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 0
|
9
|
+
version: 0.9.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Piotr Macuk
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-20 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Nip class to check NIP number
|
22
|
+
email:
|
23
|
+
- piotr@macuk.pl
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- README
|
34
|
+
- Rakefile
|
35
|
+
- lib/nip.rb
|
36
|
+
- lib/version.rb
|
37
|
+
- nip.gemspec
|
38
|
+
- test/nip_test.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/macuk/nip
|
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
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: nip
|
67
|
+
rubygems_version: 1.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: NIP validation
|
71
|
+
test_files: []
|
72
|
+
|