random_username 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fcaa30c20e7ef7460e9b0e54fd794a57cb59b2e
4
- data.tar.gz: 83edcf252a32e76d9cc2cfb0613d39187375da55
3
+ metadata.gz: a675071998814bca647af20257e0ec2a73809f9c
4
+ data.tar.gz: 7c9585857dfe4f97157dfc420474eddf858568c4
5
5
  SHA512:
6
- metadata.gz: 3a023446e43e957d7e6e7c72f40d6d9386e525f2a6e57e2d40759492b2fbd896e82207d23bb0d7ff3fb357b0b69fb42830ed939edd32a9b646943fd6c55be2b4
7
- data.tar.gz: 835c7110b1d0916eef8346cff440f7f43a41f70f1c5722b549035d6713505f701b8982772ee0b5cefaaad25737665bf7c7fc1b85d3e9820a2c09645a390c9666
6
+ metadata.gz: 352037dc4b567b4e7cf74a53713f26ac42acf6a3310ca84bf84d47a5022515616b65f5803971062bec939c4194300970752d55e081e784c84633a83bd9f3a548
7
+ data.tar.gz: 2c1c8990bb4fffefeb75e8db1f9ce2d0479a97c0f147ccc1a233866c9a94557ff3c8d7beea4ba22c4c7f376421f4156e2f113509e9a1c787b02098717dadc097
data/README.md CHANGED
@@ -1,4 +1,42 @@
1
- random_username
2
- ===============
1
+ # RandomUsername
3
2
 
4
- Generate random Heroku-style names
3
+ A random Heroku-style name generator.
4
+
5
+ # Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "random_username"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install random_username
18
+
19
+ ## Usage
20
+
21
+ RandomUsername.noun
22
+ => "sunrise"
23
+ => "prize"
24
+ => "sage"
25
+
26
+ RandomUsername.adjective
27
+ => "heroic"
28
+ => "worthy"
29
+ => "enigmatic"
30
+
31
+ RandomUsername.username
32
+ => "hollowlight"
33
+ => "legitsunrise"
34
+ => "earthyleaf"
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
@@ -1,6 +1,8 @@
1
1
  require "random_username/version"
2
2
 
3
3
  module RandomUsername
4
+ RandomUsername::Error = Class.new(StandardError)
5
+
4
6
  def self.adjective(options = {})
5
7
  get_item("adjectives", options)
6
8
  end
@@ -9,10 +11,15 @@ module RandomUsername
9
11
  get_item("nouns", options)
10
12
  end
11
13
 
14
+ def self.username(options = {})
15
+ options[:max_length] /= 2 if options[:max_length]
16
+ adjective(options) + noun(options)
17
+ end
18
+
12
19
  def self.get_item(filename, options = {})
13
20
  items = items_from_file(filename)
14
21
  items.select!{ |item| item.length <= options[:max_length] } if options[:max_length]
15
- items.sample
22
+ items.sample || fail(RandomUsername::Error, "No words found")
16
23
  end
17
24
 
18
25
  def self.items_from_file(filename)
@@ -1,3 +1,3 @@
1
1
  module RandomUsername
2
- VERSION = "1.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -2,8 +2,18 @@ require "minitest/autorun"
2
2
  require "random_username"
3
3
 
4
4
  class TestRandomUsername < Minitest::Test
5
+ def all_adjectives
6
+ @all_adjectives ||= RandomUsername.items_from_file("adjectives")
7
+ end
8
+
9
+ def all_nouns
10
+ @all_nouns ||= RandomUsername.items_from_file("nouns")
11
+ end
12
+
5
13
  def test_adjective
6
- refute_empty RandomUsername.adjective
14
+ adjective = RandomUsername.adjective
15
+ refute_empty adjective
16
+ assert_includes all_adjectives, adjective
7
17
  end
8
18
 
9
19
  def test_adjective_max_length
@@ -12,11 +22,15 @@ class TestRandomUsername < Minitest::Test
12
22
  end
13
23
 
14
24
  def test_adjective_invalid_max_length
15
- assert_nil RandomUsername.adjective(:max_length => 1)
25
+ assert_raises RandomUsername::Error do
26
+ RandomUsername.adjective(:max_length => 1)
27
+ end
16
28
  end
17
29
 
18
30
  def test_noun
19
- refute_empty RandomUsername.noun
31
+ noun = RandomUsername.noun
32
+ refute_empty noun
33
+ assert_includes all_nouns, noun
20
34
  end
21
35
 
22
36
  def test_noun_max_length
@@ -25,6 +39,29 @@ class TestRandomUsername < Minitest::Test
25
39
  end
26
40
 
27
41
  def test_noun_invalid_max_length
28
- assert_nil RandomUsername.noun(:max_length => 1)
42
+ assert_raises RandomUsername::Error do
43
+ RandomUsername.noun(:max_length => 1)
44
+ end
45
+ end
46
+
47
+ def test_username
48
+ username = RandomUsername.username
49
+ refute_empty username
50
+ anchor = (2..username.length).detect do |i|
51
+ all_adjectives.include?(username[0..i])
52
+ end
53
+ assert_includes all_adjectives, username[0..anchor]
54
+ assert_includes all_nouns, username[anchor+1..-1]
55
+ end
56
+
57
+ def test_username_max_length
58
+ username = RandomUsername.username(:max_length => 10)
59
+ assert username.length <= 10
60
+ end
61
+
62
+ def test_username_invalid_max_length
63
+ assert_raises RandomUsername::Error do
64
+ RandomUsername.username(:max_length => 2)
65
+ end
29
66
  end
30
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_username
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Foley