sluggo 0.0.1
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 +15 -0
- data/.gitignore +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/LICENSE +20 -0
- data/README.md +45 -0
- data/Rakefile +25 -0
- data/lib/sluggo.rb +56 -0
- data/lib/sluggo/random.rb +24 -0
- data/lib/sluggo/version.rb +3 -0
- data/sluggo.gemspec +23 -0
- data/spec/sluggo/random_spec.rb +26 -0
- data/spec/sluggo_spec.rb +41 -0
- data/spec/spec_helper.rb +6 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MzU0OTcxMjNjMzdiZmRkYWM3YjVlY2ZiMDc0NTdhZTRmY2Q5NTFiMg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YjQ3ODVkNDk2OTk4OGZlMGI3Zjc3NzU5MzY0ZjQ2OTAzMmNmMmVkMw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MTljZDBmMDYwMDNjMDYxM2IxNmFlZGQwODI3OTU5NTUyY2VlNjM5MzE2NDhk
|
10
|
+
MTQ1NjYwOWFhMmQyMDM0ZGM1MTc5MmY5ODNkYzY2ZTZlY2U5ZGMzOWNmMzNm
|
11
|
+
OGQ1M2QyMGI4OGU2MGVhOWI1Mzg3M2E4NTZlMWM0NjM2MDI4ZDg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZGY5ZDJmNTBlMzAyNWUzZDQwMGE2ZmQ4NmIyYzZmNGRhYTdhYmE1NmI4ODYz
|
14
|
+
NjkyYjBjNmE4MTI5MTI4ZGMxOTRlZTE0OTFhYzU2Yzc5ZDI2MmY2NTcxN2Jh
|
15
|
+
ODlhNmJjNjFkMjk3OTBmMTM5MzJhMjYwYWNhYmU3M2RlMjc3MTM=
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
slugmill
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sluggo (0.0.1)
|
5
|
+
radix62 (~> 1.0.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
metaclass (0.0.1)
|
11
|
+
minitest (5.0.8)
|
12
|
+
mocha (0.14.0)
|
13
|
+
metaclass (~> 0.0.1)
|
14
|
+
radix62 (1.0.1)
|
15
|
+
rake (10.1.0)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
ruby
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
minitest (~> 5.0.8)
|
22
|
+
mocha
|
23
|
+
rake (~> 10.1.0)
|
24
|
+
sluggo!
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Dennis Walters
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Sluggo #
|
2
|
+
|
3
|
+
Sometimes, we just want a string of quasi-random gobbledeygook to use as
|
4
|
+
a token of some sort. We may want these strings to be unique (or at least
|
5
|
+
somewhat close to it), or we may just them to be a given length.
|
6
|
+
|
7
|
+
That's what this is for.
|
8
|
+
|
9
|
+
## Important Note ##
|
10
|
+
|
11
|
+
This is very much not ready for primetime. I'm only releasing right now so
|
12
|
+
as to stop working on it and accept a baseline.
|
13
|
+
|
14
|
+
## Install ##
|
15
|
+
|
16
|
+
You can add it to your Gemfile:
|
17
|
+
|
18
|
+
gem 'sluggo'
|
19
|
+
|
20
|
+
Or you can do it the hard way:
|
21
|
+
|
22
|
+
gem install sluggo
|
23
|
+
|
24
|
+
## Usage ##
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'sluggo'
|
28
|
+
|
29
|
+
# Let's get a 6-character random base62 slug
|
30
|
+
|
31
|
+
slug = Sluggo::Random.base62(6)
|
32
|
+
|
33
|
+
# Heck, let's get a possibly unique 6-character base62 slug
|
34
|
+
# This works by passing a block to the rare method. If the block
|
35
|
+
# returns true, we assume that the proposed slug is unique and
|
36
|
+
# return it. We're going to assume that we're working with a
|
37
|
+
# Widget with a .open_key? method that returns true if the
|
38
|
+
# supplied key isn't in use.
|
39
|
+
|
40
|
+
slug = Sluggo.rare(6) {|slug| Widget.open_key?(slug)}
|
41
|
+
```
|
42
|
+
|
43
|
+
## License ##
|
44
|
+
|
45
|
+
MIT License. Copyright 2013 Ess
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
require 'rake/testtask'
|
6
|
+
|
7
|
+
RUBY_TEST_VERSIONS = [
|
8
|
+
'1.8.7',
|
9
|
+
'1.9.2-p180',
|
10
|
+
'1.9.3',
|
11
|
+
'2.0.0'
|
12
|
+
]
|
13
|
+
|
14
|
+
Rake::TestTask.new do |t|
|
15
|
+
t.libs.push "spec"
|
16
|
+
t.pattern = 'spec/**/*_spec.rb'
|
17
|
+
t.verbose = true
|
18
|
+
end
|
19
|
+
|
20
|
+
task :default => :test
|
21
|
+
|
22
|
+
desc 'Tests the library against several rubies'
|
23
|
+
task :overtest do
|
24
|
+
system "rvm #{RUBY_TEST_VERSIONS.map{|x| "#{x}@slugmill"}.join(',')} do bundle exec rake test"
|
25
|
+
end
|
data/lib/sluggo.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'radix62'
|
2
|
+
require 'sluggo/version'
|
3
|
+
require 'sluggo/random'
|
4
|
+
|
5
|
+
# Sluggo generates strings that look like tokens
|
6
|
+
module Sluggo
|
7
|
+
# Repeatedly attempt to generate a unique token, where uniqueness is
|
8
|
+
# determined by a provided code block. This may not actually end up
|
9
|
+
# generating a unique token, but possibly just a rare token.
|
10
|
+
# @param options [Hash]
|
11
|
+
# @option options [Integer] :length (6) the length of the token to return
|
12
|
+
# @option options [Integer] :chances (100) the number of tries before
|
13
|
+
# giving up on uniqueness
|
14
|
+
# @param is_unique [Proc] a proc that returns true if the supplied
|
15
|
+
# |slug| is unique
|
16
|
+
# @raise [ArgumentError] if no block is given
|
17
|
+
# @raise [ArgumentError] if chances are provided, but is < 1
|
18
|
+
# @raise [ArgumentError] if length is provided, but is < 1
|
19
|
+
def self.rare(options = {}, &is_unique)
|
20
|
+
length, chances, is_unique = sanitize_options(
|
21
|
+
options.merge({:is_unique => is_unique})
|
22
|
+
)
|
23
|
+
|
24
|
+
while (chances -= 1) >= 0 do
|
25
|
+
current_slug = Random.base62(length)
|
26
|
+
break if is_unique.call(current_slug)
|
27
|
+
end
|
28
|
+
current_slug
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
private
|
33
|
+
def sanitize_options(options = {})
|
34
|
+
if options[:is_unique].nil?
|
35
|
+
raise ArgumentError.new(rare_error_message(:is_unique))
|
36
|
+
end
|
37
|
+
|
38
|
+
options[:chances].tap do |chances|
|
39
|
+
unless chances.nil?
|
40
|
+
raise ArgumentError.new(rare_error_message(:chances, chances)) if chances < 1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
[options[:length] || 6, options[:chances] || 100, options[:is_unique]]
|
45
|
+
end
|
46
|
+
|
47
|
+
def rare_error_message(option, value = nil)
|
48
|
+
case option
|
49
|
+
when :is_unique
|
50
|
+
'You must provide a block to determine uniqueness'
|
51
|
+
when :chances
|
52
|
+
"You must allow at least one chance (chances: #{value})"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'radix62'
|
2
|
+
|
3
|
+
module Sluggo
|
4
|
+
# Used for generating random strings of gobbledeygook
|
5
|
+
class Random
|
6
|
+
# Generate a random base62-encoded string
|
7
|
+
# @param length (Integer)
|
8
|
+
# @return [String] a random selection of base62 digits
|
9
|
+
# @raise [ArgumentError] when length < 1
|
10
|
+
def self.base62(length = 1)
|
11
|
+
length = sanitize_length(length)
|
12
|
+
Array.new(length).map {Radix62.encode62(rand(62))}.join
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
private
|
17
|
+
def sanitize_length(length)
|
18
|
+
length = length.to_i
|
19
|
+
raise ArgumentError.new('length must be at lest 1') if length < 1
|
20
|
+
length
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/sluggo.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/sluggo/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dennis Walters"]
|
6
|
+
gem.email = ["dennis@elevatorup.com"]
|
7
|
+
gem.description = %q{A gobbledeygook string generator}
|
8
|
+
gem.summary = %q{A gobbledeygook string generator}
|
9
|
+
gem.homepage = "http://github.com/ess/sluggo"
|
10
|
+
gem.license = 'MIT'
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "sluggo"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = Sluggo::VERSION
|
18
|
+
|
19
|
+
gem.add_dependency 'radix62', '~> 1.0.1'
|
20
|
+
gem.add_development_dependency 'minitest', '~> 5.0.8'
|
21
|
+
gem.add_development_dependency 'mocha'
|
22
|
+
gem.add_development_dependency 'rake', '~> 10.1.0'
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sluggo/random'
|
3
|
+
|
4
|
+
describe Sluggo::Random do
|
5
|
+
let(:klass) {Sluggo::Random}
|
6
|
+
|
7
|
+
describe %{.base62} do
|
8
|
+
it %{defaults to a length of 1} do
|
9
|
+
klass.base62.length.must_equal 1
|
10
|
+
end
|
11
|
+
|
12
|
+
it %{can take a specific length} do
|
13
|
+
klass.base62(12).length.must_equal 12
|
14
|
+
end
|
15
|
+
|
16
|
+
it %{is comprised of valid slug characters} do
|
17
|
+
klass.base62.split('').each do |character|
|
18
|
+
(0..61).to_a.must_include Radix62.decode62(character)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it %{requires that a provided length be > 0} do
|
23
|
+
lambda {klass.base62(0)}.must_raise ArgumentError
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/sluggo_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sluggo'
|
3
|
+
|
4
|
+
describe Sluggo do
|
5
|
+
let(:klass) {Sluggo}
|
6
|
+
let(:random) {Sluggo::Random}
|
7
|
+
|
8
|
+
describe %{.rare} do
|
9
|
+
it %{requires a block to check for uniqueness} do
|
10
|
+
lambda {klass.rare}.must_raise ArgumentError
|
11
|
+
klass.rare {true}.must_be_kind_of String
|
12
|
+
end
|
13
|
+
|
14
|
+
it %{allows a length} do
|
15
|
+
generated = klass.rare(:length => 12) {true}
|
16
|
+
generated.length.must_equal 12
|
17
|
+
end
|
18
|
+
|
19
|
+
it %{is uses Sluggo::Random} do
|
20
|
+
random.expects(:base62)
|
21
|
+
klass.rare {true}
|
22
|
+
end
|
23
|
+
|
24
|
+
it %{has 100 chances to generate a unique slug by default} do
|
25
|
+
chances = 100
|
26
|
+
random.expects(:base62).at_most(100).at_least(100)
|
27
|
+
klass.rare(:length => 1) {false}
|
28
|
+
end
|
29
|
+
|
30
|
+
it %{also accepts a number of chances to generate a unique slug} do
|
31
|
+
chances = 10
|
32
|
+
random.expects(:base62).at_most(chances)
|
33
|
+
klass.rare(:length => 1, :chances => chances) {false}
|
34
|
+
end
|
35
|
+
|
36
|
+
it %{needs at least 1 chance to generate a unique slug} do
|
37
|
+
lambda {klass.rare(:length => 1, :chances => 0) {false}}.must_raise ArgumentError
|
38
|
+
klass.rare(:length => 1, :chances => 1) {false}.must_be_kind_of String
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dennis Walters
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: radix62
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.0.8
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.0.8
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mocha
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 10.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 10.1.0
|
69
|
+
description: A gobbledeygook string generator
|
70
|
+
email:
|
71
|
+
- dennis@elevatorup.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .ruby-gemset
|
78
|
+
- .ruby-version
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/sluggo.rb
|
85
|
+
- lib/sluggo/random.rb
|
86
|
+
- lib/sluggo/version.rb
|
87
|
+
- sluggo.gemspec
|
88
|
+
- spec/sluggo/random_spec.rb
|
89
|
+
- spec/sluggo_spec.rb
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
homepage: http://github.com/ess/sluggo
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.1.11
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A gobbledeygook string generator
|
115
|
+
test_files:
|
116
|
+
- spec/sluggo/random_spec.rb
|
117
|
+
- spec/sluggo_spec.rb
|
118
|
+
- spec/spec_helper.rb
|