youyouaidi 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +5 -0
- data/lib/kernel_patch.rb +7 -0
- data/lib/youyouaidi.rb +8 -0
- data/lib/youyouaidi/converter.rb +43 -0
- data/lib/youyouaidi/uuid.rb +38 -0
- data/lib/youyouaidi/version.rb +3 -0
- data/spec/kernel_patch_spec.rb +33 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/youyouaidi/converter_spec.rb +29 -0
- data/spec/youyouaidi_spec.rb +99 -0
- data/youyouaidi.gemspec +23 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0d8a7918c5ccc52c7b51c259aef312e9c0c4b74
|
4
|
+
data.tar.gz: 4e280e52b10d17e1adebe93986e353078d90de18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 31b9dbba81a25d1923a44ca05b731d4b7762c3fbde742f36259269d781e10e45d9911042d385891f828fae2469f09d8913d0dfd74022b05d67f12a742d77950f
|
7
|
+
data.tar.gz: e162eb97d5f4609ebd91389ba0907134a0e5f1761764139730f9cfcedb4562561ea14edc6b2dc895a7d1a0a584d43b3fec32837d55b63e803ebce72c2f8471cd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nicolas Fricke
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Youyouaidi
|
2
|
+
|
3
|
+
Ruby Gem `Youyouaidi` offers a UUID class for parsing, validating and converting UUIDs into / from shorter representations.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'youyouaidi'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install youyouaidi
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
|
23
|
+
uuid_short = '_oGOAbD9fsFFEHWSMal1v' # Same UUID in its short format
|
24
|
+
|
25
|
+
Youyouaidi::UUID.valid_uuid? uuid_string # => true
|
26
|
+
|
27
|
+
uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.new uuid_string into kernel.
|
28
|
+
# => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
|
29
|
+
# or alternatively a short UUID can be passed
|
30
|
+
uuid = UUID uuid_short # creates similar Youyouaidi::UUID object
|
31
|
+
# => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
|
32
|
+
|
33
|
+
uuid.to_s # Returns the string representation of the UUID object
|
34
|
+
# => '550e8400-e29b-41d4-a716-446655440000'
|
35
|
+
uuid.to_short_s # Returns the short string representation of the UUID object
|
36
|
+
# => '_oGOAbD9fsFFEHWSMal1v', alias for method: #to_param
|
37
|
+
```
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it ( http://github.com/<my-github-username>/youyouaidi/fork )
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/kernel_patch.rb
ADDED
data/lib/youyouaidi.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Based on Base62 module by sinefunc
|
2
|
+
# => https://github.com/sinefunc/base62
|
3
|
+
class Youyouaidi::Converter
|
4
|
+
class << self
|
5
|
+
def encode(uuid)
|
6
|
+
base_encode uuid.to_i
|
7
|
+
end
|
8
|
+
|
9
|
+
def decode(encoded_uuid)
|
10
|
+
base_decode encoded_uuid
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
BASE = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a + %w(_ - +)
|
15
|
+
|
16
|
+
def base_encode(numeric)
|
17
|
+
raise ArgumentError, "`#{numeric}' needs to be a Numeric" unless numeric.is_a? Numeric
|
18
|
+
|
19
|
+
return '0' if numeric == 0
|
20
|
+
s = ''
|
21
|
+
|
22
|
+
while numeric > 0
|
23
|
+
s << BASE[numeric.modulo(BASE.size)]
|
24
|
+
numeric /= BASE.size
|
25
|
+
end
|
26
|
+
s.reverse
|
27
|
+
end
|
28
|
+
|
29
|
+
def base_decode(encoded_numeric)
|
30
|
+
s = encoded_numeric.to_s.reverse.split('')
|
31
|
+
|
32
|
+
total = 0
|
33
|
+
s.each_with_index do |char, index|
|
34
|
+
if ord = BASE.index(char)
|
35
|
+
total += ord * (BASE.size ** index)
|
36
|
+
else
|
37
|
+
raise ArgumentError, "`#{encoded_numeric}' has `#{char}' which is not valid"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
total
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Youyouaidi::UUID
|
2
|
+
attr_reader :uuid
|
3
|
+
|
4
|
+
def initialize(uuid_param, options = {})
|
5
|
+
@converter = options[:converter] || Youyouaidi::Converter
|
6
|
+
if self.class.valid_uuid? uuid_param
|
7
|
+
@uuid = uuid_param.to_s
|
8
|
+
else
|
9
|
+
@uuid = decode_short_string uuid_param
|
10
|
+
raise ArgumentError.new "`#{uuid_param}' could not be converted to valid UUID" unless self.class.valid_uuid? @uuid
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_i
|
15
|
+
@uuid.gsub('-', '').hex
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
@uuid
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_short_string
|
23
|
+
@converter.encode self
|
24
|
+
end
|
25
|
+
alias_method :to_param, :to_short_string
|
26
|
+
|
27
|
+
private
|
28
|
+
def decode_short_string(short_uuid_string)
|
29
|
+
uuid = @converter.decode(short_uuid_string).to_i.to_s(16)
|
30
|
+
"#{uuid[0,8]}-#{uuid[8,4]}-#{uuid[12,4]}-#{uuid[16,4]}-#{uuid[20,12]}"
|
31
|
+
end
|
32
|
+
|
33
|
+
class << self
|
34
|
+
def valid_uuid?(uuid_canidate)
|
35
|
+
(uuid_canidate.to_s =~ /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i) == 0
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Kernel do
|
4
|
+
describe '.UUID' do
|
5
|
+
let(:valid_uuid) { '550e8400-e29b-41d4-a716-446655440000' }
|
6
|
+
let(:encoded_uuid) { '_oGOAbD9fsFFEHWSMal1v' }
|
7
|
+
let(:action) { UUID param }
|
8
|
+
|
9
|
+
subject { action }
|
10
|
+
|
11
|
+
|
12
|
+
shared_examples_for 'a valid UUID object' do
|
13
|
+
it { should be_a Youyouaidi::UUID }
|
14
|
+
its(:to_s) { should eq valid_uuid }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'initialized via valid uuid' do
|
18
|
+
let(:param) { valid_uuid }
|
19
|
+
it_behaves_like 'a valid UUID object'
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'initialized via vaild encoded uuid' do
|
23
|
+
let(:param) { encoded_uuid }
|
24
|
+
it_behaves_like 'a valid UUID object'
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'initialized with invalid param' do
|
28
|
+
let(:param) { 'KEKSE' }
|
29
|
+
subject { -> { action } }
|
30
|
+
it { should raise_error ArgumentError }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler.require
|
5
|
+
|
6
|
+
require 'youyouaidi'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
# ## Mock Framework
|
10
|
+
#
|
11
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
12
|
+
#
|
13
|
+
# config.mock_with :mocha
|
14
|
+
# config.mock_with :flexmock
|
15
|
+
# config.mock_with :rr
|
16
|
+
|
17
|
+
# Run specs in random order to surface order dependencies. If you find an
|
18
|
+
# order dependency and want to debug it, you can fix the order by providing
|
19
|
+
# the seed, which is printed after each run.
|
20
|
+
# --seed 1234
|
21
|
+
config.order = 'random'
|
22
|
+
|
23
|
+
# Raise error when using old :should expectation syntax.
|
24
|
+
# config.raise_errors_for_deprecations!
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Youyouaidi::Converter do
|
4
|
+
let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
|
5
|
+
let(:uuid_bignum) { 113059749145936325402354257176981405696 }
|
6
|
+
let(:encoded_uuid) { '_oGOAbD9fsFFEHWSMal1v' }
|
7
|
+
|
8
|
+
describe 'use case' do
|
9
|
+
subject { described_class.decode described_class.encode(uuid_bignum) }
|
10
|
+
|
11
|
+
it { should eq uuid_bignum }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'methods' do
|
15
|
+
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
16
|
+
|
17
|
+
describe '.encode' do
|
18
|
+
subject { described_class.encode uuid }
|
19
|
+
|
20
|
+
it { should eq encoded_uuid }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '.decode' do
|
24
|
+
subject { described_class.decode encoded_uuid }
|
25
|
+
|
26
|
+
it { should eq uuid_bignum }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Youyouaidi do
|
4
|
+
describe Youyouaidi::UUID do
|
5
|
+
describe '.new' do
|
6
|
+
let(:param) { '' }
|
7
|
+
subject { Youyouaidi::UUID.new param }
|
8
|
+
|
9
|
+
context 'with valid uuid string' do
|
10
|
+
let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
|
11
|
+
|
12
|
+
it { should be_a Youyouaidi::UUID }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with uuid in short format' do
|
16
|
+
let(:param) { '_oGOAbD9fsFFEHWSMal1v' }
|
17
|
+
let(:decoded_uuid) { '550e8400-e29b-41d4-a716-446655440000' }
|
18
|
+
|
19
|
+
it { should be_a Youyouaidi::UUID }
|
20
|
+
its(:to_s) { should eq decoded_uuid }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'with invalid uuid string' do
|
24
|
+
let(:param) { 'Kekse' }
|
25
|
+
|
26
|
+
it 'raises error' do
|
27
|
+
expect { Youyouaidi::UUID.new param }.to raise_error ArgumentError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with non-string input' do
|
32
|
+
let(:param) { 1234 }
|
33
|
+
|
34
|
+
it 'raises error' do
|
35
|
+
expect { Youyouaidi::UUID.new param }.to raise_error ArgumentError
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#to_s' do
|
41
|
+
let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
|
42
|
+
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
43
|
+
subject { uuid.to_s }
|
44
|
+
|
45
|
+
it { should be_a String }
|
46
|
+
it { should eq uuid_string }
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#to_i' do
|
50
|
+
let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
|
51
|
+
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
52
|
+
subject { uuid.to_i }
|
53
|
+
|
54
|
+
it { should be_a Bignum }
|
55
|
+
it { should eq 113059749145936325402354257176981405696 }
|
56
|
+
end
|
57
|
+
|
58
|
+
shared_examples_for 'method for short format' do
|
59
|
+
let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
|
60
|
+
let(:encoded_uuid) { '_oGOAbD9fsFFEHWSMal1v' }
|
61
|
+
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
62
|
+
|
63
|
+
let(:action) { uuid.send method }
|
64
|
+
subject { action }
|
65
|
+
|
66
|
+
it { should be_a String }
|
67
|
+
it { should eq encoded_uuid }
|
68
|
+
end
|
69
|
+
describe '#to_short_string' do
|
70
|
+
let(:method) { :to_short_string }
|
71
|
+
it_behaves_like 'method for short format'
|
72
|
+
end
|
73
|
+
describe '#to_param' do
|
74
|
+
let(:method) { :to_param }
|
75
|
+
it_behaves_like 'method for short format'
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '.valid_uuid?' do
|
79
|
+
let(:param) { '' }
|
80
|
+
subject { Youyouaidi::UUID.valid_uuid? param }
|
81
|
+
|
82
|
+
context 'with valid uuid' do
|
83
|
+
let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
|
84
|
+
|
85
|
+
it { should be_true }
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'with invalid uuid' do
|
89
|
+
invalid_uuids = ['Kekse', 'aa550e8400-e29b-41d4-a716-446655440000']
|
90
|
+
|
91
|
+
invalid_uuids.each do |invalid_uuid|
|
92
|
+
it "should return false for `#{invalid_uuid}`" do
|
93
|
+
expect(Youyouaidi::UUID.valid_uuid? invalid_uuid).to eq false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/youyouaidi.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'youyouaidi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'youyouaidi'
|
8
|
+
spec.version = Youyouaidi::VERSION
|
9
|
+
spec.authors = ['Nicolas Fricke']
|
10
|
+
spec.email = ['mail@nicolasfricke.de']
|
11
|
+
spec.summary = %q{UUID class}
|
12
|
+
spec.description = %q{Youyouaidi offers a UUID class for parsing, validating and encoding UUIDs}
|
13
|
+
spec.homepage = 'https://github.com/nicolas-fricke/youyouaidi'
|
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.5'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: youyouaidi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Fricke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-06 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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
|
+
description: Youyouaidi offers a UUID class for parsing, validating and encoding UUIDs
|
42
|
+
email:
|
43
|
+
- mail@nicolasfricke.de
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- .travis.yml
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/kernel_patch.rb
|
56
|
+
- lib/youyouaidi.rb
|
57
|
+
- lib/youyouaidi/converter.rb
|
58
|
+
- lib/youyouaidi/uuid.rb
|
59
|
+
- lib/youyouaidi/version.rb
|
60
|
+
- spec/kernel_patch_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/youyouaidi/converter_spec.rb
|
63
|
+
- spec/youyouaidi_spec.rb
|
64
|
+
- youyouaidi.gemspec
|
65
|
+
homepage: https://github.com/nicolas-fricke/youyouaidi
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.2.2
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: UUID class
|
89
|
+
test_files:
|
90
|
+
- spec/kernel_patch_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
- spec/youyouaidi/converter_spec.rb
|
93
|
+
- spec/youyouaidi_spec.rb
|
94
|
+
has_rdoc:
|