Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Hallgren committed Jul 9, 2013
1 parent 80a4c1d commit a1b565f
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format d
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uuid_shortner
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.0.0
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in uuid_shortner.gemspec
gemspec
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
PATH
remote: .
specs:
uuid_shortner (0.0.1)

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.4)
gem-release (0.6.0)
macaddr (1.6.1)
systemu (~> 2.5.0)
rake (10.1.0)
rspec (2.14.0)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.1)
rspec-expectations (2.14.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.1)
systemu (2.5.2)
uuid (2.3.7)
macaddr (~> 1.0)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.3)
gem-release
rake
rspec
uuid
uuid_shortner!
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013 Morgan Hallgren

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "bundler/gem_tasks"

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
11 changes: 11 additions & 0 deletions lib/uuid_shortner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "uuid_shortner/version"
require "uuid_shortner/guid_decoder"
require "uuid_shortner/guid_encoder"

module UuidShortner
class << self
def ALPHABET
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(//)
end
end
end
24 changes: 24 additions & 0 deletions lib/uuid_shortner/guid_decoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module UuidShortner
module GuidDecoder
def decode short_uuid
guid_as_int = bijective_decode short_uuid
guid_as_hex = guid_as_int.to_s(16)
insert_uuid_hyphens guid_as_hex
end

private
def insert_uuid_hyphens compact_guid
guid = compact_guid.rjust(32,'0')
"#{guid[0..7]}-#{guid[8..11]}-#{guid[12..15]}-#{guid[16..19]}-#{guid[20..31]}"
end
def bijective_decode(s)
# based on base2dec() in Tcl translation
# at http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
i = 0
base = UuidShortner.ALPHABET.length
s.each_char { |c| i = i * base + UuidShortner.ALPHABET.index(c) }
i
end

end
end
22 changes: 22 additions & 0 deletions lib/uuid_shortner/guid_encoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module UuidShortner
module GuidEncoder
def encode uuid
clean_guid = uuid.gsub(/[^a-zA-Z0-9 ]/,'')
guid_as_hex = clean_guid.hex
short_id = bijective_encode guid_as_hex
end
private
def bijective_encode(i)
# from http://refactormycode.com/codes/125-base-62-encoding
# with only minor modification
return UuidShortner.ALPHABET[0] if i == 0
s = ''
base = UuidShortner.ALPHABET.length
while i > 0
s << UuidShortner.ALPHABET[i.modulo(base)]
i /= base
end
s.reverse
end
end
end
3 changes: 3 additions & 0 deletions lib/uuid_shortner/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module UuidShortner
VERSION = "0.0.1"
end
27 changes: 27 additions & 0 deletions spec/decode_encode_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'
require 'uuid'

module UuidShortner
describe GuidDecoder do
before(:all) do
@dummy_object = Object.new
@dummy_object.extend GuidDecoder
@dummy_object.extend GuidEncoder
end
it 'should encode and decode to and from values' do
u = UUID.new
10.times do
id = u.generate
short_id = @dummy_object.encode id
short_id.length.should <= 22
decoded = @dummy_object.decode short_id
decoded.should eql id
end
end
it "should decode and encode edge cases" do
c1 = "00000310-250a-0130-47db-58b035f3cd77"
e1 = @dummy_object.encode c1
@dummy_object.decode(e1).should eql c1
end
end
end
28 changes: 28 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "bundler"

Bundler.require

module Helpers
def class_including(mod)
Class.new.tap {|c| c.send :include, mod }
end
end

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'

end
27 changes: 27 additions & 0 deletions uuid_shortner.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'uuid_shortner/version'

Gem::Specification.new do |spec|
spec.name = "uuid_shortner"
spec.version = UuidShortner::VERSION
spec.authors = ["Morgan Hallgren"]
spec.email = ["[email protected]"]
spec.description = %q{shorten a uuid to a shorter representation and back}
spec.summary = %q{shorten a uuid to a shorter representation and back}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "gem-release"
spec.add_development_dependency "uuid"

end

0 comments on commit a1b565f

Please sign in to comment.