Permalink
Browse files

initial

  • Loading branch information...
0 parents commit 2f06fa2fb2281c35be70616deb2be4abfd79c3d3 @grosser committed Aug 30, 2009
Showing with 99 additions and 0 deletions.
  1. +13 −0 README.markdown
  2. +21 −0 Rakefile.rb
  3. +1 −0 VERSION
  4. +2 −0 init.rb
  5. +22 −0 lib/readable_random.rb
  6. 0 readable_random.gemspec
  7. +35 −0 spec/readable_random_spec.rb
  8. +5 −0 spec/spec_helper.rb
@@ -0,0 +1,13 @@
+Readable string for coupon codes or auth tokens, something people can read/write and do not confuse.
+
+ - more information density than hex -> shorter codes
+ - less confusion than base64 -> readable codes
+
+Install
+=======
+ - As Rails plugin: ` script/plugin install git://github.com/grosser/readable_random.git `
+ - As gem: ` sudo gem install grosser-readable_random --source http://gems.github.com `
+
+Usage
+=====
+ ReadableRandom.get(5) -> 'aB23r'
@@ -0,0 +1,21 @@
+desc "Run all specs in spec directory"
+task :default do
+ options = "--colour --format progress --loadby --reverse"
+ files = FileList['spec/**/*_spec.rb']
+ system("spec #{options} #{files}")
+end
+
+begin
+ require 'jeweler'
+ project_name = 'readable_random'
+ Jeweler::Tasks.new do |gem|
+ gem.name = project_name
+ gem.description = gem.summary = "Readable random strings for coupons or tokens"
+ gem.email = "grosser.michael@gmail.com"
+ gem.homepage = "http://github.com/grosser/#{project_name}"
+ gem.authors = ["Michael Grosser"]
+ gem.add_dependency ['openssl']
+ end
+rescue LoadError
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
+end
@@ -0,0 +1 @@
+0.1.0
@@ -0,0 +1,2 @@
+#only needed wen used as Rails plugin
+require 'readable_random'
@@ -0,0 +1,22 @@
+require 'openssl'
+
+class ReadableRandom
+ EXCLUDE = %w[o O 0 1 l / = +]
+
+ def self.get(size)
+ try = random_string(size * 2)
+ try = try.split('').reject{|x| EXCLUDE.include? x}.join('')
+
+ if try.length < size #too many excluded characters !?
+ get(size) #try once again...
+ else
+ try[0...size]
+ end
+ end
+
+ private
+
+ def self.random_string(size)
+ [OpenSSL::Random.random_bytes(size)].pack("m*").delete("\n")
+ end
+end
No changes.
@@ -0,0 +1,35 @@
+require 'spec/spec_helper'
+
+describe ReadableRandom do
+ describe :get do
+ def set(size)
+ set = []
+ 100.times{ set << ReadableRandom.get(size) }
+ set
+ end
+
+ it "has the expected length" do
+ set(7).map{|x|x.length}.uniq.should == [7]
+ end
+
+ it "is unique" do
+ set = set(7)
+ set.uniq.size.should == set.size
+ end
+
+ it "works for small numbers" do
+ set(1).map{|x|x.length}.uniq.should == [1]
+ end
+
+ it "works for large numbers" do
+ set(255).map{|x|x.length}.uniq.should == [255]
+ end
+
+ it "does not contain hard to read chars" do
+ set = set(7)
+ ReadableRandom::EXCLUDE.each do |ambiguouse_letter|
+ (set * '').should_not include(ambiguouse_letter)
+ end
+ end
+ end
+end
@@ -0,0 +1,5 @@
+# ---- requirements
+$LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
+
+# ---- setup environment/plugin
+require File.expand_path("../init", File.dirname(__FILE__))

0 comments on commit 2f06fa2

Please sign in to comment.