youyouaidi 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/lib/kernel_patch.rb +1 -1
- data/lib/youyouaidi.rb +1 -0
- data/lib/youyouaidi/converter.rb +9 -4
- data/lib/youyouaidi/invalid_uuid.rb +2 -0
- data/lib/youyouaidi/uuid.rb +17 -14
- data/lib/youyouaidi/version.rb +1 -1
- data/spec/kernel_patch_spec.rb +2 -2
- data/spec/youyouaidi/converter_spec.rb +15 -6
- data/spec/youyouaidi_spec.rb +40 -8
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1348322fab3186ecf944dd41ca64cae5316b4648
|
4
|
+
data.tar.gz: f469604e1089be795b4d49bb533af942be2af1d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 577327867e66034cf00973254f133b173c8799545916bb4403e7df4adcaf96c7f0f68570ebe620ef7b852c0110598857e667266b8cf5264ff4246ca3625065de
|
7
|
+
data.tar.gz: 90418860a463cba9a14a5cfad83ae40fed522ca6fce33ecdd611c2bfbfc7f8c2e6ce1a06f6544426d4dcba3d0f0f41346270afee0b8369e03de3581efddb1058
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Youyouaidi
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/youyouaidi.svg)](https://rubygems.org/gems/youyouaidi)
|
4
|
+
[![Build Status](https://travis-ci.org/nicolas-fricke/youyouaidi.svg)](https://travis-ci.org/nicolas-fricke/youyouaidi)
|
5
|
+
|
6
|
+
|
3
7
|
Ruby Gem `Youyouaidi` offers a UUID class for parsing, validating and converting UUIDs into / from shorter representations.
|
4
8
|
|
5
9
|
## Installation
|
@@ -22,9 +26,9 @@ Or install it yourself as:
|
|
22
26
|
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
|
23
27
|
uuid_short = '_oGOAbD9fsFFEHWSMal1v' # Same UUID in its short format
|
24
28
|
|
25
|
-
Youyouaidi::UUID.
|
29
|
+
Youyouaidi::UUID.valid? uuid_string # => true
|
26
30
|
|
27
|
-
uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.
|
31
|
+
uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.parse uuid_string into kernel.
|
28
32
|
# => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
|
29
33
|
# or alternatively a short UUID can be passed
|
30
34
|
uuid = UUID uuid_short # creates similar Youyouaidi::UUID object
|
data/lib/kernel_patch.rb
CHANGED
data/lib/youyouaidi.rb
CHANGED
data/lib/youyouaidi/converter.rb
CHANGED
@@ -7,14 +7,14 @@ class Youyouaidi::Converter
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def decode(encoded_uuid)
|
10
|
-
base_decode encoded_uuid
|
10
|
+
Youyouaidi::UUID.new convert_bignum_to_uuid_string base_decode encoded_uuid
|
11
11
|
end
|
12
12
|
|
13
13
|
private
|
14
14
|
BASE = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a + %w(_ - +)
|
15
15
|
|
16
16
|
def base_encode(numeric)
|
17
|
-
raise
|
17
|
+
raise Youyouaidi::InvalidUUIDError.new "`#{numeric}' needs to be a Numeric" unless numeric.is_a? Numeric
|
18
18
|
|
19
19
|
return '0' if numeric == 0
|
20
20
|
s = ''
|
@@ -34,10 +34,15 @@ class Youyouaidi::Converter
|
|
34
34
|
if ord = BASE.index(char)
|
35
35
|
total += ord * (BASE.size ** index)
|
36
36
|
else
|
37
|
-
raise
|
37
|
+
raise Youyouaidi::InvalidUUIDError.new "`#{encoded_numeric}' has `#{char}' which is not valid"
|
38
38
|
end
|
39
39
|
end
|
40
40
|
total
|
41
41
|
end
|
42
|
+
|
43
|
+
def convert_bignum_to_uuid_string(short_uuid_string)
|
44
|
+
uuid = short_uuid_string.to_i.to_s(16)
|
45
|
+
"#{uuid[0,8]}-#{uuid[8,4]}-#{uuid[12,4]}-#{uuid[16,4]}-#{uuid[20,12]}"
|
46
|
+
end
|
42
47
|
end
|
43
|
-
end
|
48
|
+
end
|
data/lib/youyouaidi/uuid.rb
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
class Youyouaidi::UUID
|
2
2
|
attr_reader :uuid
|
3
3
|
|
4
|
-
def initialize(
|
4
|
+
def initialize(uuid_string, options = {})
|
5
5
|
@converter = options[:converter] || Youyouaidi::Converter
|
6
|
-
|
7
|
-
|
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
|
6
|
+
raise Youyouaidi::InvalidUUIDError.new "`#{uuid_string}' could not be converted to valid UUID" unless self.class.valid? uuid_string
|
7
|
+
@uuid = uuid_string.to_s
|
12
8
|
end
|
13
9
|
|
14
10
|
def to_i
|
@@ -25,14 +21,21 @@ class Youyouaidi::UUID
|
|
25
21
|
alias_method :to_param, :to_short_string
|
26
22
|
|
27
23
|
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
24
|
|
33
25
|
class << self
|
34
|
-
def
|
35
|
-
|
26
|
+
def parse(uuid_param, options = {})
|
27
|
+
@converter = options[:converter] || Youyouaidi::Converter
|
28
|
+
if valid? uuid_param
|
29
|
+
self.new uuid_param.to_s, options
|
30
|
+
else
|
31
|
+
uuid_obj = @converter.decode uuid_param
|
32
|
+
raise Youyouaidi::InvalidUUIDError.new "`#{uuid_param}' could not be converted to valid UUID" unless valid? uuid_obj.to_s
|
33
|
+
uuid_obj
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def valid?(uuid_canidate)
|
38
|
+
(uuid_canidate.to_s =~ /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i) == 0
|
36
39
|
end
|
37
40
|
end
|
38
|
-
end
|
41
|
+
end
|
data/lib/youyouaidi/version.rb
CHANGED
data/spec/kernel_patch_spec.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Youyouaidi::Converter do
|
4
|
+
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
4
5
|
let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
|
5
|
-
let(:uuid_bignum) { 113059749145936325402354257176981405696 }
|
6
6
|
let(:encoded_uuid) { '_oGOAbD9fsFFEHWSMal1v' }
|
7
7
|
|
8
8
|
describe 'use case' do
|
9
|
-
subject { described_class.decode described_class.encode(
|
9
|
+
subject { described_class.decode described_class.encode(uuid) }
|
10
10
|
|
11
|
-
it { should
|
11
|
+
it { should be_a Youyouaidi::UUID }
|
12
|
+
its(:to_s) { should eq uuid_string }
|
12
13
|
end
|
13
14
|
|
14
15
|
describe 'methods' do
|
15
|
-
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
16
16
|
|
17
17
|
describe '.encode' do
|
18
18
|
subject { described_class.encode uuid }
|
@@ -23,7 +23,16 @@ describe Youyouaidi::Converter do
|
|
23
23
|
describe '.decode' do
|
24
24
|
subject { described_class.decode encoded_uuid }
|
25
25
|
|
26
|
-
it { should
|
26
|
+
it { should be_a Youyouaidi::UUID }
|
27
|
+
its(:to_s) { should eq uuid_string }
|
28
|
+
|
29
|
+
context 'with invalid characters' do
|
30
|
+
let(:encoded_uuid) { ' ' }
|
31
|
+
|
32
|
+
it 'raises error' do
|
33
|
+
expect { subject }.to raise_error Youyouaidi::InvalidUUIDError
|
34
|
+
end
|
35
|
+
end
|
27
36
|
end
|
28
37
|
end
|
29
|
-
end
|
38
|
+
end
|
data/spec/youyouaidi_spec.rb
CHANGED
@@ -4,7 +4,36 @@ describe Youyouaidi do
|
|
4
4
|
describe Youyouaidi::UUID do
|
5
5
|
describe '.new' do
|
6
6
|
let(:param) { '' }
|
7
|
-
|
7
|
+
let(:action) { Youyouaidi::UUID.new param }
|
8
|
+
subject { -> { action } }
|
9
|
+
|
10
|
+
context 'with valid uuid string' do
|
11
|
+
let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
|
12
|
+
subject { action }
|
13
|
+
|
14
|
+
it { should be_a Youyouaidi::UUID }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'with uuid in short format' do
|
18
|
+
let(:param) { '_oGOAbD9fsFFEHWSMal1v' }
|
19
|
+
it { should raise_error Youyouaidi::InvalidUUIDError }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with invalid uuid string' do
|
23
|
+
let(:param) { 'Kekse' }
|
24
|
+
it { should raise_error Youyouaidi::InvalidUUIDError }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with non-uuid-string input' do
|
28
|
+
let(:param) { 1234 }
|
29
|
+
it { should raise_error Youyouaidi::InvalidUUIDError }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.parse' do
|
34
|
+
let(:param) { '' }
|
35
|
+
let(:action) { Youyouaidi::UUID.parse param }
|
36
|
+
subject { action }
|
8
37
|
|
9
38
|
context 'with valid uuid string' do
|
10
39
|
let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
|
@@ -24,7 +53,7 @@ describe Youyouaidi do
|
|
24
53
|
let(:param) { 'Kekse' }
|
25
54
|
|
26
55
|
it 'raises error' do
|
27
|
-
expect { Youyouaidi::UUID.
|
56
|
+
expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
|
28
57
|
end
|
29
58
|
end
|
30
59
|
|
@@ -32,7 +61,7 @@ describe Youyouaidi do
|
|
32
61
|
let(:param) { 1234 }
|
33
62
|
|
34
63
|
it 'raises error' do
|
35
|
-
expect { Youyouaidi::UUID.
|
64
|
+
expect { Youyouaidi::UUID.parse param }.to raise_error Youyouaidi::InvalidUUIDError
|
36
65
|
end
|
37
66
|
end
|
38
67
|
end
|
@@ -75,9 +104,9 @@ describe Youyouaidi do
|
|
75
104
|
it_behaves_like 'method for short format'
|
76
105
|
end
|
77
106
|
|
78
|
-
describe '.
|
107
|
+
describe '.valid?' do
|
79
108
|
let(:param) { '' }
|
80
|
-
subject { Youyouaidi::UUID.
|
109
|
+
subject { Youyouaidi::UUID.valid? param }
|
81
110
|
|
82
111
|
context 'with valid uuid' do
|
83
112
|
let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
|
@@ -86,14 +115,17 @@ describe Youyouaidi do
|
|
86
115
|
end
|
87
116
|
|
88
117
|
context 'with invalid uuid' do
|
89
|
-
|
118
|
+
uuid_string = '550e8400-e29b-41d4-a716-446655440000'
|
119
|
+
encoded_uuid = '_oGOAbD9fsFFEHWSMal1v'
|
120
|
+
invalid_uuids = ['Kekse', "aa#{uuid_string}", "#{uuid_string}bb",
|
121
|
+
"#{encoded_uuid}" ]
|
90
122
|
|
91
123
|
invalid_uuids.each do |invalid_uuid|
|
92
124
|
it "should return false for `#{invalid_uuid}`" do
|
93
|
-
expect(Youyouaidi::UUID.
|
125
|
+
expect(Youyouaidi::UUID.valid? invalid_uuid).to eq false
|
94
126
|
end
|
95
127
|
end
|
96
128
|
end
|
97
129
|
end
|
98
130
|
end
|
99
|
-
end
|
131
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: youyouaidi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Fricke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- lib/kernel_patch.rb
|
56
56
|
- lib/youyouaidi.rb
|
57
57
|
- lib/youyouaidi/converter.rb
|
58
|
+
- lib/youyouaidi/invalid_uuid.rb
|
58
59
|
- lib/youyouaidi/uuid.rb
|
59
60
|
- lib/youyouaidi/version.rb
|
60
61
|
- spec/kernel_patch_spec.rb
|