friendly_digest 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +1 -0
- data/Manifest +1 -0
- data/README +25 -0
- data/friendly_digest.gemspec +4 -3
- data/lib/friendly_digest.rb +20 -4
- data/test/test_all.rb +20 -0
- metadata +6 -5
data/CHANGELOG
CHANGED
data/Manifest
CHANGED
data/README
CHANGED
@@ -16,3 +16,28 @@ gem install 'friendly_digest'
|
|
16
16
|
=> 12345
|
17
17
|
|
18
18
|
|
19
|
+
|
20
|
+
New! Parity flag
|
21
|
+
----------------
|
22
|
+
|
23
|
+
Adds a 1 character checksum to detect input mistakes.
|
24
|
+
|
25
|
+
irb> FriendlyDigest.new(12345, true)
|
26
|
+
=> "4ADM"
|
27
|
+
|
28
|
+
irb> FriendlyDigest.decode('4adm', true)
|
29
|
+
=> 12345
|
30
|
+
|
31
|
+
irb> FriendlyDigest.decode('4abm', true)
|
32
|
+
FriendlyDigest::InvalidInputException: Invalid input
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
Contact the author
|
38
|
+
------------------
|
39
|
+
|
40
|
+
Andrew Snow <andrew@modulus.org>
|
41
|
+
Andys^ on irc.freenode.net
|
42
|
+
@andy_snow on the twitter
|
43
|
+
|
data/friendly_digest.gemspec
CHANGED
@@ -2,21 +2,22 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{friendly_digest}
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Andrew Snow"]
|
9
|
-
s.date = %q{
|
9
|
+
s.date = %q{2011-02-23}
|
10
10
|
s.description = %q{Ruby gem to send to convert integer IDs into easy to type strings}
|
11
11
|
s.email = %q{andrew@modulus.org}
|
12
12
|
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/friendly_digest.rb"]
|
13
|
-
s.files = ["CHANGELOG", "LICENSE", "Manifest", "README", "Rakefile", "lib/friendly_digest.rb", "friendly_digest.gemspec"]
|
13
|
+
s.files = ["CHANGELOG", "LICENSE", "Manifest", "README", "Rakefile", "lib/friendly_digest.rb", "test/test_all.rb", "friendly_digest.gemspec"]
|
14
14
|
s.homepage = %q{}
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Friendly_digest", "--main", "README"]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
s.rubyforge_project = %q{friendly_digest}
|
18
18
|
s.rubygems_version = %q{1.3.7}
|
19
19
|
s.summary = %q{Ruby gem to send to convert integer IDs into easy to type strings}
|
20
|
+
s.test_files = ["test/test_all.rb"]
|
20
21
|
|
21
22
|
if s.respond_to? :specification_version then
|
22
23
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/lib/friendly_digest.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
class FriendlyDigest < String
|
3
|
+
class InvalidInputException < Exception ; end
|
3
4
|
class << self
|
4
5
|
def [](*p) ; new(*p) ; end
|
5
6
|
|
@@ -7,18 +8,33 @@ class FriendlyDigest < String
|
|
7
8
|
@codes ||= ['Z','2','B','T','C','9','H','Q','V','K','N','W','7','P','4','J','Y','G','E','A','D','3','F','8','M','X','6','U','R']
|
8
9
|
end
|
9
10
|
|
10
|
-
def
|
11
|
-
|
11
|
+
def numcodes
|
12
|
+
@numcodes ||= codes.length
|
13
|
+
end
|
14
|
+
|
15
|
+
def decode(input, parity=false)
|
16
|
+
cleaned = input.strip.upcase.split('')
|
17
|
+
raise InvalidInputException.new "Invalid character" if cleaned.any?(&:nil?)
|
18
|
+
raise InvalidInputException.new "Invalid input" if parity && cleaned.pop!= checksum(cleaned)
|
19
|
+
|
20
|
+
cleaned.map {|c| codes.index(c) }.inject(0) {|tot,c| (tot * numcodes) + c }
|
21
|
+
end
|
22
|
+
|
23
|
+
def checksum(array)
|
24
|
+
codes[array.inject(0) {|sum,c| (sum + codes.index(c)) % numcodes }]
|
12
25
|
end
|
13
26
|
end
|
14
27
|
|
15
|
-
def initialize(input)
|
28
|
+
def initialize(input, parity=false)
|
16
29
|
output = input == 0 ? [self.class.codes.first] : []
|
17
30
|
while input > 0
|
18
31
|
rem = input % self.class.codes.length
|
19
|
-
input = (input - rem) / self.class.
|
32
|
+
input = (input - rem) / self.class.numcodes #/
|
20
33
|
output << self.class.codes[rem]
|
21
34
|
end
|
22
35
|
output.reverse.each {|c| self << c }
|
36
|
+
self << self.class.checksum(output) if parity
|
23
37
|
end
|
38
|
+
|
39
|
+
|
24
40
|
end
|
data/test/test_all.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
require 'friendly_digest'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestFriendlyDigest < Test::Unit::TestCase
|
6
|
+
def test_no_parity
|
7
|
+
(0..10000).each do |n|
|
8
|
+
assert_equal n, FriendlyDigest.decode(FriendlyDigest.new(n))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_with_parity
|
13
|
+
(0..10000).each do |n|
|
14
|
+
assert_equal n, FriendlyDigest.decode(FriendlyDigest.new(n, true), true)
|
15
|
+
assert_raise(FriendlyDigest::InvalidInputException) { FriendlyDigest.decode('X' + FriendlyDigest.new(n, true), true) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
version: "1.
|
7
|
+
- 1
|
8
|
+
version: "1.1"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Andrew Snow
|
@@ -13,7 +13,7 @@ autorequire:
|
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
15
|
|
16
|
-
date:
|
16
|
+
date: 2011-02-23 00:00:00 +11:00
|
17
17
|
default_executable:
|
18
18
|
dependencies: []
|
19
19
|
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- README
|
36
36
|
- Rakefile
|
37
37
|
- lib/friendly_digest.rb
|
38
|
+
- test/test_all.rb
|
38
39
|
- friendly_digest.gemspec
|
39
40
|
has_rdoc: true
|
40
41
|
homepage: ""
|
@@ -74,5 +75,5 @@ rubygems_version: 1.3.7
|
|
74
75
|
signing_key:
|
75
76
|
specification_version: 3
|
76
77
|
summary: Ruby gem to send to convert integer IDs into easy to type strings
|
77
|
-
test_files:
|
78
|
-
|
78
|
+
test_files:
|
79
|
+
- test/test_all.rb
|