alnum 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +80 -0
- data/alnum.gemspec +17 -0
- data/lib/alnum/cypher.rb +26 -0
- data/lib/alnum/decipher.rb +22 -0
- data/lib/alnum.rb +14 -0
- data/test/support/test_data.rb +5 -0
- data/test/test_alnum.rb +15 -0
- data/test/test_cypher.rb +29 -0
- data/test/test_decipher.rb +29 -0
- data/test/test_suite_all.rb +3 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 540fcbabc9ec1452ffe147f018924f7594511c84
|
4
|
+
data.tar.gz: ffc28febfd232baa286f2a70027751755c5a9163
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 543ec3b8eeea6fca40eb8ad85844635295db713b02f16efbaed9ad51b9407ed99d64184c515fb5ec42c2eb6242d4674b0aa7d4d5240ccda0e63517ae4da9cea7
|
7
|
+
data.tar.gz: 2c201eb26e79a302d369ff96731c7b18c244b1d36a7d939dc99640fdb744fd90aea2d3e8b25a4fab013d8d39d1a3ff5b228118590394ed234851484f13a921a1
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Hanes Barbosa Marques de Oliveira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# alnum
|
2
|
+
|
3
|
+
Alnum is a generator of alphanumeric code from integers and vice-versa.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
[sudo] gem install alnum
|
9
|
+
```
|
10
|
+
|
11
|
+
## The Long Story Short
|
12
|
+
|
13
|
+
Quick examples to hit the road:
|
14
|
+
|
15
|
+
### Instantiate your cypher
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
cypher = Alnum::Cypher.new
|
19
|
+
```
|
20
|
+
|
21
|
+
### Get your alphanumeric code
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
cypher.write(32456) => "8ru"
|
25
|
+
```
|
26
|
+
|
27
|
+
### Instantiate your decipher
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
decipher = Alnum::Decipher.new
|
31
|
+
```
|
32
|
+
|
33
|
+
### Get your integer back
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
decipher.read("8ru") => 32456
|
37
|
+
```
|
38
|
+
|
39
|
+
## Purpose
|
40
|
+
|
41
|
+
This gem was created to serve as a parser that can generates alphanumeric tokens as a resultant conversion of integer numbers. It can work backwards too, converting alphanumeric codes back to the previous integer representation. You can only decipher a code that was created by this same module.
|
42
|
+
|
43
|
+
This can be used for any kind of tokens, but mainly for shortening URIs or URLs.
|
44
|
+
|
45
|
+
## URL Shorteners
|
46
|
+
|
47
|
+
When you're using URL shorteners, you are switching your link by an alphanumeric token. The short or tiny URLs are abstrations of the real ones. The original link is stored in some database and you can use the tiny generated code to share among all those social media out there.
|
48
|
+
|
49
|
+
Systems can store large URLs in databases and using the ID key generated (integer) can create a short URL. So you can append this little part to your domain like this:
|
50
|
+
|
51
|
+
http://www.yourdomain.com/token
|
52
|
+
|
53
|
+
The module here doesn’t create the hole functionality, just give you methods for the parsing. This way you’re not bounded to the developer’s perception on how your framework should behave across classes, controllers, views, etc.
|
54
|
+
|
55
|
+
## Background
|
56
|
+
|
57
|
+
According to the HTTP protocol, an URI can be depicted as follows:
|
58
|
+
|
59
|
+
scheme://authority/path?query#fragment
|
60
|
+
|
61
|
+
As an example we can figure out how it appears in the real world:
|
62
|
+
|
63
|
+
http://github.com/search?utf8=✓&q=gemname
|
64
|
+
|
65
|
+
In the example above there’s no fragment, but you got the mood.
|
66
|
+
|
67
|
+
As a intended shortener functionality, this code focus on the URI part called “path”. So we need to investigate the characters allowed to be part of this particular slice of the resource.
|
68
|
+
|
69
|
+
You can restrict the outcome to just alphanumeric characters or unleash the full amount of symbols in order to have even more small tokens. However, taking punctuation characters or such can result in more gibberished words.
|
70
|
+
|
71
|
+
At a glance, when we take a look at the rules applied to the path part we can consider that:
|
72
|
+
|
73
|
+
* If the path does not begins with a slash (“/”) character, it should be empty (that’s not our case).
|
74
|
+
* The path is terminated by the first question mark ("?") – the <query> portion, the number sign ("#") character – the <fragment> – or by the end of the URI.
|
75
|
+
|
76
|
+
Path segments like “.” and “..” and so forth, has special meanings, so you should take care of it in your application or restrict your characters just to alphanumeric when parsing.
|
77
|
+
|
78
|
+
## Conclusion
|
79
|
+
|
80
|
+
The code here just give you symbols from the alphabet and digits. You can expand the classes in differents flavors as you wish.
|
data/alnum.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'alnum'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2015-05-26'
|
5
|
+
s.summary = "Alphanumeric code generator."
|
6
|
+
s.description = "Alnum is a generator of alphanumeric code from integers and vice-versa. Can be useful shortening ID numbers in order to create tiny or short URLs."
|
7
|
+
s.authors = ["Hanes Barbosa"]
|
8
|
+
s.email = 'hanes.barbosa@gmail.com'
|
9
|
+
s.homepage = 'http://github.com/hanesbarbosa/alnum'
|
10
|
+
s.license = 'MIT'
|
11
|
+
s.post_install_message = "Let's cypher some code!"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.required_ruby_version = '>= 1.9.3'
|
17
|
+
end
|
data/lib/alnum/cypher.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../alnum'
|
2
|
+
|
3
|
+
module Alnum
|
4
|
+
|
5
|
+
class Cypher
|
6
|
+
|
7
|
+
def write(number)
|
8
|
+
raise TypeError, "Only integers allowed" unless number.is_a? Integer
|
9
|
+
|
10
|
+
numbers = []
|
11
|
+
quotient = 0
|
12
|
+
|
13
|
+
until number < BASE do
|
14
|
+
rest = number % BASE
|
15
|
+
quotient = (number - rest) / BASE
|
16
|
+
numbers << SYMBOLS[rest]
|
17
|
+
number = quotient
|
18
|
+
end
|
19
|
+
|
20
|
+
numbers << SYMBOLS[number]
|
21
|
+
numbers.reverse.join
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../alnum'
|
2
|
+
|
3
|
+
module Alnum
|
4
|
+
|
5
|
+
class Decipher
|
6
|
+
|
7
|
+
def read(alphanumeric)
|
8
|
+
number = 0
|
9
|
+
count = 0
|
10
|
+
|
11
|
+
alphanumeric.to_s.reverse.each_char() do |c|
|
12
|
+
raise RangeError, "Code contains characters out of range: '#{c}'" if SYMBOLS.index(c).nil?
|
13
|
+
number += SYMBOLS.index(c) * BASE**count
|
14
|
+
count += 1
|
15
|
+
end
|
16
|
+
|
17
|
+
number
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/alnum.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative 'alnum/cypher'
|
2
|
+
require_relative 'alnum/decipher'
|
3
|
+
|
4
|
+
module Alnum
|
5
|
+
|
6
|
+
SYMBOLS = %w{
|
7
|
+
0 1 2 3 4 5 6 7 8 9
|
8
|
+
a b c d e f g h i j k l m n o p q r s t u v w x y z
|
9
|
+
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
10
|
+
}
|
11
|
+
|
12
|
+
BASE = SYMBOLS.length
|
13
|
+
|
14
|
+
end
|
data/test/test_alnum.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/alnum'
|
3
|
+
require_relative './support/test_data'
|
4
|
+
|
5
|
+
class TestAlnum < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_amount_of_symbols
|
8
|
+
assert_equal Alnum::SYMBOLS.length, SYMBOLS.length
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_symbols_should_be_the_same
|
12
|
+
assert_equal Alnum::SYMBOLS, SYMBOLS
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/test/test_cypher.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/alnum/cypher'
|
3
|
+
require_relative './support/test_data'
|
4
|
+
|
5
|
+
class TestCypher < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@cypher = Alnum::Cypher.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_one_character_range
|
12
|
+
62.times do |number|
|
13
|
+
assert_equal @cypher.write(number), "#{SYMBOLS[number]}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_only_integers_allowed
|
18
|
+
assert_raise TypeError do
|
19
|
+
@cypher.write('a')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_empty_arguments
|
24
|
+
assert_raise ArgumentError do
|
25
|
+
@cypher.write()
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/alnum/decipher'
|
3
|
+
require_relative './support/test_data'
|
4
|
+
|
5
|
+
class TestDecipher < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@decipher = Alnum::Decipher.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_one_character_range
|
12
|
+
62.times do |number|
|
13
|
+
assert_equal @decipher.read(SYMBOLS[number]), number
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_character_out_of_range
|
18
|
+
assert_raise RangeError do
|
19
|
+
@decipher.read('-')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_empty_arguments
|
24
|
+
assert_raise ArgumentError do
|
25
|
+
@decipher.read()
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alnum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hanes Barbosa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Alnum is a generator of alphanumeric code from integers and vice-versa.
|
14
|
+
Can be useful shortening ID numbers in order to create tiny or short URLs.
|
15
|
+
email: hanes.barbosa@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- alnum.gemspec
|
23
|
+
- lib/alnum.rb
|
24
|
+
- lib/alnum/cypher.rb
|
25
|
+
- lib/alnum/decipher.rb
|
26
|
+
- test/support/test_data.rb
|
27
|
+
- test/test_alnum.rb
|
28
|
+
- test/test_cypher.rb
|
29
|
+
- test/test_decipher.rb
|
30
|
+
- test/test_suite_all.rb
|
31
|
+
homepage: http://github.com/hanesbarbosa/alnum
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata: {}
|
35
|
+
post_install_message: Let's cypher some code!
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.9.3
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 2.4.6
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: Alphanumeric code generator.
|
55
|
+
test_files:
|
56
|
+
- test/support/test_data.rb
|
57
|
+
- test/test_alnum.rb
|
58
|
+
- test/test_cypher.rb
|
59
|
+
- test/test_decipher.rb
|
60
|
+
- test/test_suite_all.rb
|