url_store 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -14,7 +14,7 @@ Install
14
14
  Usage
15
15
  =====
16
16
  # config (e.g environment.rb)
17
- UrlStore.secret = 'adadasd2adsdasd4ads4eas4dea4dsea4sd'
17
+ UrlStore.defaults = {:secret => 'adadasd2adsdasd4ads4eas4dea4dsea4sd'}
18
18
 
19
19
  # View:
20
20
  <%= link_to 'paid', :controller=>:payments, :action=>:paid, :data=>UrlStore.encode(:id=>1, :status=>'paid') %>
@@ -26,13 +26,17 @@ Usage
26
26
  raise 'FRAUD!'
27
27
  end
28
28
 
29
- ### Options
30
- UrlStore.secret = 'something random'
31
- UrlStore.hasher = 'MD5' # default: 'SHA1'
32
- UrlStore.serializer = :yaml # default: :marshal
29
+ ### Defaults
30
+ UrlStore.defaults = {:secret => 'something random'} # ALWAYS use your own secret
31
+ UrlStore.defaults = {... , :hasher => 'MD5'} # default: 'SHA1'
32
+ UrlStore.defaults = {... , :serializer => :yaml} # default: :marshal
33
+
34
+ ### Tips
35
+ - If you need multiple UrlStores, just use ` UrlStore.new(:secret => 'sadasd', ...) `
36
+ - 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)
33
37
 
34
38
  Author
35
39
  =======
