public_uid 1.2.2 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7323c6067a97bf3479759130f27f3ab6a2629084
4
- data.tar.gz: 2a5aa34772776cc391d103d9f1ecb776899f0b0e
2
+ SHA256:
3
+ metadata.gz: c2d69f6a9e786d8688e3e6c0bcfa330c14fd7b127b5e3472d7ce5b82f2f03b87
4
+ data.tar.gz: 25d0ab2367c4ff2326b8853fe1835484206ce016d364d21c09a0220a98355519
5
5
  SHA512:
6
- metadata.gz: 51f04e7f8a1331cacee37033b013237a49cbd9946d9f564998705c4ba5be1a1a2e16960a09d9de298579178cc97bcb612d361301795aa80c21178974eb4ff4c9
7
- data.tar.gz: 935f54f79fcd7b681e8ad76f28a5b98d45810236ed2fc86862a6a38dad9e1d1b0a30c13a6815a460d27ffc45c3411ac55b839236c043dc67c5ead9e1eee9d49e
6
+ metadata.gz: f0b0837bb2a8104ac4cf986fe8bb595b0502ad4f53cdf7c10e9f3f99cf0e4a9f338de1eca825cc29038a14f85f0033a348192094f402b042568b5363af1ce85b
7
+ data.tar.gz: 9ab628f84f3883aa7f6c2063c74dbfd8b8897a15b6613ccdcb020af3456f45dd3b5c56f487d037e981cae3d5ab2f438c94fc1d1021be9c453e573285de4a8541
@@ -5,4 +5,5 @@ rvm:
5
5
  - 2.1.1
6
6
  - 2.2.2
7
7
  - 2.3.0
8
- - ruby-head
8
+ - 2.4.0
9
+ - 2.5.1
data/README.md CHANGED
@@ -141,6 +141,36 @@ generator wont work if your public uniq ID column is a String (as the
141
141
  gem would try to set Integer on a String). If you really want a number
142
142
  like string you can specify number range for Range String Generator
143
143
 
144
+ If you want to generate random Integer using SecureRandom ruby library you can use built-in number secure generator:
145
+
146
+ ```ruby
147
+ class User < ActiveRecord::Base
148
+ generate_public_uid generator: PublicUid::Generators::NumberSecureRandom.new
149
+ end
150
+ ```
151
+
152
+ ```irb
153
+ u = User.new
154
+ u.public_uid #=> nil
155
+ u.save! #=> true
156
+ u.public_uid #=> 4567123
157
+ ```
158
+
159
+ If you want to generate random Hexadecimal String using SecureRandom ruby library you can use built-in hexadecimal string secure generator:
160
+
161
+ ```ruby
162
+ class User < ActiveRecord::Base
163
+ generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new
164
+ end
165
+ ```
166
+
167
+ ```irb
168
+ u = User.new
169
+ u.public_uid #=> nil
170
+ u.save! #=> true
171
+ u.public_uid #=> 0b30ffbc7de3b362
172
+ ```
173
+
144
174
  ### Customizing generated string
145
175
 
146
176
  ```ruby
@@ -150,6 +180,14 @@ class User < ActiveRecord::Base
150
180
  end
151
181
  ```
152
182
 
183
+ or in case you are using SecureRandom ruby library:
184
+
185
+ ```ruby
186
+ class User < ActiveRecord::Base
187
+ generate_public_uid generator: PublicUid::Generators::HexStringSecureRandom.new(4) #4 is length of hexadecimal string. If this argument is not set, length of hexadecimal string will be 8 characters.
188
+ end
189
+ ```
190
+
153
191
  ```irb
154
192
  u = User.new
155
193
  u.public_uid #=> nil
@@ -177,6 +215,14 @@ class User < ActiveRecord::Base
177
215
  end
178
216
  ```
179
217
 
