grosser-readable_random 0.1.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.
- data/README.markdown +31 -0
- data/Rakefile.rb +21 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/lib/readable_random.rb +22 -0
- data/readable_random.gemspec +47 -0
- data/spec/readable_random_spec.rb +35 -0
- data/spec/spec_helper.rb +5 -0
- metadata +70 -0
data/README.markdown
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Readable string for coupon codes or auth tokens, something people can read/write and do not confuse.
|
|
2
|
+
|
|
3
|
+
- more information density than hex -> shorter codes
|
|
4
|
+
- less confusion than base64 -> readable codes
|
|
5
|
+
|
|
6
|
+
Install
|
|
7
|
+
=======
|
|
8
|
+
- As Rails plugin: ` script/plugin install git://github.com/grosser/readable_random.git `
|
|
9
|
+
- As gem: ` sudo gem install grosser-readable_random --source http://gems.github.com `
|
|
10
|
+
|
|
11
|
+
Usage
|
|
12
|
+
=====
|
|
13
|
+
ReadableRandom.get(5) -> 'aB23r'
|
|
14
|
+
|
|
15
|
+
# user.rb
|
|
16
|
+
def generate_token
|
|
17
|
+
while token.blank? or User.find_by_token(token)
|
|
18
|
+
self.token = ReadableRandom.get(6)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
TODO
|
|
23
|
+
====
|
|
24
|
+
- prevent nasty words by not using vovels or a more clever tick...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
AUTHOR
|
|
28
|
+
======
|
|
29
|
+
[Michael Grosser](http://pragmatig.wordpress.com)
|
|
30
|
+
grosser.michael@gmail.com
|
|
31
|
+
Hereby placed under public domain, do what you want, just do not hold me accountable...
|
data/Rakefile.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
desc "Run all specs in spec directory"
|
|
2
|
+
task :default do
|
|
3
|
+
options = "--colour --format progress --loadby --reverse"
|
|
4
|
+
files = FileList['spec/**/*_spec.rb']
|
|
5
|
+
system("spec #{options} #{files}")
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
begin
|
|
9
|
+
require 'jeweler'
|
|
10
|
+
project_name = 'readable_random'
|
|
11
|
+
Jeweler::Tasks.new do |gem|
|
|
12
|
+
gem.name = project_name
|
|
13
|
+
gem.description = gem.summary = "Readable random strings for coupons or tokens"
|
|
14
|
+
gem.email = "grosser.michael@gmail.com"
|
|
15
|
+
gem.homepage = "http://github.com/grosser/#{project_name}"
|
|
16
|
+
gem.authors = ["Michael Grosser"]
|
|
17
|
+
gem.add_dependency ['openssl']
|
|
18
|
+
end
|
|
19
|
+
rescue LoadError
|
|
20
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
|
21
|
+
end
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.1
|
data/init.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'openssl'
|
|
2
|
+
|
|
3
|
+
class ReadableRandom
|
|
4
|
+
EXCLUDE = %w[o O 0 1 l / = +]
|
|
5
|
+
|
|
6
|
+
def self.get(size)
|
|
7
|
+
try = random_string(size * 2)
|
|
8
|
+
try = try.split('').reject{|x| EXCLUDE.include? x}.join('')
|
|
9
|
+
|
|
10
|
+
if try.length < size #too many excluded characters !?
|
|
11
|
+
get(size) #try once again...
|
|
12
|
+
else
|
|
13
|
+
try[0...size]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def self.random_string(size)
|
|
20
|
+
[OpenSSL::Random.random_bytes(size)].pack("m*").delete("\n")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{readable_random}
|
|
5
|
+
s.version = "0.1.1"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["Michael Grosser"]
|
|
9
|
+
s.date = %q{2009-09-02}
|
|
10
|
+
s.description = %q{Readable random strings for coupons or tokens}
|
|
11
|
+
s.email = %q{grosser.michael@gmail.com}
|
|
12
|
+
s.extra_rdoc_files = [
|
|
13
|
+
"README.markdown"
|
|
14
|
+
]
|
|
15
|
+
s.files = [
|
|
16
|
+
"README.markdown",
|
|
17
|
+
"Rakefile.rb",
|
|
18
|
+
"VERSION",
|
|
19
|
+
"init.rb",
|
|
20
|
+
"lib/readable_random.rb",
|
|
21
|
+
"readable_random.gemspec",
|
|
22
|
+
"spec/readable_random_spec.rb",
|
|
23
|
+
"spec/spec_helper.rb"
|
|
24
|
+
]
|
|
25
|
+
s.homepage = %q{http://github.com/grosser/readable_random}
|
|
26
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
27
|
+
s.require_paths = ["lib"]
|
|
28
|
+
s.rubygems_version = %q{1.3.4}
|
|
29
|
+
s.summary = %q{Readable random strings for coupons or tokens}
|
|
30
|
+
s.test_files = [
|
|
31
|
+
"spec/spec_helper.rb",
|
|
32
|
+
"spec/readable_random_spec.rb"
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
if s.respond_to? :specification_version then
|
|
36
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
37
|
+
s.specification_version = 3
|
|
38
|
+
|
|
39
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
40
|
+
s.add_runtime_dependency(%q<openssl>, [">= 0"])
|
|
41
|
+
else
|
|
42
|
+
s.add_dependency(%q<openssl>, [">= 0"])
|
|
43
|
+
end
|
|
44
|
+
else
|
|
45
|
+
s.add_dependency(%q<openssl>, [">= 0"])
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'spec/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe ReadableRandom do
|
|
4
|
+
describe :get do
|
|
5
|
+
def set(size)
|
|
6
|
+
set = []
|
|
7
|
+
100.times{ set << ReadableRandom.get(size) }
|
|
8
|
+
set
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "has the expected length" do
|
|
12
|
+
set(7).map{|x|x.length}.uniq.should == [7]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "is unique" do
|
|
16
|
+
set = set(7)
|
|
17
|
+
set.uniq.size.should == set.size
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "works for small numbers" do
|
|
21
|
+
set(1).map{|x|x.length}.uniq.should == [1]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "works for large numbers" do
|
|
25
|
+
set(255).map{|x|x.length}.uniq.should == [255]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "does not contain hard to read chars" do
|
|
29
|
+
set = set(7)
|
|
30
|
+
ReadableRandom::EXCLUDE.each do |ambiguouse_letter|
|
|
31
|
+
(set * '').should_not include(ambiguouse_letter)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: grosser-readable_random
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Michael Grosser
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-09-02 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: openssl
|
|
17
|
+
type: :runtime
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: "0"
|
|
24
|
+
version:
|
|
25
|
+
description: Readable random strings for coupons or tokens
|
|
26
|
+
email: grosser.michael@gmail.com
|
|
27
|
+
executables: []
|
|
28
|
+
|
|
29
|
+
extensions: []
|
|
30
|
+
|
|
31
|
+
extra_rdoc_files:
|
|
32
|
+
- README.markdown
|
|
33
|
+
files:
|
|
34
|
+
- README.markdown
|
|
35
|
+
- Rakefile.rb
|
|
36
|
+
- VERSION
|
|
37
|
+
- init.rb
|
|
38
|
+
- lib/readable_random.rb
|
|
39
|
+
- readable_random.gemspec
|
|
40
|
+
- spec/readable_random_spec.rb
|
|
41
|
+
- spec/spec_helper.rb
|
|
42
|
+
has_rdoc: false
|
|
43
|
+
homepage: http://github.com/grosser/readable_random
|
|
44
|
+
post_install_message:
|
|
45
|
+
rdoc_options:
|
|
46
|
+
- --charset=UTF-8
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: "0"
|
|
54
|
+
version:
|
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: "0"
|
|
60
|
+
version:
|
|
61
|
+
requirements: []
|
|
62
|
+
|
|
63
|
+
rubyforge_project:
|
|
64
|
+
rubygems_version: 1.2.0
|
|
65
|
+
signing_key:
|
|
66
|
+
specification_version: 3
|
|
67
|
+
summary: Readable random strings for coupons or tokens
|
|
68
|
+
test_files:
|
|
69
|
+
- spec/spec_helper.rb
|
|
70
|
+
- spec/readable_random_spec.rb
|