36
40
  [Michael Grosser](http://pragmatig.wordpress.com)
37
41
  grosser.michael@gmail.com
38
- Hereby placed under public domain, do what you want, just do not hold me accountable...
42
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -3,8 +3,8 @@ require 'zlib'
3
3
 
4
4
  class UrlStore
5
5
  class CompactEncoder
6
- def initialize(secret, options={})
7
- @secret = secret
6
+ def initialize(options={})
7
+ @secret = options[:secret] || raise('i need a :secret !!')
8
8
  @hasher = options[:hasher] || 'SHA1'
9
9
  @serializer = options[:serializer] || :marshal
10
10
  end
data/lib/url_store.rb CHANGED
@@ -3,42 +3,44 @@ require 'url_store/compact_encoder'
3
3
  class UrlStore
4
4
  VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
5
5
  SECRET = 'asdkasjlwqjdqaccxnjkasdfh2313'
6
- HASHER = 'SHA1'
7
- SERIALIZER = :marshal
8
6
 
9
7
  # (convert to base64url <-> RFC4648) and '|'
10
8
  # which is not url-safe if you ask ERB/CGI, but browsers accept it
11
9
  IN = '+/='
12
10
  OUT = '-_|'
13
11
 
14
- @@secret = SECRET
15
- def self.secret=(x); @@secret=x; end
16
- def self.secret; @@secret; end
12
+ @@defaults = {}
13
+ def self.defaults=(x); @@defaults=x; end
17
14
 
18
- @@hasher = HASHER
19
- def self.hasher=(x); @@hasher=x; end
20
- def self.hasher; @@hasher; end
15
+ def self.encode(data)
16
+ new.encode(data)
17
+ end
18
+
19
+ def self.decode(string)
20
+ new.decode(string)
21
+ end
21
22
 
22
- @@serializer = SERIALIZER
23
- def self.serializer=(x); @@serializer=x; end
24
- def self.serializer; @@serializer; end
23
+ def initialize(options={})
24
+ @options = @@defaults.merge(options)
25
+ end
25
26
 
26
- def self.encode(data)
27
+ def encode(data)
27
28
  string = encoder.encode(data)
28
29
  string.to_s.tr(IN,OUT)
29
30
  end
30
31
 
31
- def self.decode(string)
32
+ def decode(string)
32
33
  string = string.to_s.tr(OUT,IN) # convert to base64url <-> RFC4648
33
34
  encoder.decode(string)
34
35
  end
35
36
 
36
37
  private
37
38
 
38
- def self.encoder
39
- if secret == SECRET
39
+ def encoder
40
+ options = {:secret => SECRET}.merge(@options)
41
+ if options[:secret] == SECRET
40
42
  warn "WARNING: you should not use the default secret! use UrlStore.secret='something'"
41
43
  end
42
- UrlStore::CompactEncoder.new(secret, :hasher => hasher, :serializer => serializer)
44
+ UrlStore::CompactEncoder.new(options)
43
45
  end
44
46
  end
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe UrlStore::CompactEncoder do
4
4
  before do
5
- @encoder = UrlStore::CompactEncoder.new('asdasdsa')
5
+ @encoder = UrlStore::CompactEncoder.new(:secret => 'asdasdsa')
6
6
  @data = {:x => 1, 'asdadadadas' => 'asdasdadawvxcxcxcvjs', 'dasdasdadsadad' => 'asdasdwxczvvcjjkdfjkdf'}
7
7
  end
8
8
 
@@ -20,12 +20,12 @@ describe UrlStore::CompactEncoder do
20
20
  end
21
21
 
22
22
  it "can encode/decode with yaml" do
23
- @encoder = UrlStore::CompactEncoder.new('asdasdsa', :serializer => :yaml)
23
+ @encoder = UrlStore::CompactEncoder.new(:secret => 'asdasdsa', :serializer => :yaml)
24
24
  @encoder.decode(@encoder.encode(@data)).should == @data
25
25
  end
26
26
 
27
27
  it "can hash with other hasher" do
28
- @encoder = UrlStore::CompactEncoder.new('asdasdsa', :hasher => 'MD5')
28
+ @encoder = UrlStore::CompactEncoder.new(:secret => 'asdasdsa', :hasher => 'MD5')
29
29
  @encoder.decode(@encoder.encode(@data)).should == @data
30
30
  end
31
31
  end
@@ -3,7 +3,8 @@ require 'cgi'
3
3
 
4
4
  describe UrlStore do
5
5
  before do
6
- UrlStore.secret = 'not the standart sssecrettt1231231áßðáïíœï©óáßïáöððííïö'
6
+ @secret = 'not the standart sssecrettt1231231áßðáïíœï©óáßïáöððííïö'
7
+ UrlStore.defaults = {:secret => @secret}
7
8
  @data = {:x => 11212, :y => 'asdasda sdasdasdASDJKSAJDLSKDLKDS', 'asdasd' => 12312312, 12.12 => 123123212312123, :asdasdasd => '2134 adasdasóáößðóöáåöäóðᜩöóöfóöåäfóöéåfó'}
8
9
  end
9
10
 
@@ -31,13 +32,13 @@ describe UrlStore do
31
32
 
32
33
  it "cannot decode with wrong secret" do
33
34
  encoded = UrlStore.encode(@data)
34
- UrlStore.secret = 'xxx'
35
+ UrlStore.defaults = {:secret => 'xxx'}
35
36
  UrlStore.decode(encoded).should == nil
36
37
  end
37
38
 
38
39
  it "warns when default secret is used" do
39
- UrlStore.secret = UrlStore::SECRET
40
- UrlStore.should_receive(:warn)
40
+ UrlStore.defaults = {:secret => UrlStore::SECRET}
41
+ $stderr.should_receive(:write).at_least(1)
41
42
  UrlStore.encode(1)
42
43
  end
43
44
 
@@ -48,13 +49,13 @@ describe UrlStore do
48
49
 
49
50
  it "can serialize using a different method" do
50
51
  old = UrlStore.encode(@data)
51
- UrlStore.serializer = :yaml
52
+ UrlStore.defaults = {:serializer => :yaml, :secret => @secret}
52
53
  UrlStore.encode(@data).size.should_not == old.size
53
54
  end
54
55
 
55
56
  it "can serialize using different hasher" do
56
57
  old = UrlStore.encode(@data)
57
- UrlStore.hasher = 'MD5'
58
+ UrlStore.defaults = {:hasher => 'MD5', :secret => @secret}
58
59
  UrlStore.encode(@data).size.should_not == old.size
59
60
  end
60
61
 
data/url_store.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{url_store}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2010-01-19}
12
+ s.date = %q{2010-01-20}
13
13
  s.description = %q{Data securely stored in urls.}
14
14
  s.email = %q{grosser.michael@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-19 00:00:00 +01:00
12
+ date: 2010-01-20 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15