pickaname 0.1.3 → 0.1.8

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.
@@ -1,46 +1,113 @@
1
1
  require "pickaname/version"
2
+ require 'timeout'
2
3
 
3
4
  module Pickaname
4
5
  class Error < StandardError; end
5
6
 
6
7
  class Robot
7
- PREFIX = File.foreach("lib/data/prefix.txt").map { |line| line.split(' ') }
8
- SUFFIX = File.foreach("lib/data/suffix.txt").map { |line| line.split(' ') }
9
- FUNNY = File.foreach("lib/data/funny.txt").map { |line| line.split(' ') }
8
+ PREFIX = File.foreach(File.join(File.dirname(__FILE__), 'data', 'prefix.txt')).map { |line| line.split(' ') }
9
+ SUFFIX = File.foreach(File.join(File.dirname(__FILE__), 'data', 'suffix.txt')).map { |line| line.split(' ') }
10
+ FUNNY = File.foreach(File.join(File.dirname(__FILE__), 'data', 'funny.txt')).map { |line| line.split(' ') }
11
+ COMMON = File.foreach(File.join(File.dirname(__FILE__), 'data', 'common.txt')).map { |line| line.split(' ') }
12
+ DARK = File.foreach(File.join(File.dirname(__FILE__), 'data', 'dark.txt')).map { |line| line.split(' ') }
13
+
14
+ FIRSTNAME = File.foreach(File.join(File.dirname(__FILE__), 'data', 'firstname.txt')).map { |line| line.split(' ') }
15
+ LASTNAME = File.foreach(File.join(File.dirname(__FILE__), 'data', 'lastname.txt')).map { |line| line.split(' ') }
16
+
17
+
10
18
  PREFIX_SIZE = 1
11
19
  SUFFIX_SIZE = 1
12
20
 
21
+ TIMEOUT_IN_SECONDS = 5
22
+
13
23
  attr_reader :name
14
24
 
15
25
  def initialize()
16
- @name = random_string
26
+ @name = random_str
27
+ end
28
+
29
+ # def self.pseudo
30
+ # @name = PREFIX.sample(PREFIX_SIZE).join << SUFFIX.sample(SUFFIX_SIZE).join
31
+ # end
32
+
33
+ def self.common(length: nil)
34
+ begin
35
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
36
+ @name = COMMON.sample(PREFIX_SIZE).join << COMMON.sample(SUFFIX_SIZE).join
37
+ while length
38
+ break if @name.length == length
39
+ @name = COMMON.sample(PREFIX_SIZE).join << COMMON.sample(SUFFIX_SIZE).join
40
+ end
41
+ return @name
42
+ end
43
+ rescue Timeout::Error
44
+ random_letters(length: length)
45
+ end
17
46
  end
18
47
 
19
- def self.pseudo
20
- @name = PREFIX.sample(PREFIX_SIZE).join << SUFFIX.sample(SUFFIX_SIZE).join
48
+ def self.funny(length: nil)
49
+ begin
50
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
51
+ @name = COMMON.sample(PREFIX_SIZE).join << FUNNY.sample(SUFFIX_SIZE).join
52
+ while length
53
+ break if @name.length == length
54
+ @name = COMMON.sample(PREFIX_SIZE).join << FUNNY.sample(SUFFIX_SIZE).join
55
+ end
56
+ return @name
57
+ end
58
+ rescue Timeout::Error
59
+ random_letters(length: length)
60
+ end
21
61
  end
22
62
 
23
- def self.funny
24
- @name = FUNNY.sample(PREFIX_SIZE).join << FUNNY.sample(SUFFIX_SIZE).join
63
+ def self.dark(length: nil)
64
+ begin
65
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
66
+ @name = COMMON.sample(PREFIX_SIZE).join << DARK.sample(SUFFIX_SIZE).join
67
+ while length
68
+ break if @name.length == length
69
+ @name = COMMON.sample(PREFIX_SIZE).join << DARK.sample(SUFFIX_SIZE).join
70
+ end
71
+ return @name
72
+ end
73
+ rescue Timeout::Error
74
+ random_letters(length: length)
75
+ end
25
76
  end
26
77
 
27
- def self.random(record = Robot.new)
28
- @name = record.random_string
78
+ def self.celebrity(length: nil)
79
+ begin
80
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
81
+ @name = FIRSTNAME.sample(PREFIX_SIZE).join << LASTNAME.sample(SUFFIX_SIZE).join
82
+ while length
83
+ break if @name.length == length
84
+ @name = FIRSTNAME.sample(PREFIX_SIZE).join << LASTNAME.sample(SUFFIX_SIZE).join
85
+ end
86
+ return @name
87
+ end
88
+ rescue Timeout::Error
89
+ random_letters(length: length)
90
+ end
29
91
  end
30
92
 
31
- def random_string
32
- down = ('a'..'z').to_a
33
- up = ('A'..'Z').to_a
34
- digits = ('0'..'9').to_a
35
- [extract1(down), extract1(up), extract1(digits)].
36
- concat(((down+up+digits).sample(8))).shuffle.join
93
+ def self.random_letters(length: 8)
94
+ record = Robot.new
95
+ begin
96
+ Timeout::timeout(TIMEOUT_IN_SECONDS) do
97
+ @name = record.random_str(length)
98
+ while length
99
+ break if @name.length == length
100
+ @name = record.random_str(length)
101
+ end
102
+ return @name
103
+ end
104
+ rescue Timeout::Error
105
+ puts "Timeout: No results found"
106
+ end
37
107
  end
38
108
 
39
- def extract1(arr)
40
- i = arr.size.times.to_a.sample
41
- c = arr[i]
42
- arr.delete_at(i)
43
- c
109
+ def random_str(length = 8)
110
+ return Array.new(length){[*"a".."z", *"0".."9"].sample}.join
44
111
  end
45
112
  end
46
113
  end
@@ -1,3 +1,3 @@
1
1
  module Pickaname
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pickaname
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-26 00:00:00.000000000 Z
11
+ date: 2020-09-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Randomly generates a name
14
14
  email:
@@ -24,9 +24,14 @@ files:
24
24
  - LICENSE.txt
25
25
  - README.md
26
26
  - Rakefile
27
+ - _config.yml
27
28
  - bin/console
28
29
  - bin/setup
30
+ - lib/data/common.txt
31
+ - lib/data/dark.txt
32
+ - lib/data/firstname.txt
29
33
  - lib/data/funny.txt
34
+ - lib/data/lastname.txt
30
35
  - lib/data/prefix.txt
31
36
  - lib/data/suffix.txt
32
37
  - lib/pickaname.rb