shrinker 0.1.0 → 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.
@@ -44,6 +44,12 @@ module Shrinker
44
44
 
45
45
  # setting boolean to replace links only in anchor tags href inside html
46
46
  config_setting :anchors_only_in_html
47
+
48
+ # how long should url tokens be, default 6
49
+ config_setting :token_length_target
50
+
51
+ # how should it generate new tokens: longer (default), random
52
+ config_setting :token_length_strategy
47
53
 
48
54
  def ==(config)
49
55
  self.class.settings.each { |setting| send(setting) == config.send(setting) }
@@ -7,17 +7,34 @@ module Shrinker
7
7
  def fetch_unique_token(backend, options = {})
8
8
  need_token = true
9
9
 
10
+ round = 0
10
11
  while need_token
11
- token = generate(options)
12
+ token = generate(round, options)
12
13
  need_token = backend.used_token?(token)
14
+ round += 1
13
15
  end
14
16
 
15
17
  token
16
18
  end
17
19
 
18
- def generate(options = {})
19
- prefix = options[:prefix]
20
- Digest::MD5.hexdigest("#{prefix}__#{rand(99999)}")[-12..-1]
20
+ def generate(round, options = {})
21
+ length = Shrinker.config.token_length_target || 6
22
+ strategy = Shrinker.config.token_length_strategy || "longer"
23
+
24
+ prefix = options[:prefix].to_s
25
+ if !prefix || prefix.length == 0
26
+ # need soemthing
27
+ prefix = rand(99999999).to_s
28
+ end
29
+
30
+ extra = ""
31
+ if strategy.to_s == "random"
32
+ extra= "__#{rand(99999999999)}"
33
+ else
34
+ length += round
35
+ end
36
+
37
+ Digest::MD5.hexdigest("#{prefix}#{extra}")[-1*length..-1]
21
38
  end
22
39
  end
23
40
  end
@@ -3,7 +3,7 @@ module Shrinker
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- PATCH = 0
6
+ PATCH = 1
7
7
  PRE = nil
8
8
 
9
9
  def self.to_s
@@ -2,16 +2,62 @@ require 'spec_helper'
2
2
 
3
3
  describe Shrinker::Token do
4
4
  describe "#generate" do
5
- it "allows passing a prefix" do
5
+ before do
6
+ Shrinker.config.reset!
6
7
  Shrinker::Token.stub(:rand).and_return('random')
7
- Digest::MD5.should_receive(:hexdigest).with("something__random").and_return("abcdefghijklmnopqrstuvwxyz")
8
+ end
9
+
10
+ after do
11
+ Shrinker.config.reset!
12
+ end
13
+
14
+ it "should use the length config setting" do
15
+ Shrinker.configure do
16
+ token_length_target 4
17
+ end
18
+
19
+ Digest::MD5.should_receive(:hexdigest).with("something").and_return("abcdefghijklmnopqrstuvwxyz")
20
+ Shrinker::Token.generate(0, prefix: 'something').should == "wxyz"
21
+ end
22
+
23
+ it "should use longer than defualt if that is the strategy (default)" do
24
+ Shrinker.configure do
25
+ token_length_target 4
26
+ end
27
+
28
+ Digest::MD5.should_receive(:hexdigest).with("something").and_return("abcdefghijklmnopqrstuvwxyz")
29
+ Shrinker::Token.generate(1, prefix: 'something').should == "vwxyz"
30
+ end
31
+
32
+ it "should default to 6 length" do
33
+ Digest::MD5.should_receive(:hexdigest).with("something").and_return("abcdefghijklmnopqrstuvwxyz")
8
34
 
9
- Shrinker::Token.generate(prefix: 'something').should == "opqrstuvwxyz"
35
+ Shrinker::Token.generate(0, prefix: 'something').should == "uvwxyz"
36
+ end
37
+
38
+ it "should use the random strategy if asked" do
39
+ Shrinker.configure do
40
+ token_length_strategy :random
41
+ token_length_target 6
42
+ end
43
+
44
+ Digest::MD5.should_receive(:hexdigest).with("something__random").and_return("abcdefghijklmnopqrstuvwxyz")
45
+ Shrinker::Token.generate(0, prefix: 'something').should == "uvwxyz"
46
+ end
47
+
48
+ it "should not get longer if strategy random" do
49
+ Shrinker.configure do
50
+ token_length_strategy :random
51
+ token_length_target 6
52
+ end
53
+
54
+ Digest::MD5.should_receive(:hexdigest).with("something__random").and_return("abcdefghijklmnopqrstuvwxyz")
55
+ Shrinker::Token.generate(1, prefix: 'something').should == "uvwxyz"
10
56
  end
11
57
  end
12
58
 
13
59
  describe "#fetch_uniq_token" do
14
- let(:backend) { stub }
60
+ let(:backend) { double }
15
61
 
16
62
  it "returns an unique token" do
17
63
  backend.stub(:used_token?).and_return(true, false)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrinker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: