Skip to content
Browse files

use simple defaults, make everything a instance/no more all-static

  • Loading branch information...
1 parent 5a07853 commit e7ce02a548efccd1adcfcbcfc7c706de8ab478cc @grosser committed Jan 20, 2010
Showing with 36 additions and 32 deletions.
  1. +6 −5 README.markdown
  2. +18 −16 lib/url_store.rb
  3. +2 −2 lib/url_store/compact_encoder.rb
  4. +3 −3 spec/url_store/compact_encoder_spec.rb
  5. +7 −6 spec/url_store_spec.rb
View
11 README.markdown
@@ -14,7 +14,7 @@ Install
Usage
=====
# config (e.g environment.rb)
- UrlStore.secret = 'adadasd2adsdasd4ads4eas4dea4dsea4sd'
+ UrlStore.defaults = {:secret => 'adadasd2adsdasd4ads4eas4dea4dsea4sd'}
# View:
<%= link_to 'paid', :controller=>:payments, :action=>:paid, :data=>UrlStore.encode(:id=>1, :status=>'paid') %>
@@ -26,12 +26,13 @@ Usage
raise 'FRAUD!'
end
-### Options
- UrlStore.secret = 'something random'
- UrlStore.hasher = 'MD5' # default: 'SHA1'
- UrlStore.serializer = :yaml # default: :marshal
+### Defaults
+ UrlStore.defaults = {:secret => 'something random'} # ALWAYS use your own secret
+ UrlStore.defaults = {... , :hasher => 'MD5'} # default: 'SHA1'
+ UrlStore.defaults = {... , :serializer => :yaml} # default: :marshal
### Tips
+ - If you need multiple UrlStores, just use ` UrlStore.new(:secret => 'sadasd', ...) `
- As long as you stay under 2k chars there should be no problems. [max url lengths per browser/server](http://www.boutell.com/newfaq/misc/urllength.html)
Author
View
34 lib/url_store.rb
@@ -3,42 +3,44 @@
class UrlStore
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
SECRET = 'asdkasjlwqjdqaccxnjkasdfh2313'
- HASHER = 'SHA1'
- SERIALIZER = :marshal
# (convert to base64url <-> RFC4648) and '|'
# which is not url-safe if you ask ERB/CGI, but browsers accept it
IN = '+/='
OUT = '-_|'
- @@secret = SECRET
- def self.secret=(x); @@secret=x; end
- def self.secret; @@secret; end
+ @@defaults = {}
+ def self.defaults=(x); @@defaults=x; end
- @@hasher = HASHER
- def self.hasher=(x); @@hasher=x; end
- def self.hasher; @@hasher; end
+ def self.encode(data)
+ new.encode(data)
+ end
+
+ def self.decode(string)
+ new.decode(string)
+ end
- @@serializer = SERIALIZER
- def self.serializer=(x); @@serializer=x; end
- def self.serializer; @@serializer; end
+ def initialize(options={})
+ @options = @@defaults.merge(options)
+ end
- def self.encode(data)
+ def encode(data)
string = encoder.encode(data)
string.to_s.tr(IN,OUT)
end
- def self.decode(string)
+ def decode(string)
string = string.to_s.tr(OUT,IN) # convert to base64url <-> RFC4648
encoder.decode(string)
end
private
- def self.encoder
- if secret == SECRET
+ def encoder
+ options = {:secret => SECRET}.merge(@options)
+ if options[:secret] == SECRET
warn "WARNING: you should not use the default secret! use UrlStore.secret='something'"
end
- UrlStore::CompactEncoder.new(secret, :hasher => hasher, :serializer => serializer)
+ UrlStore::CompactEncoder.new(options)
end
end
View
4 lib/url_store/compact_encoder.rb
@@ -3,8 +3,8 @@
class UrlStore
class CompactEncoder
- def initialize(secret, options={})
- @secret = secret
+ def initialize(options={})
+ @secret = options[:secret] || raise('i need a :secret !!')
@hasher = options[:hasher] || 'SHA1'
@serializer = options[:serializer] || :marshal
end
View
6 spec/url_store/compact_encoder_spec.rb
@@ -2,7 +2,7 @@
describe UrlStore::CompactEncoder do
before do
- @encoder = UrlStore::CompactEncoder.new('asdasdsa')
+ @encoder = UrlStore::CompactEncoder.new(:secret => 'asdasdsa')
@data = {:x => 1, 'asdadadadas' => 'asdasdadawvxcxcxcvjs', 'dasdasdadsadad' => 'asdasdwxczvvcjjkdfjkdf'}
end
@@ -20,12 +20,12 @@
end
it "can encode/decode with yaml" do
- @encoder = UrlStore::CompactEncoder.new('asdasdsa', :serializer => :yaml)
+ @encoder = UrlStore::CompactEncoder.new(:secret => 'asdasdsa', :serializer => :yaml)
@encoder.decode(@encoder.encode(@data)).should == @data
end
it "can hash with other hasher" do
- @encoder = UrlStore::CompactEncoder.new('asdasdsa', :hasher => 'MD5')
+ @encoder = UrlStore::CompactEncoder.new(:secret => 'asdasdsa', :hasher => 'MD5')
@encoder.decode(@encoder.encode(@data)).should == @data
end
end
View
13 spec/url_store_spec.rb
@@ -3,7 +3,8 @@
describe UrlStore do
before do
- UrlStore.secret = 'not the standart sssecrettt1231231áßðáïíœï©óáßïáöððííïö'
+ @secret = 'not the standart sssecrettt1231231áßðáïíœï©óáßïáöððííïö'
+ UrlStore.defaults = {:secret => @secret}
@data = {:x => 11212, :y => 'asdasda sdasdasdASDJKSAJDLSKDLKDS', 'asdasd' => 12312312, 12.12 => 123123212312123, :asdasdasd => '2134 adasdasóáößðóöáåöäóðᜩöóöfóöåäfóöéåfó'}
end
@@ -31,13 +32,13 @@
it "cannot decode with wrong secret" do
encoded = UrlStore.encode(@data)
- UrlStore.secret = 'xxx'
+ UrlStore.defaults = {:secret => 'xxx'}
UrlStore.decode(encoded).should == nil
end
it "warns when default secret is used" do
- UrlStore.secret = UrlStore::SECRET
- UrlStore.should_receive(:warn)
+ UrlStore.defaults = {:secret => UrlStore::SECRET}
+ $stderr.should_receive(:write).at_least(1)
UrlStore.encode(1)
end
@@ -48,13 +49,13 @@
it "can serialize using a different method" do
old = UrlStore.encode(@data)
- UrlStore.serializer = :yaml
+ UrlStore.defaults = {:serializer => :yaml, :secret => @secret}
UrlStore.encode(@data).size.should_not == old.size
end
it "can serialize using different hasher" do
old = UrlStore.encode(@data)
- UrlStore.hasher = 'MD5'
+ UrlStore.defaults = {:hasher => 'MD5', :secret => @secret}
UrlStore.encode(@data).size.should_not == old.size
end

0 comments on commit e7ce02a

Please sign in to comment.
Something went wrong with that request. Please try again.