readable_random 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
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
+ - less 'stupid' then ActiveSupport::SecureRandom, e.g. hex(5).size == 5, not 10
6
+
7
+ Install
8
+ =======
9
+ - As gem: ` sudo gem install readable_random `
10
+ - As Rails plugin: ` script/plugin install git://github.com/grosser/readable_random.git `
11
+
12
+ Usage
13
+ =====
14
+ ReadableRandom.get(5) -> 'aB23r'
15
+
16
+ # user.rb
17
+ def generate_token
18
+ while token.blank? or User.find_by_token(token)
19
+ self.token = ReadableRandom.get(6)
20
+ end
21
+ end
22
+
23
+ ReadableRandom.hex(5) -> '12af4'
24
+ ReadableRandom.base64(5) -> 'at12k'
25
+
26
+ TODO
27
+ ====
28
+ - prevent nasty words by not using vovels or a more clever tick...
29
+
30
+
31
+ AUTHOR
32
+ ======
33
+ [Michael Grosser](http://pragmatig.wordpress.com)
34
+ grosser.michael@gmail.com
35
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
@@ -0,0 +1,29 @@
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.rubyforge_project = 'readable-random'
18
+ end
19
+
20
+ # fake task so that rubyforge:release works
21
+ task :rdoc do
22
+ `mkdir rdoc`
23
+ `echo documentation is at http://github.com/grosser/#{project_name} > rdoc/README.rdoc`
24
+ end
25
+
26
+ Jeweler::RubyforgeTasks.new
27
+ rescue LoadError
28
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
29
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ #only needed wen used as Rails plugin
2
+ require 'readable_random'
@@ -0,0 +1,32 @@
1
+ require 'openssl'
2
+ require "base64"
3
+
4
+ class ReadableRandom
5
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
6
+ NON_READABLE = %w[o O 0 1 l / = +]
7
+
8
+ def self.get(size)
9
+ try = random_string(size * 2)
10
+ try = try.split('').reject{|x| NON_READABLE.include? x}.join('')
11
+
12
+ if try.length < size #too many excluded characters !?
13
+ get(size) #try once again...
14
+ else
15
+ try[0...size]
16
+ end
17
+ end
18
+
19
+ def self.hex(size)
20
+ random_string(size).unpack("H*")[0][0...size]
21
+ end
22
+
23
+ def self.base64(size)
24
+ random_string(size)[0...size]
25
+ end
26
+
27
+ private
28
+
29
+ def self.random_string(size)
30
+ [OpenSSL::Random.random_bytes(size)].pack("m*").delete("\n")
31
+ end
32
+ end
@@ -0,0 +1 @@
1
+ documentation is at http://github.com/grosser/readable_random
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{readable_random}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2009-10-22}
13
+ s.description = %q{Readable random strings for coupons or tokens}
14
+ s.email = %q{grosser.michael@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "Rakefile.rb",
21
+ "VERSION",
22
+ "init.rb",
23
+ "lib/readable_random.rb",
24
+ "rdoc/README.rdoc",
25
+ "readable_random.gemspec",
26
+ "spec/readable_random_spec.rb",
27
+ "spec/spec_helper.rb"
28
+ ]
29
+ s.homepage = %q{http://github.com/grosser/readable_random}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubyforge_project = %q{readable-random}
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{Readable random strings for coupons or tokens}
35
+ s.test_files = [
36
+ "spec/spec_helper.rb",
37
+ "spec/readable_random_spec.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ReadableRandom do
4
+ def set(method, size)
5
+ set = []
6
+ 100.times{ set << ReadableRandom.send(method, size) }
7
+ set
8
+ end
9
+
10
+ describe :get do
11
+ it "has the expected length" do
12
+ set(:get, 7).map{|x|x.length}.uniq.should == [7]
13
+ end
14
+
15
+ it "is unique" do
16
+ set = set(:get, 7)
17
+ set.uniq.size.should == set.size
18
+ end
19
+
20
+ it "works for small numbers" do
21
+ set(:get, 1).map{|x|x.length}.uniq.should == [1]
22
+ end
23
+
24
+ it "works for large numbers" do
25
+ set(:get, 255).map{|x|x.length}.uniq.should == [255]
26
+ end
27
+
28
+ it "does not contain hard to read chars" do
29
+ set = set(:get, 7)
30
+ ReadableRandom::NON_READABLE.each do |ambiguouse_letter|
31
+ (set * '').should_not include(ambiguouse_letter)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe :hex do
37
+ it "has given length" do
38
+ set(:hex, 5).map{|x| x.length}.uniq.should == [5]
39
+ end
40
+
41
+ it "contains the hex chars" do
42
+ set(:hex, 5).map{|x| x.split('')}.flatten.uniq.sort.should == %w[0 1 2 3 4 5 6 7 8 9 a b c d e f]
43
+ end
44
+ end
45
+
46
+ describe :base64 do
47
+ it "has given length" do
48
+ set(:base64, 5).map{|x| x.length}.uniq.should == [5]
49
+ end
50
+
51
+ it "contains the base64 chars" do
52
+ base64_chars = (['+', '/'] + ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a)
53
+ set(:base64, 5).map{|x| x.split('')}.flatten.uniq.sort.should == base64_chars
54
+ end
55
+ end
56
+
57
+ it "has a VERSION" do
58
+ ReadableRandom::VERSION.should =~ /^\d+\.\d+\.\d+$/
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ # ---- requirements
2
+ $LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
3
+
4
+ # ---- setup environment/plugin
5
+ require File.expand_path("../init", File.dirname(__FILE__))
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: readable_random
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-22 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Readable random strings for coupons or tokens
17
+ email: grosser.michael@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - README.markdown
26
+ - Rakefile.rb
27
+ - VERSION
28
+ - init.rb
29
+ - lib/readable_random.rb
30
+ - rdoc/README.rdoc
31
+ - readable_random.gemspec
32
+ - spec/readable_random_spec.rb
33
+ - spec/spec_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/grosser/readable_random
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: readable-random
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Readable random strings for coupons or tokens
62
+ test_files:
63
+ - spec/spec_helper.rb
64
+ - spec/readable_random_spec.rb