luhn 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/luhn.rb +40 -21
- data/lib/luhn/civic_number.rb +5 -1
- data/lib/luhn/version.rb +2 -3
- data/spec/spec_civic_number.rb +7 -1
- data/spec/spec_luhn.rb +5 -1
- metadata +33 -40
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: acc7be20238719d56f27cf60436232d6933aeca2
|
4
|
+
data.tar.gz: 351bc940bea52556e369602a546c5bcebdaf1e02
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c25701d9c3466526efc767fee9c0c0c7c384044c8894d84e635094b687aba5be016a1b99cfda8f0c7181693c7ac89a8cdd2d664263517d2b7132d0e1614c588
|
7
|
+
data.tar.gz: 6e0cff66c776898bb10ef90cb58129af34e64dfbef5b569af2f7998a8155c755c30d5ced4fd21f538f6f03d85e5cbb3930ea046b8752982e46723743a6cfae68
|
data/lib/luhn.rb
CHANGED
@@ -1,40 +1,59 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
require
|
2
|
+
require "luhn/extensions"
|
3
3
|
|
4
4
|
module Luhn
|
5
|
-
|
5
|
+
def self.valid? value
|
6
|
+
LuhnValue.new(value).valid?
|
7
|
+
end
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
9
|
+
def self.control_digit value
|
10
|
+
LuhnValue.new(value).control_digit
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def self.generate size, options = {}
|
14
|
+
LuhnValue.generate size, options
|
15
|
+
end
|
16
|
+
|
17
|
+
class LuhnValue
|
18
|
+
attr_reader :number
|
19
|
+
|
20
|
+
def initialize number
|
21
|
+
self.number = number
|
15
22
|
end
|
16
23
|
|
17
|
-
def generate
|
24
|
+
def self.generate size, options = {}
|
18
25
|
generated = options[:prefix] || ''
|
19
26
|
(size - generated.size - 1).times { |i| generated += rand(10).to_s }
|
20
|
-
|
27
|
+
|
28
|
+
generated + self.new(generated).control_digit.to_s
|
21
29
|
end
|
22
30
|
|
23
|
-
|
31
|
+
def valid?
|
32
|
+
number != "" && checksum(:odd) % 10 == 0
|
33
|
+
end
|
34
|
+
|
35
|
+
def control_digit
|
36
|
+
sum = checksum(:even)
|
37
|
+
(sum % 10 != 0) ? 10 - (sum % 10) : 0
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def number= number
|
43
|
+
@number = number.to_s.gsub(/\D/, "")
|
44
|
+
end
|
24
45
|
|
25
|
-
def checksum(
|
46
|
+
def checksum(operation)
|
26
47
|
i = 0
|
27
|
-
|
48
|
+
compare_method = (operation == :even) ? :== : :>
|
49
|
+
self.number.reverse.split("").inject(0) do |sum, c|
|
28
50
|
n = c.to_i
|
29
|
-
|
30
|
-
(i % 2 == 0) ? n * 2 : n
|
31
|
-
else
|
32
|
-
(i % 2 != 0) ? n * 2 : n
|
33
|
-
end
|
51
|
+
weight = (i % 2).send(compare_method, 0) ? n * 2 : n
|
34
52
|
i += 1
|
35
|
-
sum +=
|
53
|
+
sum += weight < 10 ? weight : weight - 9
|
36
54
|
end
|
37
55
|
end
|
38
56
|
end
|
57
|
+
end
|
39
58
|
|
40
|
-
|
59
|
+
require "luhn/civic_number"
|
data/lib/luhn/civic_number.rb
CHANGED
data/lib/luhn/version.rb
CHANGED
data/spec/spec_civic_number.rb
CHANGED
@@ -46,8 +46,14 @@ describe Luhn::CivicNumber do
|
|
46
46
|
civic_number.birth_date.day.must_equal 1
|
47
47
|
end
|
48
48
|
|
49
|
+
it "formats the civic number" do
|
50
|
+
civic_number = Luhn::CivicNumber.new('3001018194')
|
51
|
+
|
52
|
+
civic_number.formatted.must_equal "300101-8194"
|
53
|
+
end
|
54
|
+
|
49
55
|
it 'generates a valid random civic number' do
|
50
56
|
civic_number = Luhn::CivicNumber.generate
|
51
57
|
Luhn::CivicNumber.new(civic_number).valid?.must_equal true
|
52
58
|
end
|
53
|
-
end
|
59
|
+
end
|
data/spec/spec_luhn.rb
CHANGED
@@ -9,6 +9,10 @@ describe Luhn do
|
|
9
9
|
Luhn.valid?('1111111111').must_equal false
|
10
10
|
end
|
11
11
|
|
12
|
+
it 'requires numbers' do
|
13
|
+
Luhn.valid?('frefre').must_equal false
|
14
|
+
end
|
15
|
+
|
12
16
|
it 'generates a number string that satisfies luhn' do
|
13
17
|
luhn_string = Luhn.generate(rand(32)+1)
|
14
18
|
Luhn.valid?(luhn_string).must_equal true
|
@@ -28,4 +32,4 @@ describe Luhn do
|
|
28
32
|
it 'calculates the control digit' do
|
29
33
|
Luhn.control_digit('111111111').must_equal 6
|
30
34
|
end
|
31
|
-
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,38 +1,36 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: luhn
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.1.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
-
|
6
|
+
authors:
|
7
|
+
- Joel Junström
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2015-09-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
16
14
|
name: minitest
|
17
|
-
|
18
|
-
|
19
|
-
none: false
|
20
|
-
requirements:
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
21
17
|
- - ~>
|
22
|
-
- !ruby/object:Gem::Version
|
18
|
+
- !ruby/object:Gem::Version
|
23
19
|
version: 2.6.0
|
24
20
|
type: :development
|
25
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.6.0
|
26
27
|
description:
|
27
|
-
email:
|
28
|
+
email:
|
28
29
|
- joel.junstrom@gmail.com
|
29
30
|
executables: []
|
30
|
-
|
31
31
|
extensions: []
|
32
|
-
|
33
32
|
extra_rdoc_files: []
|
34
|
-
|
35
|
-
files:
|
33
|
+
files:
|
36
34
|
- lib/luhn/civic_number.rb
|
37
35
|
- lib/luhn/extensions/numeric.rb
|
38
36
|
- lib/luhn/extensions/string.rb
|
@@ -47,30 +45,25 @@ files:
|
|
47
45
|
- README.rdoc
|
48
46
|
homepage: http://github.com/joeljunstrom/ruby_luhn
|
49
47
|
licenses: []
|
50
|
-
|
48
|
+
metadata: {}
|
51
49
|
post_install_message:
|
52
50
|
rdoc_options: []
|
53
|
-
|
54
|
-
require_paths:
|
51
|
+
require_paths:
|
55
52
|
- lib
|
56
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version: "0"
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
68
63
|
requirements: []
|
69
|
-
|
70
64
|
rubyforge_project:
|
71
|
-
rubygems_version:
|
65
|
+
rubygems_version: 2.0.14
|
72
66
|
signing_key:
|
73
|
-
specification_version:
|
67
|
+
specification_version: 4
|
74
68
|
summary: A small implementation of the Luhn algorithm.
|
75
69
|
test_files: []
|
76
|
-
|