short_url_token_generator 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +18 -3
- data/lib/short_url_token_generator.rb +14 -5
- data/short_url_token_generator.gemspec +1 -1
- data/spec/short_url_token_generator_spec.rb +9 -10
- metadata +2 -2
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Install
|
|
7
7
|
-------
|
8
8
|
|
9
9
|
Just add this to your Gemfile:
|
10
|
-
`gem 'short_url_token_generator'`
|
10
|
+
`gem 'short_url_token_generator'`
|
11
11
|
|
12
12
|
And run your bundler:
|
13
13
|
`bundle install`
|
@@ -15,7 +15,13 @@ And run your bundler:
|
|
15
15
|
Usage
|
16
16
|
-----
|
17
17
|
|
18
|
-
|
18
|
+
Generate
|
19
|
+
--------
|
20
|
+
ShortUrlTokenGenerator.generate(134) # => "Cp"
|
21
|
+
|
22
|
+
Decode
|
23
|
+
------
|
24
|
+
ShortUrlTokenGenerator.decode("Cp") # => 134
|
19
25
|
|
20
26
|
Example
|
21
27
|
-------
|
@@ -30,7 +36,7 @@ Example
|
|
30
36
|
end
|
31
37
|
private
|
32
38
|
def generate_token num
|
33
|
-
token = ShortUrlTokenGenerator
|
39
|
+
token = ShortUrlTokenGenerator.generate num
|
34
40
|
if Url.count(conditions: {:token => token}) > 0
|
35
41
|
token = generate_token num + 1
|
36
42
|
end
|
@@ -38,6 +44,15 @@ Example
|
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
47
|
+
Changelog
|
48
|
+
-------
|
49
|
+
|
50
|
+
* 0.2.0
|
51
|
+
Migrated from Module to Class, because use Module don't make sense.
|
52
|
+
|
53
|
+
* 0.1.0
|
54
|
+
Released firt version
|
55
|
+
|
41
56
|
License
|
42
57
|
-------
|
43
58
|
|
@@ -1,10 +1,22 @@
|
|
1
|
-
|
1
|
+
class ShortUrlTokenGenerator
|
2
|
+
# Mapping of string to use for generate tokens
|
3
|
+
@mapping = :oJC8RZuYg2pTrAIkjWHN1nxQ3c4yVKP5X9LBbaisDdfqv6heU7zGFSOlEw0mMt
|
4
|
+
|
5
|
+
# Get mapping
|
6
|
+
def self.mapping
|
7
|
+
@mapping.to_s.split //
|
8
|
+
end
|
9
|
+
|
10
|
+
# Generate token (using alpha-numeric mapping)
|
11
|
+
# Param: num (index of the URL)
|
2
12
|
def self.generate num
|
3
13
|
token = ""
|
4
14
|
base_notation(num).split('-').each { |position| token += "#{mapping[position.to_i]}" }
|
5
15
|
token
|
6
16
|
end
|
7
17
|
|
18
|
+
# Decode token (for correct decoding, the token must be generated using this class)
|
19
|
+
# Param: token
|
8
20
|
def self.decode token
|
9
21
|
return -1 if token.length > 15
|
10
22
|
num = i = 0
|
@@ -15,12 +27,9 @@ module ShortUrlTokenGenerator
|
|
15
27
|
num
|
16
28
|
end
|
17
29
|
|
30
|
+
# Calculate base notation
|
18
31
|
def self.base_notation num, base = nil
|
19
32
|
base = mapping.length if base.nil?
|
20
33
|
num < base ? "#{num}" : "#{base_notation (num/base).floor, base}-#{num % base}"
|
21
34
|
end
|
22
|
-
|
23
|
-
def self.mapping
|
24
|
-
'oJC8RZuYg2pTrAIkjWHN1nxQ3c4yVKP5X9LBbaisDdfqv6heU7zGFSOlEw0mMt'.chars.to_a
|
25
|
-
end
|
26
35
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "short_url_token_generator"
|
3
|
-
s.version = "0.
|
3
|
+
s.version = "0.2.0"
|
4
4
|
s.description = "Short url token generator, use to generate/decode tokens for urls"
|
5
5
|
s.summary = "Short url token generator"
|
6
6
|
s.author = "Josemar Davi Luedke"
|
@@ -1,48 +1,47 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ShortUrlTokenGenerator do
|
4
|
-
|
5
4
|
describe 'mapping' do
|
6
5
|
it "should be array" do
|
7
|
-
ShortUrlTokenGenerator
|
6
|
+
ShortUrlTokenGenerator.mapping.should be_instance_of(Array)
|
8
7
|
end
|
9
8
|
end
|
10
9
|
|
11
10
|
describe 'base notation' do
|
12
11
|
it 'should be return 10' do
|
13
|
-
ShortUrlTokenGenerator
|
12
|
+
ShortUrlTokenGenerator.base_notation(10).should eq "10"
|
14
13
|
end
|
15
14
|
|
16
15
|
it 'should be return 1-0' do
|
17
|
-
ShortUrlTokenGenerator
|
16
|
+
ShortUrlTokenGenerator.base_notation(62).should eq "1-0"
|
18
17
|
end
|
19
18
|
|
20
19
|
it 'should be return 1-1' do
|
21
|
-
ShortUrlTokenGenerator
|
20
|
+
ShortUrlTokenGenerator.base_notation(63).should eq "1-1"
|
22
21
|
end
|
23
22
|
|
24
23
|
it 'should be return 2-2' do
|
25
|
-
ShortUrlTokenGenerator
|
24
|
+
ShortUrlTokenGenerator.base_notation(26, 12).should eq "2-2"
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
28
|
describe 'generate' do
|
30
29
|
it "should be return the first character" do
|
31
|
-
ShortUrlTokenGenerator
|
30
|
+
ShortUrlTokenGenerator.generate(0).should eq ShortUrlTokenGenerator.mapping[0]
|
32
31
|
end
|
33
32
|
|
34
33
|
it "should be return the correct characters" do
|
35
|
-
ShortUrlTokenGenerator
|
34
|
+
ShortUrlTokenGenerator.generate(134).should eq "#{ShortUrlTokenGenerator.mapping[2]}#{ShortUrlTokenGenerator.mapping[10]}"
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
39
38
|
describe "decode" do
|
40
39
|
it 'should be return correct num of token' do
|
41
|
-
ShortUrlTokenGenerator
|
40
|
+
ShortUrlTokenGenerator.decode("#{ShortUrlTokenGenerator.mapping[2]}#{ShortUrlTokenGenerator.mapping[10]}").should eq 134
|
42
41
|
end
|
43
42
|
|
44
43
|
it 'should be return -1, because token is > 15' do
|
45
|
-
ShortUrlTokenGenerator
|
44
|
+
ShortUrlTokenGenerator.decode("oJC8RZuYg2pTrAIK").should eq -1
|
46
45
|
end
|
47
46
|
end
|
48
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: short_url_token_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-07 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Short url token generator, use to generate/decode tokens for urls
|
15
15
|
email: josemarluedke@gmail.com
|