pickaname 0.1.6 → 0.1.7

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: 4d272f3d9e29e4007227045e2011d2775f87c24d963ed0574a3a7d56575f4f82
4
- data.tar.gz: 7ed45707977ef503eb6a77981ecc2a8dbcb8461833f924fd306e3d94b55c3ec2
3
+ metadata.gz: 1b45a89976620b0177db6448dfb5e903bef5e46ef7a734d9bf17f2a2469915f3
4
+ data.tar.gz: 3e346e8f807a19f8489d8fd232b5b74f29f5e7a1c57e235f934037e2a630aea7
5
5
  SHA512:
6
- metadata.gz: '048a505160ba09030de9d60cf5e3c227fa4bcdb0081c0964b1196096277dddc6651dce630b823b807a88e617eedc2e865563204b8671cebdc74d5215cb588816'
7
- data.tar.gz: 8358171cc4354be80f25aa7d121cc1a7c56bfb7e798b5e5e0856d15e477540dee56e2b749ed128e5bcb4f99a2867ee65b88da18878095ccfa314b686411f7750
6
+ metadata.gz: ef31341e9158e4fae8709bf03920d7e134aab6329d25e9785da01013d5457e69cd9301de4ef8af25bd630daa98aeca95d0900216cea6cc699d0e2bb0c560a340
7
+ data.tar.gz: 7f1283cac196c8137474d7aa900827da983f4a87a12f20993d05781fca9723c709dee239391ae9c44bc3f3266019ba456be98d6b53e05c50b43986aec55729df
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pickaname (0.1.6)
4
+ pickaname (0.1.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -36,6 +36,15 @@ To generate a random name:
36
36
 
37
37
  `Pickaname::Robot.random_letters`
38
38
 
39
+ Pass `length` argument to generate a name with specific length:
40
+
41
+ ```ruby
42
+ Pickaname::Robot.common(length: 8)
43
+ Pickaname::Robot.funny(length: 8)
44
+ Pickaname::Robot.dark(length: 8)
45
+ Pickaname::Robot.common(length: 8)
46
+ ```
47
+
39
48
  ## Development
40
49
 
41
50
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,4 +1,5 @@
1
1
  require "pickaname/version"
2
+ require 'timeout'
2
3
 
3
4
  module Pickaname
4
5
  class Error < StandardError; end
@@ -13,45 +14,81 @@ module Pickaname
13
14
  PREFIX_SIZE = 1
14
15
  SUFFIX_SIZE = 1
15
16
 
17
+ TIMEOUT_IN_SECONDS = 5
18
+
16
19
  attr_reader :name
17
20
 
18
21
  def initialize()
19
- @name = random_string
22
+ @name = random_str
20
23
  end
21
24
 
22
25
  # def self.pseudo
23
26
  # @name = PREFIX.sample(PREFIX_SIZE).join << SUFFIX.sample(SUFFIX_SIZE).join
24
27
  # end
25
28
 
26
- def self.common
27
- @name = COMMON.sample(PREFIX_SIZE).join << COMMON.sample(SUFFIX_SIZE).join
28
- end
29
-
30
- def self.funny
31
- @name = COMMON.sample(PREFIX_SIZE).join << FUNNY.sample(SUFFIX_SIZE).join
29
+ def self.common(length: nil)
30
+ begin
31
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
32
+ @name = COMMON.sample(PREFIX_SIZE).join << COMMON.sample(SUFFIX_SIZE).join
33
+ while length
34
+ break if @name.length == length
35
+ @name = COMMON.sample(PREFIX_SIZE).join << COMMON.sample(SUFFIX_SIZE).join
36
+ end
37
+ return @name
38
+ end
39
+ rescue Timeout::Error
40
+ puts "Timeout: No results found"
41
+ end
32
42
  end
33
43
 
34
- def self.dark
35
- @name = COMMON.sample(PREFIX_SIZE).join << DARK.sample(SUFFIX_SIZE).join
44
+ def self.funny(length: nil)
45
+ begin
46
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
47
+ @name = COMMON.sample(PREFIX_SIZE).join << FUNNY.sample(SUFFIX_SIZE).join
48
+ while length
49
+ break if @name.length == length
50
+ @name = COMMON.sample(PREFIX_SIZE).join << FUNNY.sample(SUFFIX_SIZE).join
51
+ end
52
+ return @name
53
+ end
54
+ rescue Timeout::Error
55
+ puts "Timeout: No results found"
56
+ end
36
57
  end
37
58
 
38
- def self.random_letters(record = Robot.new)
39
- @name = record.random_string
59
+ def self.dark(length: nil)
60
+ begin
61
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
62
+ @name = COMMON.sample(PREFIX_SIZE).join << DARK.sample(SUFFIX_SIZE).join
63
+ while length
64
+ break if @name.length == length
65
+ @name = COMMON.sample(PREFIX_SIZE).join << DARK.sample(SUFFIX_SIZE).join
66
+ end
67
+ return @name
68
+ end
69
+ rescue Timeout::Error
70
+ puts "Timeout: No results found"
71
+ end
40
72
  end
41
73
 
42
- def random_string
43
- down = ('a'..'z').to_a
44
- up = ('A'..'Z').to_a
45
- digits = ('0'..'9').to_a
46
- [extract1(down), extract1(up), extract1(digits)].
47
- concat(((down+up+digits).sample(8))).shuffle.join
74
+ def self.random_letters(length: 8)
75
+ record = Robot.new
76
+ begin
77
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
78
+ @name = record.random_str(length)
79
+ while length
80
+ break if @name.length == length
81
+ @name = record.random_str(length)
82
+ end
83
+ return @name
84
+ end
85
+ rescue Timeout::Error
86
+ puts "Timeout: No results found"
87
+ end
48
88
  end
49
89
 
50
- def extract1(arr)
51
- i = arr.size.times.to_a.sample
52
- c = arr[i]
53
- arr.delete_at(i)
54
- c
90
+ def random_str(length = 8)
91
+ return Array.new(length){[*"a".."z", *"0".."9"].sample}.join
55
92
  end
56
93
  end
57
94
  end
@@ -1,3 +1,3 @@
1
1
  module Pickaname
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pickaname
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karan