cdigits 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb6c4b993ed0c2d9ae35c5900b85b12f663c65b0fadea88c572734e79b925351
4
- data.tar.gz: ba3cceb4188ff489c3b9f7aac535a90a80eaaad47582b8e52dbfe005c244db60
3
+ metadata.gz: ee3bdcddd0d7d2725e9744ae11f557404e1555cbbc5487940ca45854efca3a76
4
+ data.tar.gz: 87ca68f822cc714e4e2f3b38ffc94c54adf073fb11146251e1e278c59b0bd9e1
5
5
  SHA512:
6
- metadata.gz: 2c01c99b794d798ada867b0dd07b6dcf2f45b7477e36c61b2640e15cf6f448f1ca08aa3a0f482837899cd41a5a983cbde9342de1037962de9eb583643991c35d
7
- data.tar.gz: aa6b66c35b3af0377d45cf74b99c30f40c62b5844017112de1dd576cf2cfee69539fa735719085b198023559b1107fbd655bb2870c7ee24071112f6fc3ce7cad
6
+ metadata.gz: 6d4fe3e3584577f942346eafcab6de214045feff760cc507cefab7eab2f39c2a2e9dc91d8ece9602d47ec6519fa143c987d07002fa7dda3b4dd0af1369c3bad4
7
+ data.tar.gz: 5280b3e582ae3376378b8d6888defed0ed03d2dc35354c331614425d486561c752ddce6331b757d13fdd80e7a6a844970e278a53ce9fa11f0d7623348542f730
@@ -8,6 +8,10 @@
8
8
  # where the inspected file is and continue its way up to the root directory.
9
9
  #
10
10
  # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
11
+ AllCops:
12
+ Exclude:
13
+ - 'cdigits.gemspec'
14
+ - 'vendor/**/*'
11
15
  Layout/LineLength:
12
16
  Max: 120
13
17
  Style/Documentation:
data/README.md CHANGED
@@ -16,8 +16,8 @@ Also, you can generate code in any format using placeholder.
16
16
  example:
17
17
 
18
18
  ```rb
19
- Cdigits::Luhn.number 'CA##-####-####-###?' # => "CA66-6567-2324-6526"
20
- Cdigits::Luhn.number '2020-01##-####-###?' # => "2020-0160-1171-1643"
19
+ Cdigits::Luhn.number 'CA##-####-####-###?' # => "CA63-6485-2316-2675"
20
+ Cdigits::Luhn.number '2020-01##-####-###?' # => "2020-0173-1935-2569"
21
21
  ```
22
22
 
23
23
  ## Installation
@@ -43,21 +43,21 @@ Or install it yourself as:
43
43
  ```rb
44
44
  placeholder = 'CA##-####-####-###?'
45
45
  Cdigits::Luhn.number
46
- # => "6907562414"
46
+ # => "1638302982"
47
47
  Cdigits::Luhn.number placeholder
48
- # => "CA98-4890-6337-4381"
48
+ # => "CA16-5930-2060-1262"
49
49
  Cdigits::Luhn.hex
50
- # => "2c14a42508"
50
+ # => "2b69307483"
51
51
  Cdigits::Luhn.hex placeholder
52
- # => "CA27-675e-7136-5fa1"
52
+ # => "CA1e-fe6c-1c04-9a28"
53
53
  Cdigits::Luhn.alphanumeric
54
- # => "yrkxeh4eie"
54
+ # => "th5h7fewjl"
55
55
  Cdigits::Luhn.alphanumeric placeholder
56
- # => "CA59-bdxv-wjei-gdc9"
56
+ # => "CAwf-mfkx-ncsv-odl2"
57
57
  Cdigits::Luhn.easy
58
- # => "16TPF8RETL"
58
+ # => "NWK036TN1R"
59
59
  Cdigits::Luhn.easy placeholder
60
- # => "CABW-LF40-G11S-TL3U"
60
+ # => "CAPY-SHHN-U55U-9PG7"
61
61
  ```
62
62
 
63
63
  #### Special chars in placeholder
@@ -9,7 +9,12 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ['kengo@kengos.jp']
10
10
 
11
11
  spec.summary = 'Generate code with Luhn mod N algorithm using placeholder.'
12
- spec.description = "`Cdigits::Luhn.number('CA##-####-####-###?')` #=> 'CA98-4890-6337-4381'\n`Cdigits::Luhn.easy` #=> '16TPF8RETL'"
12
+ spec.description = [
13
+ 'Generate code with Luhn mod N algorithm using placeholder.',
14
+ 'e.g)',
15
+ "Cdigits::Luhn.number('CA##-####-####-###?') #=> 'CA63-6485-2316-2675'",
16
+ "Cdigits::Luhn.easy #=> '16TPF8RETL'"
17
+ ].join("\n")
13
18
  spec.homepage = 'https://github.com/kengos/cdigits'
14
19
  spec.license = 'MIT'
