youyouaidi 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/README.md +10 -7
- data/lib/youyouaidi/converter.rb +12 -4
- data/lib/youyouaidi/version.rb +1 -1
- data/spec/kernel_patch_spec.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/youyouaidi/converter_spec.rb +34 -9
- data/spec/youyouaidi/uuid_spec.rb +17 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 383f96781da456cf785f37008b07cea302bc6465
|
4
|
+
data.tar.gz: 63ad6a4540f8f1740b5e9fb0477c610203c93a32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98d5f4460aade04d5bd5b3d458057de3e04321968d7a65194ff26161ca219b6e2a6b1e0b259641eb24e9c8e5a55d8080f123f6821f1449ce5751ad9266531c4f
|
7
|
+
data.tar.gz: 769b6f76f82a8add80421b20ac845487cfa07138cd1286a5e6fb62d982120da90b9f3f90e2ec4c33a19ca2db85cdc0ec9f7c4a8a008bb72b18d7387f602bcf21
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
-
# Youyouaidi
|
2
|
-
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/youyouaidi.svg)](https://rubygems.org/gems/youyouaidi)
|
1
|
+
# Youyouaidi
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/youyouaidi.svg)](https://rubygems.org/gems/youyouaidi)
|
4
3
|
[![Build Status](https://travis-ci.org/nicolas-fricke/youyouaidi.svg)](https://travis-ci.org/nicolas-fricke/youyouaidi)
|
4
|
+
[![Coverage Status](http://img.shields.io/coveralls/nicolas-fricke/youyouaidi.svg)](https://coveralls.io/r/nicolas-fricke/youyouaidi)
|
5
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/nicolas-fricke/youyouaidi.svg)](https://codeclimate.com/github/nicolas-fricke/youyouaidi)
|
6
|
+
[![License](http://img.shields.io/badge/license-MIT-brightgreen.svg)](https://tldrlegal.com/license/mit-license)
|
5
7
|
|
6
8
|
|
7
|
-
Ruby Gem
|
9
|
+
`Youyouaidi` is a Ruby Gem that offers a UUID class for parsing, validating and converting UUIDs into / from shorter representations.
|
10
|
+
While a UUID consists of 32 hexadecimal characters by dashes divided into 5 subgroups, the short representation (invoked via `#to_shrort_string`) consists of exactly 22 digit and lower- and uppercase characters.
|
8
11
|
|
9
12
|
## Installation
|
10
13
|
|
@@ -25,8 +28,8 @@ Or install it yourself as:
|
|
25
28
|
### Initializing UUIDs
|
26
29
|
|
27
30
|
```ruby
|
28
|
-
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
|
29
|
-
uuid_short = '
|
31
|
+
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format, has exactly 32 hexadecimal characters in 5 groups
|
32
|
+
uuid_short = '2AuYQJcZeiIeCymkJ7tzTW' # Same UUID in its short format, has exactly 22 characters of [0-9a-zA-Z]
|
30
33
|
|
31
34
|
uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.parse uuid_string into kernel.
|
32
35
|
# => #<Youyouaidi::UUID:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
|
@@ -49,7 +52,7 @@ uuid.to_s # Returns the string representation of the UUID object
|
|
49
52
|
# => '550e8400-e29b-41d4-a716-446655440000'
|
50
53
|
|
51
54
|
uuid.to_short_s # Returns the short string representation of the UUID object
|
52
|
-
# => '
|
55
|
+
# => '2AuYQJcZeiIeCymkJ7tzTW', alias for method: #to_param
|
53
56
|
```
|
54
57
|
|
55
58
|
|
data/lib/youyouaidi/converter.rb
CHANGED
@@ -7,11 +7,14 @@ class Youyouaidi::Converter
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def decode(encoded_uuid)
|
10
|
+
encoded_uuid = encoded_uuid.to_s
|
11
|
+
raise Youyouaidi::InvalidUUIDError.new "`#{encoded_uuid}' needs to have exactly #{ENCODED_LENGTH} characters" unless encoded_uuid.length == ENCODED_LENGTH
|
10
12
|
Youyouaidi::UUID.new convert_bignum_to_uuid_string base_decode encoded_uuid
|
11
13
|
end
|
12
14
|
|
13
15
|
private
|
14
|
-
BASE
|
16
|
+
BASE = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
|
17
|
+
ENCODED_LENGTH = 22 # Needs to be greater than `(Math.log 2**128, BASE.length).floor + 1`
|
15
18
|
|
16
19
|
def base_encode(numeric)
|
17
20
|
raise Youyouaidi::InvalidUUIDError.new "`#{numeric}' needs to be a Numeric" unless numeric.is_a? Numeric
|
@@ -23,6 +26,9 @@ class Youyouaidi::Converter
|
|
23
26
|
s << BASE[numeric.modulo(BASE.size)]
|
24
27
|
numeric /= BASE.size
|
25
28
|
end
|
29
|
+
while s.length < ENCODED_LENGTH
|
30
|
+
s << BASE[0]
|
31
|
+
end
|
26
32
|
s.reverse
|
27
33
|
end
|
28
34
|
|
@@ -40,9 +46,11 @@ class Youyouaidi::Converter
|
|
40
46
|
total
|
41
47
|
end
|
42
48
|
|
43
|
-
def convert_bignum_to_uuid_string(
|
44
|
-
|
45
|
-
"#{
|
49
|
+
def convert_bignum_to_uuid_string(decoded_uuid_bignum)
|
50
|
+
decoded_uuid = decoded_uuid_bignum.to_i.to_s(16).rjust(32, '0')
|
51
|
+
parsed_uuid = "#{decoded_uuid[0,8]}-#{decoded_uuid[8,4]}-#{decoded_uuid[12,4]}-#{decoded_uuid[16,4]}-#{decoded_uuid[20,12]}"
|
52
|
+
raise Youyouaidi::InvalidUUIDError.new "Converted string `#{decoded_uuid}' has too many characters" if decoded_uuid.length > 32
|
53
|
+
parsed_uuid
|
46
54
|
end
|
47
55
|
end
|
48
56
|
end
|
data/lib/youyouaidi/version.rb
CHANGED
data/spec/kernel_patch_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Kernel do
|
4
4
|
describe '.UUID' do
|
5
5
|
let(:valid_uuid) { '550e8400-e29b-41d4-a716-446655440000' }
|
6
|
-
let(:encoded_uuid) { '
|
6
|
+
let(:encoded_uuid) { '2AuYQJcZeiIeCymkJ7tzTW' }
|
7
7
|
let(:action) { UUID param }
|
8
8
|
|
9
9
|
subject { action }
|
data/spec/spec_helper.rb
CHANGED
@@ -3,7 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe Youyouaidi::Converter do
|
4
4
|
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
5
5
|
let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
|
6
|
-
let(:encoded_uuid) { '
|
6
|
+
let(:encoded_uuid) { '2AuYQJcZeiIeCymkJ7tzTW' }
|
7
|
+
|
8
|
+
uuids_with_encoding = {
|
9
|
+
'550e8400-e29b-41d4-a716-446655440000' => '2AuYQJcZeiIeCymkJ7tzTW',
|
10
|
+
'00000000-bbbb-2222-8888-000000000000' => '000001dyObGywDRlcScExy'
|
11
|
+
}
|
7
12
|
|
8
13
|
describe 'use case' do
|
9
14
|
subject { described_class.decode described_class.encode(uuid) }
|
@@ -17,20 +22,40 @@ describe Youyouaidi::Converter do
|
|
17
22
|
describe '.encode' do
|
18
23
|
subject { described_class.encode uuid }
|
19
24
|
|
20
|
-
|
25
|
+
uuids_with_encoding.each do |valid_uuid, encoded_uuid|
|
26
|
+
context "for uuid `#{valid_uuid}'" do
|
27
|
+
let(:uuid_string) { valid_uuid }
|
28
|
+
it { should have(22).characters }
|
29
|
+
it { should eq encoded_uuid }
|
30
|
+
end
|
31
|
+
end
|
21
32
|
end
|
22
33
|
|
23
34
|
describe '.decode' do
|
24
|
-
|
35
|
+
let(:action) { described_class.decode encoded_param }
|
25
36
|
|
26
|
-
|
27
|
-
|
37
|
+
context 'with valid param' do
|
38
|
+
subject { action }
|
28
39
|
|
29
|
-
|
30
|
-
|
40
|
+
uuids_with_encoding.each do |valid_uuid, encoded_uuid|
|
41
|
+
context "for encoded uuid `#{encoded_uuid}'" do
|
42
|
+
let(:encoded_param) { encoded_uuid }
|
43
|
+
it { should be_a Youyouaidi::UUID }
|
44
|
+
its(:to_s) { should eq valid_uuid }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with invalid param' do
|
50
|
+
subject { -> { action } }
|
51
|
+
context 'with invalid characters' do
|
52
|
+
let(:encoded_param) { ' ' }
|
53
|
+
it { should raise_error Youyouaidi::InvalidUUIDError }
|
54
|
+
end
|
31
55
|
|
32
|
-
|
33
|
-
|
56
|
+
context 'with too long encoded param' do
|
57
|
+
let(:encoded_param) { "#{encoded_uuid}abc" }
|
58
|
+
it { should raise_error Youyouaidi::InvalidUUIDError }
|
34
59
|
end
|
35
60
|
end
|
36
61
|
end
|
@@ -7,15 +7,24 @@ describe Youyouaidi::UUID do
|
|
7
7
|
subject { -> { action } }
|
8
8
|
|
9
9
|
context 'with valid uuid string' do
|
10
|
-
let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
|
11
10
|
subject { action }
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
uuid_string = '550e8400-e29b-41d4-a716-446655440000'
|
13
|
+
encoded_uuid = '2AuYQJcZeiIeCymkJ7tzTW'
|
14
|
+
valid_uuids = ['550e8400-e29b-41d4-a716-446655440000',
|
15
|
+
'00000000-bbbb-2222-8888-000000000000']
|
16
|
+
|
17
|
+
valid_uuids.each do |valid_uuid|
|
18
|
+
context "for uuid `#{valid_uuid}'" do
|
19
|
+
let(:param) { valid_uuid }
|
20
|
+
it { should be_a Youyouaidi::UUID }
|
21
|
+
its(:to_s) { should eq param }
|
22
|
+
end
|
23
|
+
end
|
15
24
|
end
|
16
25
|
|
17
26
|
context 'with uuid in short format' do
|
18
|
-
let(:param) { '
|
27
|
+
let(:param) { '2AuYQJcZeiIeCymkJ7tzTW' }
|
19
28
|
it { should raise_error Youyouaidi::InvalidUUIDError }
|
20
29
|
end
|
21
30
|
|
@@ -42,7 +51,7 @@ describe Youyouaidi::UUID do
|
|
42
51
|
end
|
43
52
|
|
44
53
|
context 'with uuid in short format' do
|
45
|
-
let(:param) { '
|
54
|
+
let(:param) { '2AuYQJcZeiIeCymkJ7tzTW' }
|
46
55
|
let(:decoded_uuid) { '550e8400-e29b-41d4-a716-446655440000' }
|
47
56
|
|
48
57
|
it { should be_a Youyouaidi::UUID }
|
@@ -68,7 +77,7 @@ describe Youyouaidi::UUID do
|
|
68
77
|
|
69
78
|
shared_examples_for 'equality check for two UUID objects' do
|
70
79
|
let(:first_uuid_string) { 'aaaaaaaa-eeee-4444-aaaa-444444444444' }
|
71
|
-
let(:second_uuid_string) { '00000000-bbbb-2222-
|
80
|
+
let(:second_uuid_string) { '00000000-bbbb-2222-8888-000000000000' }
|
72
81
|
let(:first_uuid) { Youyouaidi::UUID.new first_uuid_string }
|
73
82
|
let(:second_uuid) { Youyouaidi::UUID.new second_uuid_string }
|
74
83
|
|
@@ -168,7 +177,7 @@ describe Youyouaidi::UUID do
|
|
168
177
|
|
169
178
|
shared_examples_for 'method for short format' do
|
170
179
|
let(:uuid_string) { '550e8400-e29b-41d4-a716-446655440000' }
|
171
|
-
let(:encoded_uuid) { '
|
180
|
+
let(:encoded_uuid) { '2AuYQJcZeiIeCymkJ7tzTW' }
|
172
181
|
let(:uuid) { Youyouaidi::UUID.new uuid_string }
|
173
182
|
|
174
183
|
let(:action) { uuid.send method }
|
@@ -198,7 +207,7 @@ describe Youyouaidi::UUID do
|
|
198
207
|
|
199
208
|
context 'with invalid uuid' do
|
200
209
|
uuid_string = '550e8400-e29b-41d4-a716-446655440000'
|
201
|
-
encoded_uuid = '
|
210
|
+
encoded_uuid = '2AuYQJcZeiIeCymkJ7tzTW'
|
202
211
|
invalid_uuids = ['Kekse', "aa#{uuid_string}", "#{uuid_string}bb",
|
203
212
|
"#{encoded_uuid}" ]
|
204
213
|
|
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.1.0
|
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-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|