luhn_util 0.1.0 → 0.3.0
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +19 -5
- data/lib/luhn/version.rb +1 -1
- data/lib/luhn.rb +14 -8
- data/luhn_util.gemspec +2 -4
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de11d9f613bff6a60e8a0171b12d6ac314896effa2b2ff9b71bb4ebb3fe4acfa
|
4
|
+
data.tar.gz: 791ac942326d80198704a456fca3942d8d967530d283e01691bf4203f8a28bea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3016fc85a49a12b87197c1935b6569390331b151c9832831adc8af817f299d6373583efa0721ab2f11ba82b2e31f0c3266384fee9e7648cb87e3cc1afa988bac
|
7
|
+
data.tar.gz: 494ff51979775268613b1cf183e2b9afff08e64a02fa2f2d297351f07fc904fcb793ae3b556446e08495d39c8b1c18a8809b318280fbf639261c71646e69db7e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# luhn_util
|
2
2
|
|
3
|
-
|
3
|
+
A simple Ruby utility to validate and generate Luhn-compliant numbers.
|
4
4
|
|
5
|
-
`luhn_util` is
|
5
|
+
`luhn_util` is able to:
|
6
6
|
- Validate an input according to the Luhn algorithm
|
7
|
-
-
|
7
|
+
- Calculate the check digit for a given numeric input
|
8
|
+
- Generate random Luhn number based on any valid length and prefix
|
8
9
|
|
9
10
|
The algorithm followed is based on [Luhn Algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm).
|
10
11
|
|
@@ -32,6 +33,11 @@ To check if input is valid:
|
|
32
33
|
$ Luhn.valid?('5185443765446305')
|
33
34
|
$ true
|
34
35
|
```
|
36
|
+
To calculate the check digit:
|
37
|
+
```shell
|
38
|
+
$ Luhn.check_digit('1234567890')
|
39
|
+
$ 3
|
40
|
+
```
|
35
41
|
The `generate` method returns a 16-character number by default:
|
36
42
|
```shell
|
37
43
|
$ Luhn.generate
|
@@ -42,6 +48,14 @@ Length may also be specified:
|
|
42
48
|
$ Luhn.generate(length: 8)
|
43
49
|
$ 29974938
|
44
50
|
```
|
51
|
+
Prefix may also be supplied:
|
52
|
+
```shell
|
53
|
+
$ Luhn.generate(prefix: '88')
|
54
|
+
$ 8821951428417137
|
55
|
+
|
56
|
+
$ Luhn.generate(length: 8, prefix: '88')
|
57
|
+
$ 88405022
|
58
|
+
```
|
45
59
|
|
46
60
|
## Development
|
47
61
|
|
@@ -51,7 +65,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
51
65
|
|
52
66
|
## Contributing
|
53
67
|
|
54
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/katpadi/luhn_util. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/luhn_util/blob/main/CODE_OF_CONDUCT.md).
|
55
69
|
|
56
70
|
## License
|
57
71
|
|
@@ -59,4 +73,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
59
73
|
|
60
74
|
## Code of Conduct
|
61
75
|
|
62
|
-
Everyone interacting in the luhn_util project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
76
|
+
Everyone interacting in the luhn_util project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/katpadi/luhn_util/blob/main/CODE_OF_CONDUCT.md).
|
data/lib/luhn/version.rb
CHANGED
data/lib/luhn.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Luhn
|
4
|
+
MIN_LENGTH = 2
|
4
5
|
class << self
|
5
6
|
def valid?(digits)
|
6
7
|
return false unless numeric_string?(digits)
|
@@ -10,12 +11,19 @@ module Luhn
|
|
10
11
|
sum = checksum + non_checksum
|
11
12
|
sum % 10 == 0
|
12
13
|
end
|
14
|
+
def check_digit(partial_num)
|
15
|
+
(10 - (non_checksum_sum(partial_num) % 10)) % 10
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate(length: 16, prefix: nil)
|
19
|
+
raise ArgumentError, 'length must be more than minimum' unless length.to_i > MIN_LENGTH
|
13
20
|
|
14
|
-
|
15
|
-
raise ArgumentError, 'length must be valid' if length.nil? || length < 2
|
21
|
+
raise ArgumentError, 'prefix length invalid' if prefix && (prefix.to_s.length + MIN_LENGTH >= length)
|
16
22
|
|
17
|
-
|
18
|
-
|
23
|
+
raise ArgumentError, 'prefix must be numeric' if prefix && !numeric_string?(prefix)
|
24
|
+
|
25
|
+
partial_num = (prefix&.empty? ? '' : prefix).to_s
|
26
|
+
(length - 1 - partial_num.length).times do
|
19
27
|
partial_num += rand(10).to_s
|
20
28
|
end
|
21
29
|
mod = 10 - (non_checksum_sum(partial_num) % 10)
|
@@ -25,16 +33,14 @@ module Luhn
|
|
25
33
|
|
26
34
|
private
|
27
35
|
|
28
|
-
def numeric_string?(str)
|
29
|
-
!!(str =~ /\A[-+]?[0-9]+\z/)
|
30
|
-
end
|
36
|
+
def numeric_string?(str) = !!(str =~ /\A[-+]?[0-9]+\z/)
|
31
37
|
|
32
38
|
def non_checksum_sum(partial_num)
|
33
39
|
sum = 0
|
34
40
|
partial_num.chars.map(&:to_i).reverse.each_with_index do |num, i|
|
35
41
|
if i.even?
|
36
42
|
num *= 2
|
37
|
-
num
|
43
|
+
num -= 9 if num > 9
|
38
44
|
end
|
39
45
|
sum += num
|
40
46
|
end
|
data/luhn_util.gemspec
CHANGED
@@ -8,15 +8,13 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Kat Padilla"]
|
9
9
|
spec.email = ["hello@katpadi.ph"]
|
10
10
|
spec.summary = "A simple Luhn algorithm implementation using Ruby."
|
11
|
-
spec.description = "A simple
|
11
|
+
spec.description = "A simple Ruby utility to validate and generate Luhn-compliant numbers"
|
12
12
|
spec.homepage = 'https://rubygems.org/gems/luhn_util'
|
13
13
|
spec.license = "MIT"
|
14
14
|
spec.required_ruby_version = ">= 3.0.0"
|
15
15
|
|
16
|
-
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
17
|
-
|
18
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
-
|
17
|
+
spec.metadata["source_code_uri"] = 'https://github.com/katpadi/luhn_util'
|
20
18
|
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
21
19
|
|
22
20
|
# Specify which files should be added to the gem when it is released.
|
metadata
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: luhn_util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kat Padilla
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-03-
|
11
|
+
date: 2025-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: A simple
|
14
|
-
Luhn numbers
|
13
|
+
description: A simple Ruby utility to validate and generate Luhn-compliant numbers
|
15
14
|
email:
|
16
15
|
- hello@katpadi.ph
|
17
16
|
executables: []
|
@@ -38,6 +37,7 @@ licenses:
|
|
38
37
|
- MIT
|
39
38
|
metadata:
|
40
39
|
homepage_uri: https://rubygems.org/gems/luhn_util
|
40
|
+
source_code_uri: https://github.com/katpadi/luhn_util
|
41
41
|
post_install_message:
|
42
42
|
rdoc_options: []
|
43
43
|
require_paths:
|