id_encoder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +58 -0
- data/Rakefile +2 -0
- data/id_encoder.gemspec +23 -0
- data/lib/id_encoder.rb +125 -0
- data/lib/id_encoder/version.rb +3 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# IdEncoder
|
2
|
+
|
3
|
+
Ruby implementation for generating Tiny URL- and bit.ly-like URLs.
|
4
|
+
|
5
|
+
A bit-shuffling approach is used to avoid generating consecutive, predictable
|
6
|
+
URLs. However, the algorithm is deterministic and will guarantee that no
|
7
|
+
collisions will occur.
|
8
|
+
|
9
|
+
The URL alphabet is fully customizable and may contain any number of
|
10
|
+
characters. By default, digits and lower-case letters are used, with
|
11
|
+
some removed to avoid confusion between characters like o, O and 0. The
|
12
|
+
default alphabet is shuffled and has a prime number of characters to further
|
13
|
+
improve the results of the algorithm.
|
14
|
+
|
15
|
+
The block size specifies how many bits will be shuffled. The lower BLOCK_SIZE
|
16
|
+
bits are reversed. Any bits higher than BLOCK_SIZE will remain as is.
|
17
|
+
BLOCK_SIZE of 0 will leave all bits unaffected and the algorithm will simply
|
18
|
+
be converting your integer to a different base.
|
19
|
+
|
20
|
+
The intended use is that incrementing, consecutive integers will be used as
|
21
|
+
keys to generate the short URLs. For example, when creating a new URL, the
|
22
|
+
unique integer ID assigned by a database could be used to generate the URL
|
23
|
+
by using this module. Or a simple counter may be used. As long as the same
|
24
|
+
integer is not used twice, the same short URL will not be generated twice.
|
25
|
+
|
26
|
+
The module supports both encoding and decoding of URLs. The min_length
|
27
|
+
parameter allows you to pad the URL if you want it to be a specific length.
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
gem 'id_encoder'
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install id_encoder
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
>> require 'id_encoder'
|
46
|
+
=> true
|
47
|
+
>> IdEncoder::UrlEncoder.encode_url(10)
|
48
|
+
=> "csqsc"
|
49
|
+
>> IdEncoder::UrlEncoder.decode_url('csqsc')
|
50
|
+
=> 10
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/id_encoder.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/id_encoder/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ruslan Khakimov"]
|
6
|
+
gem.email = ["ruslan@khakimov.com"]
|
7
|
+
gem.description = %q{A bit-shuffling approach is used to avoid generating
|
8
|
+
consecutive, predictable URLs. However, the algorithm is deterministic and
|
9
|
+
will guarantee that no collisions will occur.
|
10
|
+
|
11
|
+
The gem supports both encoding and decoding of URLs. The min_length parameter
|
12
|
+
allows you to pad the URL if you want it to be a specific length.
|
13
|
+
}
|
14
|
+
gem.summary = %q{Ruby implementation for generating Tiny URL- and bit.ly-like URLs.}
|
15
|
+
gem.homepage = ""
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($\)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.name = "id_encoder"
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
gem.version = IdEncoder::VERSION
|
23
|
+
end
|
data/lib/id_encoder.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# Short URL Generator
|
2
|
+
# ===================
|
3
|
+
|
4
|
+
# Ruby implementation for generating Tiny URL- and bit.ly-like URLs.
|
5
|
+
|
6
|
+
# A bit-shuffling approach is used to avoid generating consecutive, predictable
|
7
|
+
# URLs. However, the algorithm is deterministic and will guarantee that no
|
8
|
+
# collisions will occur.
|
9
|
+
|
10
|
+
# The URL alphabet is fully customizable and may contain any number of
|
11
|
+
# characters. By default, digits and lower-case letters are used, with
|
12
|
+
# some removed to avoid confusion between characters like o, O and 0. The
|
13
|
+
# default alphabet is shuffled and has a prime number of characters to further
|
14
|
+
# improve the results of the algorithm.
|
15
|
+
|
16
|
+
# The block size specifies how many bits will be shuffled. The lower BLOCK_SIZE
|
17
|
+
# bits are reversed. Any bits higher than BLOCK_SIZE will remain as is.
|
18
|
+
# BLOCK_SIZE of 0 will leave all bits unaffected and the algorithm will simply
|
19
|
+
# be converting your integer to a different base.
|
20
|
+
|
21
|
+
# The intended use is that incrementing, consecutive integers will be used as
|
22
|
+
# keys to generate the short URLs. For example, when creating a new URL, the
|
23
|
+
# unique integer ID assigned by a database could be used to generate the URL
|
24
|
+
# by using this module. Or a simple counter may be used. As long as the same
|
25
|
+
# integer is not used twice, the same short URL will not be generated twice.
|
26
|
+
|
27
|
+
# The module supports both encoding and decoding of URLs. The min_length
|
28
|
+
# parameter allows you to pad the URL if you want it to be a specific length.
|
29
|
+
|
30
|
+
# Sample Usage:
|
31
|
+
#
|
32
|
+
# gem install id_encoder
|
33
|
+
#
|
34
|
+
# >> require 'id_encoder'
|
35
|
+
# => true
|
36
|
+
# >> IdEncoder::UrlEncoder.encode_url(10)
|
37
|
+
# => "csqsc"
|
38
|
+
# >> IdEncoder::UrlEncoder.decode_url('csqsc')
|
39
|
+
# => 10
|
40
|
+
#
|
41
|
+
# Use the functions in the top-level of the module to use the default encoder.
|
42
|
+
# Otherwise, you may create your own UrlEncoder object and use its encode_url
|
43
|
+
# and decode_url methods.
|
44
|
+
|
45
|
+
# Author of Python version: Michael Fogleman
|
46
|
+
# Link: http://code.activestate.com/recipes/576918/
|
47
|
+
# License: MIT
|
48
|
+
|
49
|
+
# Author of Ruby version: Ruslan Khakimov
|
50
|
+
# Link: http://github.com/khakimov/id_encoder
|
51
|
+
# License: MIT
|
52
|
+
|
53
|
+
require "id_encoder/version"
|
54
|
+
|
55
|
+
DEFAULT_ALPHABET = 'mn6j2c4rv8bpygw95z7hsdaetxuk3fq'
|
56
|
+
DEFAULT_BLOCK_SIZE = 24
|
57
|
+
MIN_LENGTH = 4
|
58
|
+
|
59
|
+
module IdEncoder
|
60
|
+
class UrlEncoder
|
61
|
+
|
62
|
+
@alphabet = DEFAULT_ALPHABET
|
63
|
+
@block_size = DEFAULT_BLOCK_SIZE
|
64
|
+
@mask = (1 << @block_size) - 1 # left-shift
|
65
|
+
@mapping = (0..(@block_size-1)).to_a.reverse!
|
66
|
+
|
67
|
+
def self.encode_url(n, min_length=MIN_LENGTH)
|
68
|
+
enbase(encode(n), min_length)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.decode_url(n)
|
72
|
+
decode(debase(n))
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.encode(n)
|
76
|
+
(n & ~@mask) | _encode(n & @mask)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.decode(n)
|
80
|
+
(n & ~@mask) | _decode(n & @mask)
|
81
|
+
end
|
82
|
+
|
83
|
+
def self._encode(n)
|
84
|
+
result = 0
|
85
|
+
@mapping.each_with_index { |item, index|
|
86
|
+
if n & (1 << index) != 0
|
87
|
+
result = result | (1 << item)
|
88
|
+
end
|
89
|
+
}
|
90
|
+
result
|
91
|
+
end
|
92
|
+
|
93
|
+
def self._decode(n)
|
94
|
+
result = 0
|
95
|
+
@mapping.each_with_index { |item, index|
|
96
|
+
if n & (1 << item) != 0
|
97
|
+
result = result | (1 << index)
|
98
|
+
end
|
99
|
+
}
|
100
|
+
result
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.enbase(x, min_length=MIN_LENGTH)
|
104
|
+
_enbase(x)
|
105
|
+
end
|
106
|
+
|
107
|
+
def self._enbase(x)
|
108
|
+
n = @alphabet.size
|
109
|
+
if x < n
|
110
|
+
return @alphabet[x]
|
111
|
+
end
|
112
|
+
_enbase(x/n) + @alphabet[x % n]
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.debase(x)
|
116
|
+
n = @alphabet.size
|
117
|
+
result = 0
|
118
|
+
a = x.split('').reverse
|
119
|
+
a.each_with_index {|item, i|
|
120
|
+
result += @alphabet.index(item) * (n ** i)
|
121
|
+
}
|
122
|
+
result
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: id_encoder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ruslan Khakimov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "A bit-shuffling approach is used to avoid generating \n consecutive,
|
15
|
+
predictable URLs. However, the algorithm is deterministic and \n will guarantee
|
16
|
+
that no collisions will occur.\n \n The gem supports both encoding and decoding
|
17
|
+
of URLs. The min_length parameter \n allows you to pad the URL if you want it
|
18
|
+
to be a specific length.\n "
|
19
|
+
email:
|
20
|
+
- ruslan@khakimov.com
|
21
|
+
executables: []
|
22
|
+
extensions: []
|
23
|
+
extra_rdoc_files: []
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- id_encoder.gemspec
|
31
|
+
- lib/id_encoder.rb
|
32
|
+
- lib/id_encoder/version.rb
|
33
|
+
homepage: ''
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.24
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Ruby implementation for generating Tiny URL- and bit.ly-like URLs.
|
57
|
+
test_files: []
|