alnum 0.0.1 → 0.1.0
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.
- checksums.yaml +4 -4
- data/README.md +3 -15
- data/alnum.gemspec +3 -1
- data/lib/alnum.rb +28 -8
- data/lib/alnum/symbols.rb +9 -0
- data/lib/alnum/version.rb +3 -0
- data/test/test_cypher.rb +3 -8
- data/test/test_decipher.rb +15 -20
- metadata +3 -3
- data/lib/alnum/cypher.rb +0 -26
- data/lib/alnum/decipher.rb +0 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c1ea393ce1bb8a32bb43a3065ca50d691cb05024
|
|
4
|
+
data.tar.gz: 02915a7d82a094c5ffb0b8e40d4209c5ad905754
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e29306672a9acacdfbe878ceeefbc9e6c4af8d13c9ce3be9579841954cb60fe55b06ef5ba8d6ea5300b7fbfe03b17f3e30de1d3b1cf88a5f16dfd8133d69a335
|
|
7
|
+
data.tar.gz: bbb2b9cb9c3f30aa3892a2f08af7e15f52f22f338dc12fea842810dcce74de3265fe6e9e997ab1ca5811af8b58dfffb0358ae04d086a00458ed5bcf7b163b78c
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# alnum
|
|
2
2
|
|
|
3
|
-
Alnum is a generator of alphanumeric code from integers and vice-versa.
|
|
3
|
+
Alnum is a generator of alphanumeric code from integers and vice-versa. Inspired in the POSIX character class [:alnum:] it uses just alphanumeric characters accordingly to the pattern [a-zA-Z0-9].
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -12,28 +12,16 @@ Alnum is a generator of alphanumeric code from integers and vice-versa.
|
|
|
12
12
|
|
|
13
13
|
Quick examples to hit the road:
|
|
14
14
|
|
|
15
|
-
### Instantiate your cypher
|
|
16
|
-
|
|
17
|
-
```ruby
|
|
18
|
-
cypher = Alnum::Cypher.new
|
|
19
|
-
```
|
|
20
|
-
|
|
21
15
|
### Get your alphanumeric code
|
|
22
16
|
|
|
23
17
|
```ruby
|
|
24
|
-
cypher
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### Instantiate your decipher
|
|
28
|
-
|
|
29
|
-
```ruby
|
|
30
|
-
decipher = Alnum::Decipher.new
|
|
18
|
+
Alnum::cypher(32456) => "8ru"
|
|
31
19
|
```
|
|
32
20
|
|
|
33
21
|
### Get your integer back
|
|
34
22
|
|
|
35
23
|
```ruby
|
|
36
|
-
|
|
24
|
+
Alnum::decipher("8ru") => 32456
|
|
37
25
|
```
|
|
38
26
|
|
|
39
27
|
## Purpose
|
data/alnum.gemspec
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
require_relative 'lib/alnum/version'
|
|
2
|
+
|
|
1
3
|
Gem::Specification.new do |s|
|
|
2
4
|
s.name = 'alnum'
|
|
3
|
-
s.version =
|
|
5
|
+
s.version = Alnum::VERSION
|
|
4
6
|
s.date = '2015-05-26'
|
|
5
7
|
s.summary = "Alphanumeric code generator."
|
|
6
8
|
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."
|
data/lib/alnum.rb
CHANGED
|
@@ -1,14 +1,34 @@
|
|
|
1
|
-
require_relative 'alnum/
|
|
2
|
-
require_relative 'alnum/decipher'
|
|
1
|
+
require_relative 'alnum/symbols'
|
|
3
2
|
|
|
4
3
|
module Alnum
|
|
5
4
|
|
|
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
5
|
BASE = SYMBOLS.length
|
|
13
6
|
|
|
7
|
+
def self.cypher(number)
|
|
8
|
+
raise TypeError, "Only integers allowed" unless number.is_a? Integer
|
|
9
|
+
|
|
10
|
+
numbers = []
|
|
11
|
+
|
|
12
|
+
until number < BASE do
|
|
13
|
+
remainder = number % BASE
|
|
14
|
+
numbers << SYMBOLS[remainder]
|
|
15
|
+
number = (number - remainder) / BASE
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
numbers << SYMBOLS[number]
|
|
19
|
+
numbers.reverse.join
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.decipher(alphanumeric)
|
|
23
|
+
number, count = 0, 0
|
|
24
|
+
|
|
25
|
+
alphanumeric.to_s.reverse.each_char() do |c|
|
|
26
|
+
raise RangeError, "Code contains characters out of range: '#{c}'" if SYMBOLS.index(c).nil?
|
|
27
|
+
number += SYMBOLS.index(c) * BASE**count
|
|
28
|
+
count += 1
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
number
|
|
32
|
+
end
|
|
33
|
+
|
|
14
34
|
end
|
data/test/test_cypher.rb
CHANGED
|
@@ -1,28 +1,23 @@
|
|
|
1
1
|
require 'test/unit'
|
|
2
|
-
require_relative '../lib/alnum/cypher'
|
|
3
2
|
require_relative './support/test_data'
|
|
4
3
|
|
|
5
4
|
class TestCypher < Test::Unit::TestCase
|
|
6
5
|
|
|
7
|
-
def setup
|
|
8
|
-
@cypher = Alnum::Cypher.new
|
|
9
|
-
end
|
|
10
|
-
|
|
11
6
|
def test_one_character_range
|
|
12
7
|
62.times do |number|
|
|
13
|
-
assert_equal
|
|
8
|
+
assert_equal Alnum::cypher(number), "#{SYMBOLS[number]}"
|
|
14
9
|
end
|
|
15
10
|
end
|
|
16
11
|
|
|
17
12
|
def test_only_integers_allowed
|
|
18
13
|
assert_raise TypeError do
|
|
19
|
-
|
|
14
|
+
Alnum::cypher('a')
|
|
20
15
|
end
|
|
21
16
|
end
|
|
22
17
|
|
|
23
18
|
def test_empty_arguments
|
|
24
19
|
assert_raise ArgumentError do
|
|
25
|
-
|
|
20
|
+
Alnum::cypher()
|
|
26
21
|
end
|
|
27
22
|
end
|
|
28
23
|
|
data/test/test_decipher.rb
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
1
|
require 'test/unit'
|
|
2
|
-
require_relative '../lib/alnum/decipher'
|
|
3
2
|
require_relative './support/test_data'
|
|
4
3
|
|
|
5
4
|
class TestDecipher < Test::Unit::TestCase
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
def test_one_character_range
|
|
7
|
+
62.times do |number|
|
|
8
|
+
assert_equal Alnum::decipher(SYMBOLS[number]), number
|
|
9
|
+
end
|
|
10
|
+
end
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
def test_character_out_of_range
|
|
13
|
+
assert_raise RangeError do
|
|
14
|
+
Alnum::decipher('-')
|
|
15
|
+
end
|
|
16
|
+
end
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def test_empty_arguments
|
|
24
|
-
assert_raise ArgumentError do
|
|
25
|
-
@decipher.read()
|
|
26
|
-
end
|
|
27
|
-
end
|
|
18
|
+
def test_empty_arguments
|
|
19
|
+
assert_raise ArgumentError do
|
|
20
|
+
Alnum::decipher()
|
|
21
|
+
end
|
|
22
|
+
end
|
|
28
23
|
|
|
29
24
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: alnum
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Hanes Barbosa
|
|
@@ -21,8 +21,8 @@ files:
|
|
|
21
21
|
- README.md
|
|
22
22
|
- alnum.gemspec
|
|
23
23
|
- lib/alnum.rb
|
|
24
|
-
- lib/alnum/
|
|
25
|
-
- lib/alnum/
|
|
24
|
+
- lib/alnum/symbols.rb
|
|
25
|
+
- lib/alnum/version.rb
|
|
26
26
|
- test/support/test_data.rb
|
|
27
27
|
- test/test_alnum.rb
|
|
28
28
|
- test/test_cypher.rb
|
data/lib/alnum/cypher.rb
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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
|
data/lib/alnum/decipher.rb
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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
|