short_url_token_generator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +44 -0
- data/lib/short_url_token_generator.rb +26 -0
- data/short_url_token_generator.gemspec +10 -0
- data/spec/short_url_token_generator_spec.rb +48 -0
- data/spec/spec_helper.rb +2 -0
- metadata +49 -0
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
Short url token generator
|
2
|
+
=========================
|
3
|
+
|
4
|
+
Use to generate/decode tokens for urls
|
5
|
+
|
6
|
+
Install
|
7
|
+
-------
|
8
|
+
|
9
|
+
Just add this to your Gemfile:
|
10
|
+
`gem 'short_url_token_generator'` (not yet published in RubyGems.org)
|
11
|
+
|
12
|
+
And run your bundler:
|
13
|
+
`bundle install`
|
14
|
+
|
15
|
+
Usage
|
16
|
+
-----
|
17
|
+
|
18
|
+
pending
|
19
|
+
|
20
|
+
Example
|
21
|
+
-------
|
22
|
+
|
23
|
+
class Url < ActiveRecord::Base
|
24
|
+
before_create do
|
25
|
+
if self.token.nil? or self.token.empty?
|
26
|
+
self.token = generate_token Url.count(conditions: {:custom => false})
|
27
|
+
else
|
28
|
+
self.custom = true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
private
|
32
|
+
def generate_token num
|
33
|
+
token = ShortUrlTokenGenerator::generate num
|
34
|
+
if Url.count(conditions: {:token => token}) > 0
|
35
|
+
token = generate_token num + 1
|
36
|
+
end
|
37
|
+
token
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
License
|
42
|
+
-------
|
43
|
+
|
44
|
+
Copyright (C) 2012 Josemar Davi Luedke <josemarluedke@gmail.com>, released under the MIT license.
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ShortUrlTokenGenerator
|
2
|
+
def self.generate num
|
3
|
+
token = ""
|
4
|
+
base_notation(num).split('-').each { |position| token += "#{mapping[position.to_i]}" }
|
5
|
+
token
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.decode token
|
9
|
+
return -1 if token.length > 15
|
10
|
+
num = i = 0
|
11
|
+
token.chars.to_a.reverse_each do |t|
|
12
|
+
num += mapping.index(t) * mapping.length**i unless mapping.index(t).nil?
|
13
|
+
i += 1
|
14
|
+
end
|
15
|
+
num
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.base_notation num, base = nil
|
19
|
+
base = mapping.length if base.nil?
|
20
|
+
num < base ? "#{num}" : "#{base_notation (num/base).floor, base}-#{num % base}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.mapping
|
24
|
+
'oJC8RZuYg2pTrAIkjWHN1nxQ3c4yVKP5X9LBbaisDdfqv6heU7zGFSOlEw0mMt'.chars.to_a
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "short_url_token_generator"
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.description = "Short url token generator, use to generate/decode tokens for urls"
|
5
|
+
s.summary = "Short url token generator"
|
6
|
+
s.author = "Josemar Davi Luedke"
|
7
|
+
s.email = "josemarluedke@gmail.com"
|
8
|
+
s.homepage = "https://github.com/josemarluedke/short_url_token_generator"
|
9
|
+
s.files = Dir["{lib/*.rb,README.md,spec/*.rb,*.gemspec}"]
|
10
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ShortUrlTokenGenerator do
|
4
|
+
|
5
|
+
describe 'mapping' do
|
6
|
+
it "should be array" do
|
7
|
+
ShortUrlTokenGenerator::mapping.should be_instance_of(Array)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'base notation' do
|
12
|
+
it 'should be return 10' do
|
13
|
+
ShortUrlTokenGenerator::base_notation(10).should eq "10"
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should be return 1-0' do
|
17
|
+
ShortUrlTokenGenerator::base_notation(62).should eq "1-0"
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should be return 1-1' do
|
21
|
+
ShortUrlTokenGenerator::base_notation(63).should eq "1-1"
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should be return 2-2' do
|
25
|
+
ShortUrlTokenGenerator::base_notation(26, 12).should eq "2-2"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'generate' do
|
30
|
+
it "should be return the first character" do
|
31
|
+
ShortUrlTokenGenerator::generate(0).should eq ShortUrlTokenGenerator::mapping[0]
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be return the correct characters" do
|
35
|
+
ShortUrlTokenGenerator::generate(134).should eq "#{ShortUrlTokenGenerator::mapping[2]}#{ShortUrlTokenGenerator::mapping[10]}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "decode" do
|
40
|
+
it 'should be return correct num of token' do
|
41
|
+
ShortUrlTokenGenerator::decode("#{ShortUrlTokenGenerator::mapping[2]}#{ShortUrlTokenGenerator::mapping[10]}").should eq 134
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should be return -1, because token is > 15' do
|
45
|
+
ShortUrlTokenGenerator::decode("oJC8RZuYg2pTrAIK").should eq -1
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: short_url_token_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josemar Davi Luedke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-18 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Short url token generator, use to generate/decode tokens for urls
|
15
|
+
email: josemarluedke@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/short_url_token_generator.rb
|
21
|
+
- README.md
|
22
|
+
- spec/short_url_token_generator_spec.rb
|
23
|
+
- spec/spec_helper.rb
|
24
|
+
- short_url_token_generator.gemspec
|
25
|
+
homepage: https://github.com/josemarluedke/short_url_token_generator
|
26
|
+
licenses: []
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.10
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: Short url token generator
|
49
|
+
test_files: []
|