random_token 1.0.1 → 1.0.2
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 +7 -0
- data/HISTORY.md +5 -0
- data/README.md +2 -0
- data/lib/random_token.rb +4 -3
- data/lib/random_token/version.rb +1 -1
- data/random_token.gemspec +3 -2
- data/test/format/test_pattern.rb +10 -10
- data/test/format/test_percent.rb +3 -3
- data/test/get/test_case.rb +4 -4
- data/test/get/test_friendly.rb +5 -5
- data/test/get/test_hash_seed.rb +4 -4
- data/test/get/test_length.rb +2 -2
- data/test/get/test_mask.rb +4 -4
- data/test/get/test_seed.rb +2 -2
- data/test/length/test_case.rb +4 -4
- data/test/length/test_friendly.rb +5 -5
- data/test/length/test_hash_seed.rb +4 -4
- data/test/length/test_length.rb +2 -2
- data/test/length/test_mask.rb +4 -4
- data/test/length/test_seed.rb +2 -2
- data/test/strf/test_pattern.rb +10 -10
- data/test/strf/test_percent.rb +3 -3
- data/test/test_all.rb +1 -1
- data/test/test_gen.rb +5 -5
- data/test/test_get.rb +1 -1
- data/test/test_strf.rb +1 -1
- metadata +27 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 738c05b70d8a4fc3c236ba237e90f131625ac1fc
|
4
|
+
data.tar.gz: 13d44098afbd728d3cc8390547d42f2dfa04ff82
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 846909049c446fa3eaad0af077a273b8a051d9ef4a51d706a656a4ee6a72beedef0102cecaa54ea1f2657f0f4b7d1839c9dc2b1502cfecbb11fb4acba701f372
|
7
|
+
data.tar.gz: d87701e82fcf89f3dca17c702c7dea13c08d1cedf8c83fba8e6035551f4c140fedf5b399bdae9f8e541e70dd1cad6580cbacff56306f341a583ed0b502bed1d0
|
data/HISTORY.md
ADDED
data/README.md
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
|
3
3
|
[][gem]
|
4
4
|
[][travis]
|
5
|
+
[][coveralls]
|
5
6
|
|
6
7
|
[gem]: https://rubygems.org/gems/random_token
|
7
8
|
[travis]: https://travis-ci.org/sibevin/random_token
|
9
|
+
[coveralls]:https://coveralls.io/r/sibevin/random_token?branch=cover-check
|
8
10
|
|
9
11
|
A simple way to generate a random token.
|
10
12
|
|
data/lib/random_token.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "random_token/random_token_error"
|
2
|
+
require "securerandom"
|
2
3
|
|
3
4
|
module RandomToken
|
4
5
|
|
@@ -104,7 +105,7 @@ module RandomToken
|
|
104
105
|
# Please see {RandomToken::RandomTokenError}
|
105
106
|
def gen(arg, options = {})
|
106
107
|
arg_dispatcher(arg, options) do |length, seeds|
|
107
|
-
(0...length).map{ seeds[
|
108
|
+
(0...length).map{ seeds[SecureRandom.random_number(seeds.length)] }.join
|
108
109
|
end
|
109
110
|
end
|
110
111
|
|
@@ -284,7 +285,7 @@ module RandomToken
|
|
284
285
|
if opt[:support_case]
|
285
286
|
case_opt = opt[:case] || opt[:default_case]
|
286
287
|
case case_opt
|
287
|
-
when :up, :u
|
288
|
+
when :up, :u
|
288
289
|
cased_seed = opt[:seed].map { |s| s.upcase }
|
289
290
|
when :down, :d, :lower, :l
|
290
291
|
cased_seed = opt[:seed].map { |s| s.downcase }
|
@@ -298,7 +299,7 @@ module RandomToken
|
|
298
299
|
else
|
299
300
|
raise RandomTokenError.new(:not_support_case, opt) if opt[:case] != nil
|
300
301
|
end
|
301
|
-
if opt[:support_friendly]
|
302
|
+
if opt[:support_friendly]
|
302
303
|
if opt[:friendly] || (opt[:friendly] == nil && opt[:mask])
|
303
304
|
masked_seed = opt[:seed].dup
|
304
305
|
mask = opt[:mask] || MASK
|
data/lib/random_token/version.rb
CHANGED
data/random_token.gemspec
CHANGED
@@ -16,10 +16,10 @@ Use "gen" method to create a random token with a given length.
|
|
16
16
|
|
17
17
|
Some options can help to modify the token format.
|
18
18
|
|
19
|
-
RandomToken.gen(20, :
|
19
|
+
RandomToken.gen(20, seed: :alphabet, friendly: true, case: :up)
|
20
20
|
# "YTHJHTXKSSXTPLARALRH"
|
21
21
|
|
22
|
-
Use string format to create a random in the particular format.
|
22
|
+
Use string format to create a random in the particular format.
|
23
23
|
|
24
24
|
RandomToken.gen("Give me a token %8?")
|
25
25
|
# "Give me a token 6HRQZp8O"
|
@@ -31,4 +31,5 @@ Please see "README" to get more details.
|
|
31
31
|
spec.files = `git ls-files`.split($/)
|
32
32
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
33
33
|
spec.require_paths = ["lib"]
|
34
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
34
35
|
end
|
data/test/format/test_pattern.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestPattern < Test
|
4
|
+
class TestPattern < Minitest::Test
|
5
5
|
def test_gen_should_create_a_random_with_alphabets
|
6
|
-
length = 10000
|
6
|
+
length = 10000
|
7
7
|
pattern = "===%#{length}A===%#{length}a===Aa==="
|
8
8
|
token = RandomToken.gen(pattern)
|
9
9
|
assert_match(/^===[A-Z]{#{length}}===[a-z]{#{length}}===Aa===$/, token)
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_gen_should_create_a_random_with_numbers
|
13
|
-
length = 10000
|
13
|
+
length = 10000
|
14
14
|
pattern = "===%#{length}n===n==="
|
15
15
|
token = RandomToken.gen(pattern)
|
16
16
|
assert_match(/^===[0-9]{#{length}}===n===$/, token)
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_gen_should_create_a_random_with_0_and_1
|
20
|
-
length = 10000
|
20
|
+
length = 10000
|
21
21
|
pattern = "===%#{length}b===b==="
|
22
22
|
token = RandomToken.gen(pattern)
|
23
23
|
assert_match(/^===[0-1]{#{length}}===b===$/, token)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_gen_should_create_a_random_with_octal_digits
|
27
|
-
length = 10000
|
27
|
+
length = 10000
|
28
28
|
pattern = "===%#{length}o===o==="
|
29
29
|
token = RandomToken.gen(pattern)
|
30
30
|
assert_match(/^===[0-8]{#{length}}===o===$/, token)
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_gen_should_create_a_random_with_hexadecimal_digits
|
34
|
-
length = 10000
|
34
|
+
length = 10000
|
35
35
|
pattern = "===%#{length}h===%#{length}H===hH==="
|
36
36
|
token = RandomToken.gen(pattern)
|
37
37
|
assert_match(/^===[0-9a-f]{#{length}}===[0-9A-F]{#{length}}===hH===$/, token)
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_gen_should_create_a_random_with_alphabets_and_numbers
|
41
|
-
length = 10000
|
41
|
+
length = 10000
|
42
42
|
pattern = "===%#{length}x===%#{length}X===xX==="
|
43
43
|
token = RandomToken.gen(pattern)
|
44
44
|
assert_match(/^===[0-9a-z]{#{length}}===[0-9A-Z]{#{length}}===xX===$/, token)
|
45
45
|
end
|
46
46
|
|
47
47
|
def test_gen_should_create_a_random_with_given_format
|
48
|
-
length = 10000
|
48
|
+
length = 10000
|
49
49
|
pattern = "===%#{length}?===?==="
|
50
50
|
token = RandomToken.gen(pattern)
|
51
51
|
assert_match(/^===.{#{length}}===\?===$/, token)
|
@@ -53,7 +53,7 @@ class TestPattern < Test::Unit::TestCase
|
|
53
53
|
|
54
54
|
def test_gen_should_raise_an_exception_if_the_given_pattern_is_invalid
|
55
55
|
pattern = "===%123==="
|
56
|
-
e =
|
56
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(pattern) }
|
57
57
|
assert(e.code == :invalid_strf_pattern)
|
58
58
|
end
|
59
59
|
end
|
data/test/format/test_percent.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestPercent < Test
|
4
|
+
class TestPercent < Minitest::Test
|
5
5
|
def test_gen_should_create_a_random_with_percent_signs
|
6
|
-
length = 10000
|
6
|
+
length = 10000
|
7
7
|
pattern = "===%#{length}%===%%==="
|
8
8
|
token = RandomToken.gen(pattern)
|
9
9
|
assert_match(/^===%{#{length}}===%===$/, token)
|
data/test/get/test_case.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'minitest/autorun'
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestCase < Test
|
4
|
+
class TestCase < Minitest::Test
|
5
5
|
def test_get_should_create_a_random_with_upper_case_alphabets_or_numbers
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.get(length, :case => :up)
|
@@ -20,13 +20,13 @@ class TestCase < Test::Unit::TestCase
|
|
20
20
|
|
21
21
|
def test_get_should_not_support_case_feature_when_creating_binray_random
|
22
22
|
length = 10000
|
23
|
-
e =
|
23
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.get(length, :seed => :b, :case => :up) }
|
24
24
|
assert(e.code == :not_support_case)
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_get_should_not_support_case_feature_when_creating_octal_random
|
28
28
|
length = 10000
|
29
|
-
e =
|
29
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.get(length, :seed => :o, :case => :up) }
|
30
30
|
assert(e.code == :not_support_case)
|
31
31
|
end
|
32
32
|
|
data/test/get/test_friendly.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'minitest/autorun'
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestFriendly < Test
|
4
|
+
class TestFriendly < Minitest::Test
|
5
5
|
def test_get_should_not_create_a_random_including_masked_alphabets_or_numbers
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.get(length, :friendly => true)
|
@@ -10,19 +10,19 @@ class TestFriendly < Test::Unit::TestCase
|
|
10
10
|
|
11
11
|
def test_get_should_not_support_friendly_feature_when_creating_binary_random
|
12
12
|
length = 10000
|
13
|
-
e =
|
13
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.get(length, :seed => :b, :friendly => true) }
|
14
14
|
assert(e.code == :not_support_friendly)
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_get_should_not_support_friendly_feature_when_creating_octal_random
|
18
18
|
length = 10000
|
19
|
-
e =
|
19
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.get(length, :seed => :o, :friendly => true) }
|
20
20
|
assert(e.code == :not_support_friendly)
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_get_should_not_support_friendly_feature_when_creating_hexadecimal_random
|
24
24
|
length = 10000
|
25
|
-
e =
|
25
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.get(length, :seed => :h, :friendly => true) }
|
26
26
|
assert(e.code == :not_support_friendly)
|
27
27
|
end
|
28
28
|
|
data/test/get/test_hash_seed.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'minitest/autorun'
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestHashSeed < Test
|
4
|
+
class TestHashSeed < Minitest::Test
|
5
5
|
def test_get_should_create_a_random_with_the_given_seeds
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.get(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 })
|
@@ -20,7 +20,7 @@ class TestHashSeed < Test::Unit::TestCase
|
|
20
20
|
|
21
21
|
def test_get_should_not_support_case_feature_when_given_the_hash_seed
|
22
22
|
length = 10000
|
23
|
-
e =
|
23
|
+
e = assert_raises(RandomToken::RandomTokenError) {
|
24
24
|
RandomToken.get(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 }, :case => :up)
|
25
25
|
}
|
26
26
|
assert(e.code == :not_support_case)
|
@@ -28,7 +28,7 @@ class TestHashSeed < Test::Unit::TestCase
|
|
28
28
|
|
29
29
|
def test_get_should_not_support_friendly_feature_when_given_the_hash_seed
|
30
30
|
length = 10000
|
31
|
-
e =
|
31
|
+
e = assert_raises(RandomToken::RandomTokenError) {
|
32
32
|
RandomToken.get(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 }, :friendly => true)
|
33
33
|
}
|
34
34
|
assert(e.code == :not_support_friendly)
|
data/test/get/test_length.rb
CHANGED
data/test/get/test_mask.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'minitest/autorun'
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestMask < Test
|
4
|
+
class TestMask < Minitest::Test
|
5
5
|
def test_get_should_use_given_mask
|
6
6
|
length = 10000
|
7
7
|
mask = ['a','b','c','d','e','0','1','2','3','4']
|
@@ -12,7 +12,7 @@ class TestMask < Test::Unit::TestCase
|
|
12
12
|
def test_get_should_raise_an_exception_if_mask_is_given_but_friendly_option_is_false
|
13
13
|
length = 10000
|
14
14
|
mask = ['a','b','c','d','e','0','1','2','3','4']
|
15
|
-
e =
|
15
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.get(length, :mask => mask,
|
16
16
|
:friendly => false) }
|
17
17
|
assert(e.code == :false_friendly_with_given_mask)
|
18
18
|
end
|
@@ -21,7 +21,7 @@ class TestMask < Test::Unit::TestCase
|
|
21
21
|
length = 10000
|
22
22
|
seed = ['a']
|
23
23
|
mask = ['a']
|
24
|
-
e =
|
24
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.get(length, :seed => seed,
|
25
25
|
:mask => mask) }
|
26
26
|
assert(e.code == :mask_remove_all_seeds)
|
27
27
|
end
|
data/test/get/test_seed.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'minitest/autorun'
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestSeed < Test
|
4
|
+
class TestSeed < Minitest::Test
|
5
5
|
def test_get_should_create_a_random_with_alphabets_only
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.get(length, :seed => :a)
|
data/test/length/test_case.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestCase < Test
|
4
|
+
class TestCase < Minitest::Test
|
5
5
|
def test_gen_should_create_a_random_with_upper_case_alphabets_or_numbers
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.gen(length, :case => :up)
|
@@ -28,13 +28,13 @@ class TestCase < Test::Unit::TestCase
|
|
28
28
|
|
29
29
|
def test_gen_should_not_support_case_feature_when_creating_binray_random
|
30
30
|
length = 10000
|
31
|
-
e =
|
31
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :b, :case => :up) }
|
32
32
|
assert(e.code == :not_support_case)
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_gen_should_not_support_case_feature_when_creating_octal_random
|
36
36
|
length = 10000
|
37
|
-
e =
|
37
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :o, :case => :up) }
|
38
38
|
assert(e.code == :not_support_case)
|
39
39
|
end
|
40
40
|
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestFriendly < Test
|
4
|
+
class TestFriendly < Minitest::Test
|
5
5
|
def test_gen_should_not_create_a_random_including_masked_alphabets_or_numbers
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.gen(length, :friendly => true)
|
@@ -12,19 +12,19 @@ class TestFriendly < Test::Unit::TestCase
|
|
12
12
|
|
13
13
|
def test_gen_should_not_support_friendly_feature_when_creating_binary_random
|
14
14
|
length = 10000
|
15
|
-
e =
|
15
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :b, :friendly => true) }
|
16
16
|
assert(e.code == :not_support_friendly)
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_gen_should_not_support_friendly_feature_when_creating_octal_random
|
20
20
|
length = 10000
|
21
|
-
e =
|
21
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :o, :friendly => true) }
|
22
22
|
assert(e.code == :not_support_friendly)
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_gen_should_not_support_friendly_feature_when_creating_hexadecimal_random
|
26
26
|
length = 10000
|
27
|
-
e =
|
27
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => :h, :friendly => true) }
|
28
28
|
assert(e.code == :not_support_friendly)
|
29
29
|
end
|
30
30
|
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestHashSeed < Test
|
4
|
+
class TestHashSeed < Minitest::Test
|
5
5
|
def test_gen_should_create_a_random_with_the_given_seeds
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.gen(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 })
|
@@ -20,7 +20,7 @@ class TestHashSeed < Test::Unit::TestCase
|
|
20
20
|
|
21
21
|
def test_gen_should_not_support_case_feature_when_given_the_hash_seed
|
22
22
|
length = 10000
|
23
|
-
e =
|
23
|
+
e = assert_raises(RandomToken::RandomTokenError) {
|
24
24
|
RandomToken.gen(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 }, :case => :up)
|
25
25
|
}
|
26
26
|
assert(e.code == :not_support_case)
|
@@ -28,7 +28,7 @@ class TestHashSeed < Test::Unit::TestCase
|
|
28
28
|
|
29
29
|
def test_gen_should_not_support_friendly_feature_when_given_the_hash_seed
|
30
30
|
length = 10000
|
31
|
-
e =
|
31
|
+
e = assert_raises(RandomToken::RandomTokenError) {
|
32
32
|
RandomToken.gen(length, :seed => { 'a' => 1, 'b' => 2, 'c' => 3 }, :friendly => true)
|
33
33
|
}
|
34
34
|
assert(e.code == :not_support_friendly)
|
data/test/length/test_length.rb
CHANGED
data/test/length/test_mask.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestMask < Test
|
4
|
+
class TestMask < Minitest::Test
|
5
5
|
def test_gen_should_use_given_mask
|
6
6
|
length = 10000
|
7
7
|
mask = ['a','b','c','d','e','0','1','2','3','4']
|
@@ -14,7 +14,7 @@ class TestMask < Test::Unit::TestCase
|
|
14
14
|
def test_gen_should_raise_an_exception_if_mask_is_given_but_friendly_option_is_false
|
15
15
|
length = 10000
|
16
16
|
mask = ['a','b','c','d','e','0','1','2','3','4']
|
17
|
-
e =
|
17
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :mask => mask,
|
18
18
|
:friendly => false) }
|
19
19
|
assert(e.code == :false_friendly_with_given_mask)
|
20
20
|
end
|
@@ -23,7 +23,7 @@ class TestMask < Test::Unit::TestCase
|
|
23
23
|
length = 10000
|
24
24
|
seed = ['a']
|
25
25
|
mask = ['a']
|
26
|
-
e =
|
26
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(length, :seed => seed,
|
27
27
|
:mask => mask) }
|
28
28
|
assert(e.code == :mask_remove_all_seeds)
|
29
29
|
end
|
data/test/length/test_seed.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestSeed < Test
|
4
|
+
class TestSeed < Minitest::Test
|
5
5
|
def test_gen_should_create_a_random_with_alphabets_only
|
6
6
|
length = 10000
|
7
7
|
token = RandomToken.gen(length, :seed => :a)
|
data/test/strf/test_pattern.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestPattern < Test
|
4
|
+
class TestPattern < Minitest::Test
|
5
5
|
def test_get_should_create_a_random_with_alphabets
|
6
|
-
length = 10000
|
6
|
+
length = 10000
|
7
7
|
pattern = "===%#{length}A===%#{length}a===Aa==="
|
8
8
|
token = RandomToken.strf(pattern)
|
9
9
|
assert_match(/^===[A-Z]{#{length}}===[a-z]{#{length}}===Aa===$/, token)
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_get_should_create_a_random_with_numbers
|
13
|
-
length = 10000
|
13
|
+
length = 10000
|
14
14
|
pattern = "===%#{length}n===n==="
|
15
15
|
token = RandomToken.strf(pattern)
|
16
16
|
assert_match(/^===[0-9]{#{length}}===n===$/, token)
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_get_should_create_a_random_with_0_and_1
|
20
|
-
length = 10000
|
20
|
+
length = 10000
|
21
21
|
pattern = "===%#{length}b===b==="
|
22
22
|
token = RandomToken.strf(pattern)
|
23
23
|
assert_match(/^===[0-1]{#{length}}===b===$/, token)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_get_should_create_a_random_with_octal_digits
|
27
|
-
length = 10000
|
27
|
+
length = 10000
|
28
28
|
pattern = "===%#{length}o===o==="
|
29
29
|
token = RandomToken.strf(pattern)
|
30
30
|
assert_match(/^===[0-8]{#{length}}===o===$/, token)
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_get_should_create_a_random_with_hexadecimal_digits
|
34
|
-
length = 10000
|
34
|
+
length = 10000
|
35
35
|
pattern = "===%#{length}h===%#{length}H===hH==="
|
36
36
|
token = RandomToken.strf(pattern)
|
37
37
|
assert_match(/^===[0-9a-f]{#{length}}===[0-9A-F]{#{length}}===hH===$/, token)
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_get_should_create_a_random_with_alphabets_and_numbers
|
41
|
-
length = 10000
|
41
|
+
length = 10000
|
42
42
|
pattern = "===%#{length}x===%#{length}X===xX==="
|
43
43
|
token = RandomToken.strf(pattern)
|
44
44
|
assert_match(/^===[0-9a-z]{#{length}}===[0-9A-Z]{#{length}}===xX===$/, token)
|
45
45
|
end
|
46
46
|
|
47
47
|
def test_get_should_create_a_random_with_given_format
|
48
|
-
length = 10000
|
48
|
+
length = 10000
|
49
49
|
pattern = "===%#{length}?===?==="
|
50
50
|
token = RandomToken.strf(pattern)
|
51
51
|
assert_match(/^===.{#{length}}===\?===$/, token)
|
@@ -53,7 +53,7 @@ class TestPattern < Test::Unit::TestCase
|
|
53
53
|
|
54
54
|
def test_get_should_raise_an_exception_if_the_given_pattern_is_invalid
|
55
55
|
pattern = "===%123==="
|
56
|
-
e =
|
56
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.strf(pattern) }
|
57
57
|
assert(e.code == :invalid_strf_pattern)
|
58
58
|
end
|
59
59
|
end
|
data/test/strf/test_percent.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require "
|
1
|
+
require "minitest/autorun"
|
2
2
|
require "random_token"
|
3
3
|
|
4
|
-
class TestPercent < Test
|
4
|
+
class TestPercent < Minitest::Test
|
5
5
|
def test_get_should_create_a_random_with_percent_signs
|
6
|
-
length = 10000
|
6
|
+
length = 10000
|
7
7
|
pattern = "===%#{length}%===%%==="
|
8
8
|
token = RandomToken.strf(pattern)
|
9
9
|
assert_match(/^===%{#{length}}===%===$/, token)
|
data/test/test_all.rb
CHANGED
data/test/test_gen.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'minitest/autorun'
|
2
2
|
|
3
3
|
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}")
|
4
4
|
require "test_helper"
|
@@ -12,20 +12,20 @@ require "length/test_hash_seed"
|
|
12
12
|
require "format/test_pattern"
|
13
13
|
require "format/test_percent"
|
14
14
|
|
15
|
-
class TestGen < Test
|
15
|
+
class TestGen < Minitest::Test
|
16
16
|
def test_gen_should_raise_an_exception_if_the_given_arg_is_invalid
|
17
|
-
e =
|
17
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(nil) }
|
18
18
|
assert(e.code == :invalid_gen_arg)
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_gen_should_raise_an_exception_if_the_given_options_are_duplicated
|
22
|
-
e =
|
22
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(8, :s => :a, :seed => :o) }
|
23
23
|
assert(e.code == :duplicated_option)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_gen_should_raise_an_exception_if_the_given_option_values_are_invalid
|
27
27
|
invalid_value = 'invalid_value'
|
28
|
-
e =
|
28
|
+
e = assert_raises(RandomToken::RandomTokenError) { RandomToken.gen(8, :c => invalid_value) }
|
29
29
|
assert(e.code == :invalid_option_value)
|
30
30
|
end
|
31
31
|
end
|
data/test/test_get.rb
CHANGED
data/test/test_strf.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sibevin Wang
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
11
|
+
date: 2014-10-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
14
27
|
description: A simple way to generate a random token.
|
15
28
|
email:
|
16
29
|
- sibevin@gmail.com
|
@@ -18,6 +31,7 @@ executables: []
|
|
18
31
|
extensions: []
|
19
32
|
extra_rdoc_files: []
|
20
33
|
files:
|
34
|
+
- HISTORY.md
|
21
35
|
- LICENSE.txt
|
22
36
|
- README.md
|
23
37
|
- lib/random_token.rb
|
@@ -48,32 +62,31 @@ files:
|
|
48
62
|
homepage: https://github.com/sibevin/random_token
|
49
63
|
licenses:
|
50
64
|
- MIT
|
65
|
+
metadata: {}
|
51
66
|
post_install_message:
|
52
67
|
rdoc_options: []
|
53
68
|
require_paths:
|
54
69
|
- lib
|
55
70
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
-
none: false
|
57
71
|
requirements:
|
58
|
-
- -
|
72
|
+
- - '>='
|
59
73
|
- !ruby/object:Gem::Version
|
60
74
|
version: '0'
|
61
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
-
none: false
|
63
76
|
requirements:
|
64
|
-
- -
|
77
|
+
- - '>='
|
65
78
|
- !ruby/object:Gem::Version
|
66
79
|
version: '0'
|
67
80
|
requirements: []
|
68
81
|
rubyforge_project:
|
69
|
-
rubygems_version:
|
82
|
+
rubygems_version: 2.2.1
|
70
83
|
signing_key:
|
71
|
-
specification_version:
|
72
|
-
summary:
|
84
|
+
specification_version: 4
|
85
|
+
summary: 'Use "gen" method to create a random token with a given length. RandomToken.gen(8)
|
73
86
|
# "iUEFxcG2" Some options can help to modify the token format. RandomToken.gen(20,
|
74
|
-
:
|
75
|
-
|
76
|
-
|
87
|
+
seed: :alphabet, friendly: true, case: :up) # "YTHJHTXKSSXTPLARALRH" Use string
|
88
|
+
format to create a random in the particular format. RandomToken.gen("Give me a
|
89
|
+
token %8?") # "Give me a token 6HRQZp8O" Please see "README" to get more details.'
|
77
90
|
test_files:
|
78
91
|
- test/format/test_pattern.rb
|
79
92
|
- test/format/test_percent.rb
|