luhn_util 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 573af37dfcd3390628a8a9d191a156231ab35c56158751c21545ed2db1ad9209
4
- data.tar.gz: 9d59acbae76b661d8cd941b1c41cd8bfdfd66f5bebd9a04acc19f76e63795abf
3
+ metadata.gz: 373c083840bb7a732849f0d5f088aba9b248d2bad8d1ba1408975fadd876a33c
4
+ data.tar.gz: c3e1e098dc558a7ea542fcbf76a461a4bc4cdc9b697bd586b123996a1eacf4ab
5
5
  SHA512:
6
- metadata.gz: 393e76ca8b691b48e4f5b049a9059122c953773ee59ddea184caa58bf7916dea997d07d21ec1500a35c0f56db994d2e9ff437eb11f530893d5e7e6ac6b02dee9
7
- data.tar.gz: 8148996383ffe0c7cb82280b53bec88e2a6e0ffdfe7c0472133f716a0dfb3400bd6ee24d34ca43b8f74e0878ddc021e879ace0ec102530f95cfbf1d0cc9bc646
6
+ metadata.gz: ffb5039ba8e0f0615d872a824ee8a889ac87754fb4a9d6a2ef8a7aebd79bc0eb38af1aa2b4ab225cf23530304e23c475e13f608f267e7ea07b127e9d42b50904
7
+ data.tar.gz: f92a243d49cdb52bd538afffeba1688451f1c57968278ea4cd0623ae4af46700128d57f8f0cb3b86c50a815b88d0757bb4610edf60502c64c5f554feb349a8ca
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luhn_util (0.1.0)
4
+ luhn_util (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # luhn_util
2
2
 
3
- This is a simple gem to generate and validate using Luhn algorithm.
3
+ A simple Ruby utility to validate and generate Luhn-compliant numbers.
4
4
 
5
- `luhn_util` is a gem that is able to:
5
+ `luhn_util` is able to:
6
6
  - Validate an input according to the Luhn algorithm
7
- - Generate random Luhn number based on any valid length
7
+ - Generate random Luhn number based on any valid length and prefix
8
8
 
9
9
  The algorithm followed is based on [Luhn Algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm).
10
10
 
@@ -42,6 +42,14 @@ Length may also be specified:
42
42
  $ Luhn.generate(length: 8)
43
43
  $ 29974938
44
44
  ```
45
+ Prefix may also be supplied:
46
+ ```shell
47
+ $ Luhn.generate(prefix: '88')
48
+ $ 8821951428417137
49
+
50
+ $ Luhn.generate(length: 8, prefix: '88')
51
+ $ 88405022
52
+ ```
45
53
 
46
54
  ## Development
47
55
 
@@ -51,7 +59,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
51
59
 
52
60
  ## Contributing
53
61
 
54
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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).
62
+ 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
63
 
56
64
  ## License
57
65
 
@@ -59,4 +67,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
59
67
 
60
68
  ## Code of Conduct
61
69
 
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/[USERNAME]/luhn_util/blob/main/CODE_OF_CONDUCT.md).
70
+ 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Luhn
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
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)
@@ -11,11 +12,15 @@ module Luhn
11
12
  sum % 10 == 0
12
13
  end
13
14
 
14
- def generate(length = 16)
15
- raise ArgumentError, 'length must be valid' if length.nil? || length < 2
15
+ def generate(length: 16, prefix: nil)
16
+ raise ArgumentError, 'length must be more than minimum' unless length.to_i > MIN_LENGTH
16
17
 
17
- partial_num = ''
18
- (length - 1).times do
18
+ raise ArgumentError, 'prefix length invalid' if prefix && (prefix.to_s.length + MIN_LENGTH >= length)
19
+
20
+ raise ArgumentError, 'prefix must be numeric' if prefix && !numeric_string?(prefix)
21
+
22
+ partial_num = (prefix&.empty? ? '' : prefix).to_s
23
+ (length - 1 - partial_num.length).times do
19
24
  partial_num += rand(10).to_s
20
25
  end
21
26
  mod = 10 - (non_checksum_sum(partial_num) % 10)
@@ -25,9 +30,7 @@ module Luhn
25
30
 
26
31
  private
27
32
 
28
- def numeric_string?(str)
29
- !!(str =~ /\A[-+]?[0-9]+\z/)
30
- end
33
+ def numeric_string?(str) = !!(str =~ /\A[-+]?[0-9]+\z/)
31
34
 
32
35
  def non_checksum_sum(partial_num)
33
36
  sum = 0
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 Luhn algorithm implementation using Ruby to validate and generate Luhn numbers"
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
- # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luhn_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kat Padilla
@@ -10,8 +10,7 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2025-03-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A simple Luhn algorithm implementation using Ruby to validate and generate
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: