validatr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 18066b6a0c46e32c74c331af7fe17efe05e13666
4
+ data.tar.gz: a2ee3976cd2dea56a26484cf52eb374ab965b5f9
5
+ SHA512:
6
+ metadata.gz: 49b253cc9a94d554b8c0cb7de346fb95c987a7891e6e8a1e2180aa6d570aa13f7a8ad8ba0886b685ad1d3733c2f35a67ba05168e0364dcf51ffebeddf953c643
7
+ data.tar.gz: 8b781b98a77d5d7ceb9f997a1555d60960d5054cdf6f5e911dfa272f326c656fd0c124c1e582be67790e3f4526358e1a3860744b0af7683963eb35fd68c6d4c7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Julien Roger
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Validatr
2
+
3
+ An implementation of several error checking algorithms, including Luhn and Damm, for string and number error checking.
4
+
5
+ This gem currently only supports validation of integers and strings of integers.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "validatr"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install validatr
22
+
23
+ ## Usage
24
+
25
+
26
+ ```ruby
27
+ require "validatr"
28
+
29
+ Validatr::Luhn.check_digit 12345678 # => 2
30
+ Validatr::Verhoeff.check_digit 12345678 # => 4
31
+ Validatr::Damm.check_digit 12345678 # => 6
32
+
33
+ Validatr::Luhn.valid? 123456782 # => true
34
+ Validatr::Verhoeff.valid? 123456784 # => true
35
+ Validatr::Damm.valid? 123456786 # => true
36
+
37
+ 123456782.valid_luhn? # => true
38
+ 123456782.valid_verhoeff? # => false
39
+ 123456782.valid_damm? # => false
40
+
41
+ (123456784.0).valid_luhn? # => false
42
+ (123456784.0).valid_verhoeff? # => true
43
+ (123456784.0).valid_damm? # => false
44
+
45
+ "123456786".valid_luhn? # => false
46
+ "123456786".valid_verhoeff? # => false
47
+ "123456786".valid_damm? # => true
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/julienroger/validatr.
53
+
54
+
55
+ ## License
56
+
57
+ [MIT](./LICENSE)
58
+
59
+ Copyright (c) 2017 Julien Roger
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "validatr"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validatr
4
+ module Damm
5
+ extend Utils
6
+
7
+ # Totally Anti-Symmetric Quasigroup
8
+ TASQ = [
9
+ [0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
10
+ [7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
11
+ [4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
12
+ [1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
13
+ [6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
14
+ [3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
15
+ [5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
16
+ [8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
17
+ [9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
18
+ [2, 5, 8, 1, 4, 3, 6, 7, 9, 0]
19
+ ].freeze
20
+
21
+ # Returns true if a number is a valid Damm number, false otherwise
22
+ def self.valid?(number)
23
+ encode(number).zero?
24
+ end
25
+
26
+ # Generates the check digit for a number
27
+ def self.check_digit(number)
28
+ encode number
29
+ end
30
+
31
+ def self.encode(number)
32
+ digits = digits_from_number(number)
33
+ i = 0
34
+ digits.map { |c| i = TASQ[i][c] }
35
+ i
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Numeric
4
+ def valid_luhn?
5
+ to_s.valid_luhn?
6
+ end
7
+
8
+ def valid_verhoeff?
9
+ to_s.valid_verhoeff?
10
+ end
11
+
12
+ def valid_damm?
13
+ to_s.valid_damm?
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ def valid_luhn?
5
+ Validatr::Luhn.valid? self
6
+ end
7
+
8
+ def valid_verhoeff?
9
+ Validatr::Verhoeff.valid? self
10
+ end
11
+
12
+ def valid_damm?
13
+ Validatr::Damm.valid? self
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "validatr/ext/string"
4
+ require "validatr/ext/numeric"
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validatr
4
+ module Luhn
5
+ extend Utils
6
+
7
+ def self.valid?(number, n = 10)
8
+ digits = digits_from_number(number)
9
+ sum = sum_digits(digits)
10
+ (sum % n).zero?
11
+ end
12
+
13
+ def self.check_digit(number, n = 10)
14
+ digits = digits_from_number(number)
15
+ digits.push(0)
16
+ sum = sum_digits(digits)
17
+ (sum * 9) % n
18
+ end
19
+
20
+ def self.sum_digits(digits)
21
+ digits.reverse.map.with_index do |d, i|
22
+ if i.even?
23
+ d
24
+ else
25
+ (d < 5 ? (d * 2) : ((d * 2) % 10) + 1)
26
+ end
27
+ end.inject(:+)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validatr
4
+ module Utils
5
+ def digits_from_number(number)
6
+ number.to_i.to_s.chars.map(&:to_i)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validatr
4
+ module Verhoeff
5
+ extend Utils
6
+
7
+ D = [
8
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
9
+ [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
10
+ [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
11
+ [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
12
+ [4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
13
+ [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
14
+ [6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
15
+ [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
16
+ [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
17
+ [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
18
+ ].freeze
19
+
20
+ F = [
21
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
22
+ [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
23
+ [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
24
+ [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
25
+ [9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
26
+ [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
27
+ [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
28
+ [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
29
+ ].freeze
30
+
31
+ INV = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9].freeze
32
+
33
+ # Returns true if a number is a valid Verhoeff number, false otherwise
34
+ def self.valid?(number)
35
+ digits = digits_from_number(number)
36
+ c = encode(digits)
37
+ c.zero?
38
+ end
39
+
40
+ # Generates the check digit for a number
41
+ def self.check_digit(number)
42
+ digits = digits_from_number(number)
43
+ digits.push(0)
44
+ c = encode(digits)
45
+ INV[c]
46
+ end
47
+
48
+ def self.encode(digits)
49
+ c = 0
50
+ digits.reverse.map.with_index do |d, i|
51
+ f = F[i % 8][d]
52
+ c = D[c][f]
53
+ end
54
+ c
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validatr
4
+ VERSION = "0.1.0"
5
+ end
data/lib/validatr.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.expand_path("..", __FILE__))
4
+
5
+ require "validatr/version"
6
+ require "validatr/utils"
7
+ require "validatr/luhn"
8
+ require "validatr/damm"
9
+ require "validatr/verhoeff"
10
+ require "validatr/ext"
11
+
12
+ module Validatr
13
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validatr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Julien Roger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-06 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
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.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.49'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.49'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.15'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.15'
83
+ description: An implementation of several error checking algorithms,including Luhn
84
+ and Damm.
85
+ email:
86
+ - jroger@abletribe.com
87
+ executables:
88
+ - console
89
+ - setup
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - LICENSE
94
+ - README.md
95
+ - bin/console
96
+ - bin/setup
97
+ - lib/validatr.rb
98
+ - lib/validatr/damm.rb
99
+ - lib/validatr/ext.rb
100
+ - lib/validatr/ext/numeric.rb
101
+ - lib/validatr/ext/string.rb
102
+ - lib/validatr/luhn.rb
103
+ - lib/validatr/utils.rb
104
+ - lib/validatr/verhoeff.rb
105
+ - lib/validatr/version.rb
106
+ homepage: https://github.com/julienroger/validatr
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.11
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A set of tools for string and number error checking.
130
+ test_files: []