strandom 1.0.1 → 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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -5
  3. data/bin/strandom +30 -18
  4. data/lib/strandom.rb +49 -35
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c84788b91e8f3983ab0d906b88f15a083ab3291c
4
- data.tar.gz: 8874fb694b62d40ed4b6304ff3c7687f88369d84
3
+ metadata.gz: 50efedb190938b9aff3b35885fdfd290bfab92f2
4
+ data.tar.gz: b9f110f65fabf9ded8216883a94197da1facfb9e
5
5
  SHA512:
6
- metadata.gz: 22eaf304e191faadf282194123b7bacc96a96c839012b1337c4019375fdba93fedd2c88ae539045e43f5157fbfaba459fbf67916a9043856347dae55662956bf
7
- data.tar.gz: d5f3d11a710e959aa3c20cbec08cb9d56470c044165be2e6431286d5ec890b808acb9fd8eaee02ed20a4f24e5b0f3ae8e028f051a28d857095f17284a30e0735
6
+ metadata.gz: 89e9026f4b518b55ac04d5a43647aac1d8efcd8a79aa91b423ed8cdefddcc8203e31aa63d5258cb8f3f97a7f5d7a5d535d6de5da42b32556dc223bfba2dd9bdb
7
+ data.tar.gz: 9d33f38ada006e25bca0e9f0f7da9fa5a7c4c7c958b059821d183cad0a02f77f48088d02eea5f844d0742c5681fe001c62bec1ee96cd598670806447402cec9f
data/README.md CHANGED
@@ -1,11 +1,14 @@
1
- # Thoom::Strandom
2
- Random string command line script
1
+ Thoom::Strandom
2
+ ===============
3
+ Random string command line generator
3
4
 
4
- ## Installation
5
+ Installation
6
+ ------------
5
7
 
6
8
  gem install strandom
7
9
 
8
- ## CLI HELP
10
+ CLI Help
11
+ --------
9
12
 
10
13
  Usage: strandom [options]
11
14
 
@@ -26,7 +29,9 @@ Random string command line script
26
29
  --help Shows this message
27
30
  --version Current version
28
31
 
29
- ### Examples
32
+ Examples
33
+ --------
34
+
30
35
  #### Basic
31
36
 
32
37
  $ strandom --lownum 10
@@ -48,3 +53,18 @@ Examples:
48
53
 
49
54
  $ strandom --custom "%|4|*|,|?",10,"|"
50
55
  => ??4,%%,*%4
