luhn 1.0.0 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: acc7be20238719d56f27cf60436232d6933aeca2
4
- data.tar.gz: 351bc940bea52556e369602a546c5bcebdaf1e02
2
+ SHA256:
3
+ metadata.gz: 48cccdf14be3fc83b329b2b7407ebd4410c889d62e5fc9e526b5beded1a4f137
4
+ data.tar.gz: 31fb9fa178dcfc3068a5e4563fecd2756786334ffcda5dcb1c02831a7c085030
5
5
  SHA512:
6
- metadata.gz: 1c25701d9c3466526efc767fee9c0c0c7c384044c8894d84e635094b687aba5be016a1b99cfda8f0c7181693c7ac89a8cdd2d664263517d2b7132d0e1614c588
7
- data.tar.gz: 6e0cff66c776898bb10ef90cb58129af34e64dfbef5b569af2f7998a8155c755c30d5ced4fd21f538f6f03d85e5cbb3930ea046b8752982e46723743a6cfae68
6
+ metadata.gz: 1ed8c51ba3cf801e1f2ec7eb17b244e7bd0fc06b40899ef7f5d1a230ddd327ff01376fe54d5af9ec15676261ac09844e0f0c747dda0acc8122be805bbcb55482
7
+ data.tar.gz: bce0cbb1eb0a0b31e8d77e6794935c01377424139c21252536419376e70a0116e81dbdab0645844a60bbb2c863ae570a738593f576329f37870d79c20eec4582
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Ruby class for handling basic Luhn number generation and verification
2
+
3
+ Includes a class to handle Swedish civic numbers (Personnummer).
4
+
5
+ The interface supports checking validity (length, valid date and satisfies luhn),
6
+ returning the sex, the control digit, and generating random civic numbers.
7
+
8
+ ## Install
9
+
10
+ ```shell
11
+ $ gem install luhn
12
+ ```
13
+
14
+ ## Usage:
15
+
16
+ ### Basic Luhn
17
+
18
+ ```ruby
19
+ Luhn.valid?('0465827879483596') # true
20
+ Luhn.control_digit('046582787948359') # 6
21
+ Luhn.generate(n) # returns a random number of n length that satisfies luhn
22
+
23
+ '0465827879483596'.valid_luhn? # true
24
+ 0465827879483596.valid_luhn? # true
25
+ ```
26
+
27
+ ### Swedish civic numbers
28
+
29
+ ```ruby
30
+ civic_number = Luhn::CivicNumber.new('19391030-3183')
31
+ civic_number.valid? # true
32
+ civic_number.sex # 'male'
33
+ civic_number.male? # true
34
+ civic_number.control_digit # 3
35
+
36
+ '391030-3183'.civic_number? # true
37
+ 3910303183.civic_number? # true
38
+ ```
39
+
40
+ ## About the Luhn algorithm
41
+
42
+ The following is an excerpt from [the Wikipedia article on the Luhn algorithm.](https://en.wikipedia.org/wiki/Luhn_algorithm)
43
+
44
+ The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10"
45
+ algorithm, is a simple checksum formula used to validate a variety of
46
+ identification numbers, such as credit card numbers, IMEI numbers,
47
+ National Provider Identifier numbers in US and Canadian Social Insurance Numbers,
48
+ and even South African ID numbers.
49
+
50
+ It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent
51
+ No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.
52
+
53
+ ## Copyright
54
+
55
+ Copyright (c) 2010 Joel Junström. See [LICENSE](LICENSE) for details.
@@ -2,14 +2,14 @@
2
2
  require 'ostruct'
3
3
  module Luhn
4
4
  class CivicNumber
5
- attr_reader :civic_number
5
+ attr_reader :value
6
6
 
7
- def initialize(string)
8
- @civic_number = cleanup_string(string.to_s)
7
+ def initialize string
8
+ self.value = string
9
9
  end
10
10
 
11
11
  def valid?
12
- @valid ||= civic_number.length == 10 && valid_date? && Luhn.valid?(civic_number)
12
+ @valid ||= value.length == 10 && valid_date? && Luhn.valid?(value)
13
13
  end
14
14
 
15
15
  def valid_date?
@@ -17,11 +17,11 @@ module Luhn
17
17
  end
18
18
 
19
19
  def control_digit
20
- @control_digit ||= Luhn.control_digit(civic_number[0...9])
20
+ @control_digit ||= Luhn.control_digit(value[0...9])
21
21
  end
22
22
 
23
23
  def sex
24
- @sex ||= valid? ? (civic_number[8...9].to_i.even? ? 'female' : 'male') : 'unknown'
24
+ valid? ? (value[8...9].to_i.even? ? 'female' : 'male') : 'unknown'
25
25
  end
26
26
 
27
27
  def female?
@@ -33,34 +33,38 @@ module Luhn
33
33
  end
34
34
 
35
35
  def birth_date
36
- @date ||= OpenStruct.new({
37
- :year => civic_number[0...2].to_i,
38
- :month => civic_number[2...4].to_i,
39
- :day => civic_number[4...6].to_i
36
+ OpenStruct.new({
37
+ :year => value[0...2].to_i,
38
+ :month => value[2...4].to_i,
39
+ :day => value[4...6].to_i
40
40
  })
41
41
  end
42
42
 
43
43
  def formatted
44
- to_s.insert(civic_number.length - 4, "-")
44
+ return value if value.length < 10
45
+
46
+ value.insert(value.length - 4, "-")
45
47
  end
46
48
 
47
49
  def to_s
48
- civic_number
50
+ value
49
51
  end
50
52
 
51
- class << self
52
- def generate
53
- date = Time.local(Time.now.year - rand(100) - 1, rand(12) + 1, rand(31) + 1)
54
- Luhn.generate(10, :prefix => date.strftime("%y%m%d"))
55
- end
53
+ # For backwards compability
54
+ def civic_number
55
+ value
56
56
  end
57
57
 
58
- private
59
-
60
- def cleanup_string(string)
61
- string.gsub!(/\D/, '')
62
- string.length == 12 ? string[2...12] : string
58
+ def self.generate
59
+ date = Time.local(Time.now.year - rand(100) - 1, rand(12) + 1, rand(31) + 1)
60
+ Luhn.generate(10, :prefix => date.strftime("%y%m%d"))
63
61
  end
64
62
 
63
+ private
64
+
65
+ def value= string
66
+ val = string.to_s.gsub(/\D/, '')
67
+ @value = val.length == 12 ? val[2...12] : val
68
+ end
65
69
  end
66
70
  end
data/lib/luhn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Luhn
2
- Version = "1.0.0"
2
+ Version = "1.0.3"
3
3
  end
@@ -46,10 +46,18 @@ 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')
49
+ describe "#formatted" do
50
+ it "formats the civic number" do
51
+ civic_number = Luhn::CivicNumber.new('3001018194')
52
+
53
+ civic_number.formatted.must_equal "300101-8194"
54
+ end
55
+
56
+ it "returns the origial if the civic number is to short" do
57
+ civic_number = Luhn::CivicNumber.new('300101819')
51
58
 
52
- civic_number.formatted.must_equal "300101-8194"
59
+ civic_number.formatted.must_equal "300101819"
60
+ end
53
61
  end
54
62
 
55
63
  it 'generates a valid random civic number' do
metadata CHANGED
@@ -1,69 +1,78 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luhn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Junström
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2015-09-19 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ostruct
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
13
26
  - !ruby/object:Gem::Dependency
14
27
  name: minitest
15
28
  requirement: !ruby/object:Gem::Requirement
16
29
  requirements:
17
- - - ~>
30
+ - - "~>"
18
31
  - !ruby/object:Gem::Version
19
32
  version: 2.6.0
20
33
  type: :development
21
34
  prerelease: false
22
35
  version_requirements: !ruby/object:Gem::Requirement
23
36
  requirements:
24
- - - ~>
37
+ - - "~>"
25
38
  - !ruby/object:Gem::Version
26
39
  version: 2.6.0
27
- description:
28
40
  email:
29
41
  - joel.junstrom@gmail.com
30
42
  executables: []
31
43
  extensions: []
32
44
  extra_rdoc_files: []
33
45
  files:
46
+ - LICENSE
47
+ - README.md
48
+ - lib/luhn.rb
34
49
  - lib/luhn/civic_number.rb
50
+ - lib/luhn/extensions.rb
35
51
  - lib/luhn/extensions/numeric.rb
36
52
  - lib/luhn/extensions/string.rb
37
- - lib/luhn/extensions.rb
38
53
  - lib/luhn/version.rb
39
- - lib/luhn.rb
40
54
  - spec/helper.rb
41
55
  - spec/spec_civic_number.rb
42
56
  - spec/spec_extensions.rb
43
57
  - spec/spec_luhn.rb
44
- - LICENSE
45
- - README.rdoc
46
58
  homepage: http://github.com/joeljunstrom/ruby_luhn
47
59
  licenses: []
48
60
  metadata: {}
49
- post_install_message:
50
61
  rdoc_options: []
51
62
  require_paths:
52
63
  - lib
53
64
  required_ruby_version: !ruby/object:Gem::Requirement
54
65
  requirements:
55
- - - '>='
66
+ - - ">="
56
67
  - !ruby/object:Gem::Version
57
68
  version: '0'
58
69
  required_rubygems_version: !ruby/object:Gem::Requirement
59
70
  requirements:
60
- - - '>='
71
+ - - ">="
61
72
  - !ruby/object:Gem::Version
62
73
  version: '0'
63
74
  requirements: []
64
- rubyforge_project:
65
- rubygems_version: 2.0.14
66
- signing_key:
75
+ rubygems_version: 3.7.2
67
76
  specification_version: 4
68
77
  summary: A small implementation of the Luhn algorithm.
69
78
  test_files: []
data/README.rdoc DELETED
@@ -1,44 +0,0 @@
1
- = Ruby class for handling basic Luhn number generation and verification
2
-
3
- Includes a class to handle Swedish civic numbers (Personnummer)
4
- That interface supports checking validity (length, valid date and satisfies luhn),
5
- returning the sex, the control digit and generating random civic numbers.
6
-
7
- == Install
8
-
9
- $ gem install luhn
10
-
11
- == Usage:
12
-
13
- === Basic Luhn
14
-
15
- Luhn.valid?('0465827879483596') # true
16
- Luhn.control_digit('046582787948359') # 6
17
- Luhn.generate(n) # returns a random number of n length that satisfies luhn
18
-
19
- '0465827879483596'.valid_luhn? # true
20
- 0465827879483596.valid_luhn? # true
21
-
22
- === Swedish civic numbers
23
-
24
- civic_number = Luhn::CivicNumber.new('19391030-3183')
25
- civic_number.valid? # true
26
- civic_number.sex # 'male'
27
- civic_number.male? # true
28
- civic_number.control_digit # 3
29
-
30
- '391030-3183'.civic_number? # true
31
- 3910303183.civic_number? # true
32
-
33
- == About the Luhn algorithm (from http://en.wikipedia.org/wiki/Luhn_formula)
34
-
35
- The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10"
36
- algorithm, is a simple checksum formula used to validate a variety of
37
- identification numbers, such as credit card numbers, IMEI numbers,
38
- National Provider Identifier numbers in US and Canadian Social Insurance Numbers.
39
- It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent
40
- No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.
41
-
42
- == Copyright
43
-
44
- Copyright (c) 2010 Joel Junström. See LICENSE for details.