uuid_shortner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56307efbc0d4664e24f6809610a09a3daf542a28
4
+ data.tar.gz: 49852cb4a51f4c746a838ed6d9ec67775f93db6f
5
+ SHA512:
6
+ metadata.gz: a0cbd052ae3a3322c1e1a59fc290df415181de8637edb0a5762c671ba8faad3844dad0481367ccea2b0e6e0645368f6e7de629bc246026510e56ccf56d8f0ef1
7
+ data.tar.gz: 742c7d782b14d7c119b0bbdf8f866eadea8691d298f4ccc3d91512361e6581716719a4a1e7604ea4498b38750346f128d874a416e3a87a5f4d49f5ba7ece4afc
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format d
@@ -0,0 +1 @@
1
+ uuid_shortner
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uuid_shortner.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ uuid_shortner (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.4)
10
+ gem-release (0.6.0)
11
+ macaddr (1.6.1)
12
+ systemu (~> 2.5.0)
13
+ rake (10.1.0)
14
+ rspec (2.14.0)
15
+ rspec-core (~> 2.14.0)
16
+ rspec-expectations (~> 2.14.0)
17
+ rspec-mocks (~> 2.14.0)
18
+ rspec-core (2.14.1)
19
+ rspec-expectations (2.14.0)
20
+ diff-lcs (>= 1.1.3, < 2.0)
21
+ rspec-mocks (2.14.1)
22
+ systemu (2.5.2)
23
+ uuid (2.3.7)
24
+ macaddr (~> 1.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.3)
31
+ gem-release
32
+ rake
33
+ rspec
34
+ uuid
35
+ uuid_shortner!
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Morgan Hallgren
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.
@@ -0,0 +1,4 @@
1
+ uuid_shortner
2
+ =============
3
+
4
+ shorten a uuid to a shorter representation and back
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ require "uuid_shortner/version"
2
+ require "uuid_shortner/guid_decoder"
3
+ require "uuid_shortner/guid_encoder"
4
+
5
+ module UuidShortner
6
+ class << self
7
+ def ALPHABET
8
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(//)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module UuidShortner
2
+ module GuidDecoder
3
+ def stretch short_uuid
4
+ guid_as_int = bijective_decode short_uuid
5
+ guid_as_hex = guid_as_int.to_s(16)
6
+ insert_uuid_hyphens guid_as_hex
7
+ end
8
+
9
+ private
10
+ def insert_uuid_hyphens compact_guid
11
+ guid = compact_guid.rjust(32,'0')
12
+ "#{guid[0..7]}-#{guid[8..11]}-#{guid[12..15]}-#{guid[16..19]}-#{guid[20..31]}"
13
+ end
14
+ def bijective_decode(s)
15
+ # based on base2dec() in Tcl translation
16
+ # at http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
17
+ i = 0
18
+ base = UuidShortner.ALPHABET.length
19
+ s.each_char { |c| i = i * base + UuidShortner.ALPHABET.index(c) }
20
+ i
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module UuidShortner
2
+ module GuidEncoder
3
+ def compress uuid
4
+ clean_guid = uuid.gsub(/[^a-zA-Z0-9 ]/,'')
5
+ guid_as_hex = clean_guid.hex
6
+ short_id = bijective_encode guid_as_hex
7
+ end
8
+ private
9
+ def bijective_encode(i)
10
+ # from http://refactormycode.com/codes/125-base-62-encoding
11
+ # with only minor modification
12
+ return UuidShortner.ALPHABET[0] if i == 0
13
+ s = ''
14
+ base = UuidShortner.ALPHABET.length
15
+ while i > 0
16
+ s << UuidShortner.ALPHABET[i.modulo(base)]
17
+ i /= base
18
+ end
19
+ s.reverse
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module UuidShortner
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'uuid'
3
+
4
+ module UuidShortner
5
+ describe GuidDecoder do
6
+ before(:all) do
7
+ @dummy_object = Object.new
8
+ @dummy_object.extend GuidDecoder
9
+ @dummy_object.extend GuidEncoder
10
+ end
11
+ it 'should encode and decode to and from values' do
12
+ u = UUID.new
13
+ 10.times do
14
+ id = u.generate
15
+ short_id = @dummy_object.compress id
16
+ short_id.length.should <= 22
17
+ uuid = @dummy_object.stretch short_id
18
+ uuid.should eql id
19
+ end
20
+ end
21
+ it "should decode and encode edge cases" do
22
+ c1 = "00000310-250a-0130-47db-58b035f3cd77"
23
+ e1 = @dummy_object.compress c1
24
+ @dummy_object.stretch(e1).should eql c1
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require "bundler"
2
+
3
+ Bundler.require
4
+
5
+ module Helpers
6
+ def class_including(mod)
7
+ Class.new.tap {|c| c.send :include, mod }
8
+ end
9
+ end
10
+
11
+ # This file was generated by the `rspec --init` command. Conventionally, all
12
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
13
+ # Require this file using `require "spec_helper"` to ensure that it is only
14
+ # loaded once.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.run_all_when_everything_filtered = true
20
+ config.filter_run :focus
21
+
22
+ # Run specs in random order to surface order dependencies. If you find an
23
+ # order dependency and want to debug it, you can fix the order by providing
24
+ # the seed, which is printed after each run.
25
+ # --seed 1234
26
+ config.order = 'random'
27
+
28
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'uuid_shortner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "uuid_shortner"
8
+ spec.version = UuidShortner::VERSION
9
+ spec.authors = ["Morgan Hallgren"]
10
+ spec.email = ["morgan.hallgren@upptec.se"]
11
+ spec.description = %q{shorten a uuid to a shorter representation and back}
12
+ spec.summary = %q{shorten a uuid to a shorter representation and back}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "gem-release"
25
+ spec.add_development_dependency "uuid"
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uuid_shortner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Morgan Hallgren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gem-release
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: uuid
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: shorten a uuid to a shorter representation and back
84
+ email:
85
+ - morgan.hallgren@upptec.se
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .ruby-gemset
93
+ - .ruby-version
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/uuid_shortner.rb
100
+ - lib/uuid_shortner/guid_decoder.rb
101
+ - lib/uuid_shortner/guid_encoder.rb
102
+ - lib/uuid_shortner/version.rb
103
+ - spec/decode_encode_spec.rb
104
+ - spec/spec_helper.rb
105
+ - uuid_shortner.gemspec
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: shorten a uuid to a shorter representation and back
130
+ test_files:
131
+ - spec/decode_encode_spec.rb
132
+ - spec/spec_helper.rb