15
20
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cdigits
4
+ module Luhn
5
+ class RandomTable
6
+ attr_accessor :previsous
7
+
8
+ def initialize(modulus:)
9
+ @modulus = modulus
10
+ @previsous = nil
11
+ end
12
+
13
+ # pick from number table exclude 0
14
+ # @return [Integer]
15
+ def pick_non_zero
16
+ pick(exclude: [0])
17
+ end
18
+
19
+ # pick from number table
20
+ # @param exclude [Array<Intger>] exclude numbers
21
+ # @return [Integer]
22
+ def pick(exclude: [])
23
+ table(exclude).sample
24
+ end
25
+
26
+ private
27
+
28
+ def numbers
29
+ @numbers ||= (0..max).to_a.freeze
30
+ end
31
+
32
+ def max
33
+ @modulus - 1
34
+ end
35
+
36
+ # @return [Array<Integer>]
37
+ def table(exclude)
38
+ exclude << next_numbers_to_avoid[@previsous]
39
+ table = numbers - exclude.compact
40
+ table.empty? ? numbers : table
41
+ end
42
+
43
+ def next_numbers_to_avoid
44
+ @next_numbers_to_avoid ||= build_next_numbers_to_avoid
45
+ end
46
+
47
+ def build_next_numbers_to_avoid
48
+ defaults = { 0 => max, max => 0 }
49
+ return defaults unless occure_twin_error?
50
+
51
+ defaults.merge(twin_error_values)
52
+ end
53
+
54
+ # @note
55
+ # modulus = 10
56
+ # twin | doubled value
57
+ # ---- | -------------
58
+ # 00 | 0
59
+ # 11 | 3
60
+ # 22 | 6
61
+ # 33 | 9
62
+ # 44 | 12
63
+ # 55 | 6
64
+ # 66 | 9
65
+ # 77 | 12
66
+ # 88 | 15
67
+ # 99 | 18
68
+ # @example
69
+ # # when @modulus = 10
70
+ # twin_error_values # => { 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7 }
71
+ # @return [Hash]
72
+ def twin_error_values
73
+ size = (max / 3).to_i
74
+ start = (@modulus / 2.0).ceil - size
75
+ stop = start + size * 2 - 1
76
+ values = (start..stop).to_a
77
+ values.zip(values).to_h
78
+ end
79
+
80
+ def occure_twin_error?
81
+ (max % 3).zero?
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'securerandom'
3
+ require 'cdigits/luhn/random_table'
4
4
 
5
5
  module Cdigits
6
6
  module Luhn
@@ -34,19 +34,21 @@ module Cdigits
34
34
  # Append random non-zero number to digits array
35
35
  # @return [Integer] appended value
36
36
  def append_non_zero_number
37
- append random_number(@modulus - 1) + 1
37
+ append table.pick_non_zero
38
38
  end
39
39
 
40
40
  # Append random number to digits array
41
41
  # @return [Integer] appended value
42
42
  def append_number
43
- append random_number(@modulus)
43
+ append table.pick
44
44
  end
45
45
 
46
46
  # Append passed value to digits array
47
- # @param [Integer] value
47
+ # @param value [Integer]
48
+ # @param freeze [Boolean]
48
49
  # @return [Integer] appended value
49
50
  def append(value)
51
+ table.previsous = value
50
52
  @digits << value
51
53
  value
52
54
  end
@@ -62,8 +64,8 @@ module Cdigits
62
64
 
63
65
  private
64
66
 
65
- def random_number(num)
66
- ::SecureRandom.random_number(num)
67
+ def table
68
+ @table ||= ::Cdigits::Luhn::RandomTable.new(modulus: @modulus)
67
69
  end
68
70
 
69
71
  def calculate_check_digit(value, odd)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cdigits
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdigits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kengos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-09 00:00:00.000000000 Z
11
+ date: 2020-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -67,8 +67,10 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: |-
70
- `Cdigits::Luhn.number('CA##-####-####-###?')` #=> 'CA98-4890-6337-4381'
71
- `Cdigits::Luhn.easy` #=> '16TPF8RETL'
70
+ Generate code with Luhn mod N algorithm using placeholder.
71
+ e.g)
72
+ Cdigits::Luhn.number('CA##-####-####-###?') #=> 'CA63-6485-2316-2675'
73
+ Cdigits::Luhn.easy #=> '16TPF8RETL'
72
74
  email:
73
75
  - kengo@kengos.jp
74
76
  executables: []
@@ -90,6 +92,7 @@ files:
90
92
  - lib/cdigits.rb
91
93
  - lib/cdigits/luhn.rb
92
94
  - lib/cdigits/luhn/placeholder.rb
95
+ - lib/cdigits/luhn/random_table.rb
93
96
  - lib/cdigits/luhn/store.rb
94
97
  - lib/cdigits/version.rb
95
98
  homepage: https://github.com/kengos/cdigits