56
+
57
+ License
58
+ -------
59
+
60
+ Licensed under [MIT](https://github.com/thoom/strandom/blob/master/LICENSE).
61
+
62
+ ChangeLog
63
+ ---------
64
+
65
+ #### 2.0
66
+ Fixes Issues [#1](https://github.com/thoom/strandom/issues/1) and [#2](https://github.com/thoom/strandom/issues/2).
67
+ Complete refactor of the library class.
68
+ Added unit tests.
69
+
70
+
@@ -10,8 +10,8 @@ cust_delimiter = ''
10
10
 
11
11
  parser = OptionParser.new do |o|
12
12
  o.banner = 'Usage: strandom [options]'
13
- o.separator ""
14
- o.separator "Specific options:"
13
+ o.separator ''
14
+ o.separator 'Specific options:'
15
15
 
16
16
  o.on('--alnum [length]', 'Characters a-z, A-Z, 0-9') do |length|
17
17
  type = :alnum
@@ -67,8 +67,8 @@ parser = OptionParser.new do |o|
67
67
  len = length if length
68
68
  end
69
69
 
70
- o.separator ""
71
- o.separator "Common options:"
70
+ o.separator ''
71
+ o.separator 'Common options:'
72
72
 
73
73
  o.on_tail('--help', 'Shows this message') do
74
74
  puts o
@@ -78,10 +78,10 @@ parser = OptionParser.new do |o|
78
78
  o.on_tail('--version', 'Current version') do
79
79
  puts <<TXT
80
80
 
81
- Thoom Strandom v.1.0
82
- --------------------
81
+ Thoom Strandom v. 2.0
82
+ ---------------------
83
83
  e: zdp@thoomtech.com
84
- w: https://github.com/thoom/randstr
84
+ w: https://github.com/thoom/strandom
85
85
 
86
86
  TXT
87
87
  exit
@@ -91,16 +91,28 @@ end
91
91
  parser.parse! ARGV
92
92
 
93
93
  len = len.to_i
94
- if type == :hex
95
- result = Thoom::Strandom.hex(len: len)
96
- elsif type == :uuid
97
- result = Thoom::Strandom.uuid()
98
- elsif type == :custom
99
- cust = cust_vals.split(cust_delimiter)
100
- result = Thoom::Strandom.rand(len: len, type: type, cust: cust)
101
- else
102
- result = Thoom::Strandom.rand(len: len, type: type)
103
- end
94
+ result = case type
95
+ when :alpha
96
+ Thoom::Strandom.alphabetic(len)
97
+ when :alnum
98
+ Thoom::Strandom.alphanumeric(len)
99
+ when :custom
100
+ cust = cust_vals.split(cust_delimiter)
101
+ Thoom::Strandom.custom(len: len, values: cust)
102
+ when :hex
103
+ Thoom::Strandom.hex(len)
104
+ when :num
105
+ Thoom::Strandom.numeric(len)
106
+ when :lower
107
+ Thoom::Strandom.lower_alphabetic(len)
108
+ when :lownum
109
+ Thoom::Strandom.lower_alphanumeric(len)
110
+ when :upper
111
+ Thoom::Strandom.upper_alphabetic(len)
112
+ when :upnum
113
+ Thoom::Strandom.upper_alphanumeric(len)
114
+ when :uuid
115
+ Thoom::Strandom.uuid
116
+ end
104
117
 
105
118
  puts result
106
-
@@ -1,47 +1,61 @@
1
1
  require 'securerandom'
2
2
 
3
3
  module Thoom
4
+ # Provides several convenience methods to generating random strings
4
5
  class Strandom
6
+ @num_range = (48..57).to_a
7
+ @upper_range = (65..90).to_a
8
+ @lower_range = (97..122).to_a
9
+
5
10
  class << self
6
- def hex(len: 2)
7
- SecureRandom.hex(len / 2)
8
- end
9
-
10
- def rand(len: 0, type: :alnum, cust: [])
11
- num = (48..57).to_a
12
- upper = (65..90).to_a
13
- lower = (97..122).to_a
14
-
15
- case type
16
- when :alpha
17
- range = upper + lower
18
- when :upper
19
- range = upper
20
- when :upnum
21
- range = num + upper
22
- when :lower
23
- range = lower
24
- when :lownum
25
- range = num + lower
26
- when :num
27
- range = num
28
- when :custom
29
- range = cust
30
- else
31
- range = num + upper + lower
32
- end
11
+ attr_reader :num_range, :upper_range, :lower_range
33
12
 
34
- # pulled from http://codereview.stackexchange.com/questions/15958
35
- if type == :cust
36
- ([nil] * len).map { range.sample }.join
37
- else
38
- ([nil] * len).map { range.sample.chr }.join
39
- end
13
+ def alphabetic(len = 0)
14
+ randomize(len, upper_range + lower_range)
15
+ end
16
+
17
+ def alphanumeric(len = 0)
18
+ randomize(len, num_range + upper_range + lower_range)
19
+ end
20
+
21
+ def custom(len: 0, values: [])
22
+ ([nil] * len).map { values.sample }.join
23
+ end
24
+
25
+ def hex(len = 2)
26
+ SecureRandom.hex(len / 2)
27
+ end
28
+
29
+ def numeric(len = 0)
30
+ randomize(len, num_range)
31
+ end
32
+
33
+ def lower_alphabetic(len = 0)
34
+ randomize(len, lower_range)
35
+ end
36
+
37
+ def lower_alphanumeric(len = 0)
38
+ randomize(len, num_range + lower_range)
40
39
  end
41
40
 
42
- def uuid()
41
+ def upper_alphabetic(len = 0)
42
+ randomize(len, upper_range)
43
+ end
44
+
45
+ def upper_alphanumeric(len = 0)
46
+ randomize(len, num_range + upper_range)
47
+ end
48
+
49
+ def uuid
43
50
  SecureRandom.uuid
44
51
  end
52
+
53
+ private
54
+
55
+ def randomize(len, range)
56
+ # pulled from http://codereview.stackexchange.com/questions/15958
57
+ ([nil] * len).map { range.sample.chr }.join
58
+ end
45
59
  end
46
60
  end
47
- end
61
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strandom
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: '2.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Z.d. Peacock
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-29 00:00:00.000000000 Z
11
+ date: 2016-06-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An executable for generating a random string
14
14
  email: zdp@thoomtech.com
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  version: '0'
42
42
  requirements: []
43
43
  rubyforge_project:
44
- rubygems_version: 2.4.7
44
+ rubygems_version: 2.5.1
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: 'Thoom Strandom: A random string generator'