218
+ or in case you are using SecureRandom ruby library:
219
+ ```ruby
220
+ class User < ActiveRecord::Base
221
+ UID_RANGE = 1_000..4_000
222
+ generate_public_uid generator: PublicUid::Generators::NumberSecureRandom.new(UID_RANGE)
223
+ end
224
+ ```
225
+
180
226
  ```irb
181
227
  u = User.new
182
228
  u.public_uid #=> nil
@@ -4,7 +4,8 @@ require "public_uid/set_public_uid"
4
4
  require "public_uid/model"
5
5
  require "public_uid/generators/number_random"
6
6
  require "public_uid/generators/range_string"
7
-
7
+ require "public_uid/generators/number_secure_random"
8
+ require "public_uid/generators/hex_string_secure_random"
8
9
  require 'public_uid/tasks' if defined?(Rails)
9
10
 
10
11
  require 'orm/active_record' if defined?(ActiveRecord::Base)
@@ -0,0 +1,20 @@
1
+ require 'securerandom'
2
+
3
+ module PublicUid
4
+ module Generators
5
+ class HexStringSecureRandom
6
+ def initialize(length=8)
7
+ @length = length
8
+ end
9
+
10
+ def generate
11
+ if @length.odd?
12
+ result = SecureRandom.hex( (@length+1)/2 ) #because in "SecureRandom.hex(@length)" @length means length in bytes = 2 hexadecimal characters
13
+ return result[0...-1]
14
+ else
15
+ SecureRandom.hex(@length/2)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ require 'securerandom'
2
+
3
+ module PublicUid
4
+ module Generators
5
+ class NumberSecureRandom
6
+ def initialize(scale = 1_000_000..9_999_999)
7
+ @scale = scale
8
+ end
9
+
10
+ def generate()
11
+ generated_number = SecureRandom.random_number( (@scale.max - @scale.min) ) #because SecureRandom.random_number can have only one argument = max value.
12
+ return (generated_number + @scale.min)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -34,7 +34,7 @@ module PublicUid
34
34
  end
35
35
 
36
36
  def public_uid_generator
37
- @public_uid_generator || Generators::RangeString.new
37
+ @public_uid_generator || Generators::HexStringSecureRandom.new
38
38
  end
39
39
 
40
40
  private
@@ -1,3 +1,3 @@
1
1
  module PublicUid
2
- VERSION = "1.2.2"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ describe 'HexStringSecureRandom' do
4
+
5
+ describe "#generate" do
6
+ subject{ instance.generate }
7
+
8
+ context 'by default' do
9
+ let(:instance){ PublicUid::Generators::HexStringSecureRandom.new }
10
+ it 'generates 8 chars hexa string' do
11
+ subject.length.must_equal 8
12
+ subject.must_be_kind_of String
13
+ end
14
+
15
+ it 'generates hexadecimal chars' do
16
+ subject.must_match(/^[a-f0-9]*$/)
17
+ end
18
+ end
19
+
20
+ context 'when 10 hexa chars' do
21
+ let(:instance){ PublicUid::Generators::HexStringSecureRandom.new(10) }
22
+ it 'generates 10 chars string' do
23
+ subject.length.must_equal 10
24
+ end
25
+ end
26
+
27
+ context 'when 11 hexa chars' do
28
+ let(:instance){ PublicUid::Generators::HexStringSecureRandom.new(11) }
29
+ it 'generates 11 chars string' do
30
+ subject.length.must_equal 11
31
+ end
32
+ end
33
+ end
34
+ end
@@ -20,7 +20,7 @@ describe 'NumberRandom' do
20
20
  subject.must_be_kind_of Integer
21
21
  end
22
22
 
23
- it 'generates string containing chars x,y,z' do
23
+ it 'generates integer has to be 20 or 21' do
24
24
  [20, 21].must_include subject
25
25
  end
26
26
  end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ describe 'NumberSecureRandom' do
4
+
5
+ describe "#generate" do
6
+ subject{ instance.generate }
7
+
8
+ context 'by default' do
9
+ let(:instance){ PublicUid::Generators::NumberSecureRandom.new }
10
+ it 'generates 7 digits' do
11
+ subject.to_i.to_s.length.must_equal 7
12
+ subject.must_be_kind_of Integer
13
+ end
14
+ end
15
+
16
+ context 'when specifying number between 20 - 21' do
17
+ let(:instance){ PublicUid::Generators::NumberSecureRandom.new(20..21) }
18
+ it 'generates 2 digits' do
19
+ subject.to_i.to_s.length.must_equal 2
20
+ subject.must_be_kind_of Integer
21
+ end
22
+
23
+ it 'generates integer has to be 20 or 21' do
24
+ [20, 21].must_include subject
25
+ end
26
+ end
27
+
28
+ context 'when specifying number between 1000 - 9999' do
29
+ let(:instance){ PublicUid::Generators::NumberSecureRandom.new(1000..9999) }
30
+ it 'generates 4 digits' do
31
+ subject.to_i.to_s.length.must_equal 4
32
+ subject.must_be_kind_of Integer
33
+ end
34
+
35
+ it 'generates integer has to be between 1000 and 9999' do
36
+ (1000..9999).must_include subject
37
+ end
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: public_uid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Valent
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-04 00:00:00.000000000 Z
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: orm_adapter
@@ -123,7 +123,9 @@ files:
123
123
  - Rakefile
124
124
  - lib/orm/active_record.rb
125
125
  - lib/public_uid.rb
126
+ - lib/public_uid/generators/hex_string_secure_random.rb
126
127
  - lib/public_uid/generators/number_random.rb
128
+ - lib/public_uid/generators/number_secure_random.rb
127
129
  - lib/public_uid/generators/range_string.rb
128
130
  - lib/public_uid/model.rb
129
131
  - lib/public_uid/set_public_uid.rb
@@ -131,7 +133,9 @@ files:
131
133
  - lib/public_uid/tasks/generate.rake
132
134
  - lib/public_uid/version.rb
133
135
  - public_uid.gemspec
136
+ - test/lib/generators/hex_string_secure_random_test.rb
134
137
  - test/lib/generators/number_random_test.rb
138
+ - test/lib/generators/number_secure_random_test.rb
135
139
  - test/lib/generators/range_string_test.rb
136
140
  - test/lib/set_public_uid_test.rb
137
141
  - test/lib/tasks/generate_test.rb
@@ -162,12 +166,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
166
  version: '0'
163
167
  requirements: []
164
168
  rubyforge_project:
165
- rubygems_version: 2.6.11
169
+ rubygems_version: 2.7.7
166
170
  signing_key:
167
171
  specification_version: 4
168
172
  summary: Automatic generates public UID column
169
173
  test_files:
174
+ - test/lib/generators/hex_string_secure_random_test.rb
170
175
  - test/lib/generators/number_random_test.rb
176
+ - test/lib/generators/number_secure_random_test.rb
171
177
  - test/lib/generators/range_string_test.rb
172
178
  - test/lib/set_public_uid_test.rb
173
179
  - test/lib/tasks/generate_test.rb