youyouaidi 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -10
- data/lib/kernel_patch.rb +1 -1
- data/lib/youyouaidi/uuid.rb +21 -5
- data/lib/youyouaidi/version.rb +1 -1
- data/spec/kernel_patch_spec.rb +5 -0
- data/spec/youyouaidi/uuid_spec.rb +22 -3
- 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: 9bc980217243ad4ab182d95a4bc4e1c32ca04dc9
|
4
|
+
data.tar.gz: 6bad0870b6e0cf45e6fc8f3d783c0253d9e072d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1715bbde2ec432104c9d3012940fa4e9f51bd5e930171885136b288507de1b76f80905a15973158d7b30e978ade65e06221a55028cfd0f2b539659ad49a3a2a2
|
7
|
+
data.tar.gz: 11a1b217560ebfb1142b9405df2d186a8a60131161802530145dbbf86b534ff53804a99672dcc117a3ffa3104458bd551da586b2ff0dc2bc9ddbbb774ac15242
|
data/README.md
CHANGED
@@ -1,13 +1,31 @@
|
|
1
|
-
# Youyouaidi
|
2
|
-
[](https://rubygems.org/gems/youyouaidi)
|
1
|
+
# Youyouaidi
|
2
|
+
[](https://rubygems.org/gems/youyouaidi)
|
3
3
|
[](https://travis-ci.org/nicolas-fricke/youyouaidi)
|
4
4
|
[](https://coveralls.io/r/nicolas-fricke/youyouaidi)
|
5
5
|
[](https://codeclimate.com/github/nicolas-fricke/youyouaidi)
|
6
6
|
[](https://tldrlegal.com/license/mit-license)
|
7
7
|
|
8
8
|
|
9
|
-
`Youyouaidi` is a Ruby Gem that offers a UUID class for parsing, validating and converting UUIDs into / from shorter representations.
|
10
|
-
|
9
|
+
`Youyouaidi` is a Ruby Gem that offers a UUID class for generating, parsing, validating and converting UUIDs into / from shorter representations.
|
10
|
+
|
11
|
+
While a UUID consists of 32 hexadecimal characters by dashes divided into 5 subgroups - which makes 36 characters in total - the short representation (invoked via `#to_shrort_string`) consists of exactly 22 digit and lower- and uppercase characters.
|
12
|
+
|
13
|
+
For UUID generation, the `SecureRandom.uuid` method is used which generates valid, random *version 4* UUIDs.
|
14
|
+
|
15
|
+
This is what a valid, random (version 4) UUID looks like:
|
16
|
+
```
|
17
|
+
# chars in group: 8 | 4 | 4 | 4 | 12
|
18
|
+
▼ ▼ ▼ ▼ ▼
|
19
|
+
caed3f49-b0ca-454b-adf8-5ee2a1764759
|
20
|
+
▲ ▲
|
21
|
+
version either 8, 9
|
22
|
+
number a, or b
|
23
|
+
```
|
24
|
+
As shown, the first digit of the third group indicates the UUID version.
|
25
|
+
The first digit of the fourth group always has to be one of either `8`, `9`, `a`, or `b`.
|
26
|
+
All other digits are randomly assigned hexadecimals.
|
27
|
+
|
28
|
+
Find out more about UUIDs and the different versions on [Wikipedia](https://en.wikipedia.org/wiki/Uuid).
|
11
29
|
|
12
30
|
## Installation
|
13
31
|
|
@@ -32,11 +50,15 @@ uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string fo
|
|
32
50
|
uuid_short = '2AuYQJcZeiIeCymkJ7tzTW' # Same UUID in its short format, has exactly 22 characters of [0-9a-zA-Z]
|
33
51
|
|
34
52
|
uuid = UUID uuid_string # creates new Youyouaidi::UUID object, patches Youyouaidi::UUID.parse uuid_string into kernel.
|
35
|
-
# => #<Youyouaidi::UUID:
|
53
|
+
# => #<Youyouaidi::UUID:0x000001021f2590 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
|
36
54
|
|
37
55
|
# Alternatively a short UUID can be passed:
|
38
56
|
uuid = UUID uuid_short # creates similar Youyouaidi::UUID object
|
39
|
-
# => #<Youyouaidi::UUID:
|
57
|
+
# => #<Youyouaidi::UUID:0x00000102201b80 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
|
58
|
+
|
59
|
+
# To generate a new random UUID simply do not pass a parameter:
|
60
|
+
new_uuid = UUID() # generates a random UUID version 4 using the SecureRandom.uuid method
|
61
|
+
# => #<Youyouaidi::UUID:0x00000102201b80 @converter=Youyouaidi::Converter, @uuid="27f8bc29-be8e-4dc7-ab30-0295b2a5e902">
|
40
62
|
```
|
41
63
|
|
42
64
|
|
@@ -48,10 +70,10 @@ uuid = UUID uuid_string
|
|
48
70
|
|
49
71
|
Youyouaidi::UUID.valid? uuid_string # => true
|
50
72
|
|
51
|
-
uuid.to_s
|
73
|
+
uuid.to_s # Returns the string representation of the UUID object
|
52
74
|
# => '550e8400-e29b-41d4-a716-446655440000'
|
53
75
|
|
54
|
-
uuid.to_short_s
|
76
|
+
uuid.to_short_s # Returns the short string representation of the UUID object
|
55
77
|
# => '2AuYQJcZeiIeCymkJ7tzTW', alias for method: #to_param
|
56
78
|
```
|
57
79
|
|
@@ -62,7 +84,7 @@ uuid.to_short_s # Returns the short string representation of the UUID
|
|
62
84
|
uuid_string = '550e8400-e29b-41d4-a716-446655440000' # A valid UUID in string format
|
63
85
|
uuid = UUID uuid_string
|
64
86
|
similar_uuid = UUID uuid_string
|
65
|
-
other_uuid = UUID '
|
87
|
+
other_uuid = UUID '00000000-1111-2222-aaaa-eeeeeeeeeeee'
|
66
88
|
|
67
89
|
uuid == similar_uuid # Two UUID objects representing same UUID (#=== behaves similar for this)
|
68
90
|
# => true
|
@@ -79,7 +101,7 @@ uuid === uuid_string # Comparing a UUID object and its string representation wit
|
|
79
101
|
|
80
102
|
## Contributing
|
81
103
|
|
82
|
-
1. Fork it ( http://github.com
|
104
|
+
1. Fork it ( http://github.com/nicolas-fricke/youyouaidi/fork )
|
83
105
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
84
106
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
85
107
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/kernel_patch.rb
CHANGED
data/lib/youyouaidi/uuid.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
1
3
|
class Youyouaidi::UUID
|
2
4
|
attr_reader :uuid
|
3
5
|
|
4
|
-
def initialize(uuid_string, options = {})
|
6
|
+
def initialize(uuid_string = nil, options = {})
|
5
7
|
@converter = options[:converter] || Youyouaidi::Converter
|
6
|
-
|
7
|
-
|
8
|
+
if uuid_string
|
9
|
+
initialize_with_uuid_string uuid_string
|
10
|
+
else
|
11
|
+
initialize_without_param
|
12
|
+
end
|
8
13
|
end
|
9
14
|
|
10
15
|
def ==(other_object)
|
@@ -32,11 +37,22 @@ class Youyouaidi::UUID
|
|
32
37
|
|
33
38
|
private
|
34
39
|
|
40
|
+
def initialize_without_param
|
41
|
+
@uuid = SecureRandom.uuid
|
42
|
+
end
|
43
|
+
|
44
|
+
def initialize_with_uuid_string(uuid_string)
|
45
|
+
raise Youyouaidi::InvalidUUIDError.new "`#{uuid_string}' could not be converted to valid UUID" unless self.class.valid? uuid_string
|
46
|
+
@uuid = uuid_string.to_s.downcase
|
47
|
+
end
|
48
|
+
|
35
49
|
class << self
|
36
|
-
def parse(uuid_param, options = {})
|
50
|
+
def parse(uuid_param = nil, options = {})
|
37
51
|
@converter = options[:converter] || Youyouaidi::Converter
|
38
52
|
if valid? uuid_param
|
39
53
|
self.new uuid_param.to_s, options
|
54
|
+
elsif uuid_param.nil?
|
55
|
+
self.new nil, options
|
40
56
|
else
|
41
57
|
uuid_obj = @converter.decode uuid_param
|
42
58
|
raise Youyouaidi::InvalidUUIDError.new "`#{uuid_param}' could not be converted to valid UUID" unless valid? uuid_obj.to_s
|
@@ -45,7 +61,7 @@ class Youyouaidi::UUID
|
|
45
61
|
end
|
46
62
|
|
47
63
|
def valid?(uuid_canidate)
|
48
|
-
(uuid_canidate.to_s =~ /^[
|
64
|
+
(uuid_canidate.to_s =~ /^[\da-f]{8}(-[\da-f]{4}){2}-[89ab][\da-f]{3}-[\da-f]{12}$/i) == 0
|
49
65
|
end
|
50
66
|
end
|
51
67
|
end
|
data/lib/youyouaidi/version.rb
CHANGED
data/spec/kernel_patch_spec.rb
CHANGED
@@ -14,6 +14,11 @@ describe Kernel do
|
|
14
14
|
its(:to_s) { should eq valid_uuid }
|
15
15
|
end
|
16
16
|
|
17
|
+
context 'initialized without a param' do
|
18
|
+
let(:action) { UUID() }
|
19
|
+
it { should be_a Youyouaidi::UUID }
|
20
|
+
end
|
21
|
+
|
17
22
|
context 'initialized via valid uuid' do
|
18
23
|
let(:param) { valid_uuid }
|
19
24
|
it_behaves_like 'a valid UUID object'
|
@@ -6,6 +6,20 @@ describe Youyouaidi::UUID do
|
|
6
6
|
let(:action) { Youyouaidi::UUID.new param }
|
7
7
|
subject { -> { action } }
|
8
8
|
|
9
|
+
context 'without a param' do
|
10
|
+
let(:action) { Youyouaidi::UUID.new }
|
11
|
+
subject { action }
|
12
|
+
|
13
|
+
it { should be_a Youyouaidi::UUID }
|
14
|
+
describe 'having a valid UUID version 4' do
|
15
|
+
subject { action.to_s }
|
16
|
+
|
17
|
+
it { should be_a String }
|
18
|
+
it { should have(36).characters }
|
19
|
+
it { should match /[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}/i }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
9
23
|
context 'with valid uuid string' do
|
10
24
|
subject { action }
|
11
25
|
|
@@ -200,16 +214,21 @@ describe Youyouaidi::UUID do
|
|
200
214
|
subject { Youyouaidi::UUID.valid? param }
|
201
215
|
|
202
216
|
context 'with valid uuid' do
|
203
|
-
|
217
|
+
valid_uuids = ['550e8400-e29b-41d4-a716-446655440000',
|
218
|
+
'27f8bc29-be8e-4dc7-8b30-0295b2a5e902']
|
204
219
|
|
205
|
-
|
220
|
+
valid_uuids.each do |valid_uuid|
|
221
|
+
it "should return true for `#{valid_uuid}`" do
|
222
|
+
expect(Youyouaidi::UUID.valid? valid_uuid).to eq true
|
223
|
+
end
|
224
|
+
end
|
206
225
|
end
|
207
226
|
|
208
227
|
context 'with invalid uuid' do
|
209
228
|
uuid_string = '550e8400-e29b-41d4-a716-446655440000'
|
210
229
|
encoded_uuid = '2AuYQJcZeiIeCymkJ7tzTW'
|
211
230
|
invalid_uuids = ['Kekse', "aa#{uuid_string}", "#{uuid_string}bb",
|
212
|
-
"#{encoded_uuid}" ]
|
231
|
+
"#{encoded_uuid}", '550e8400-e29b-41d4-2716-446655440000']
|
213
232
|
|
214
233
|
invalid_uuids.each do |invalid_uuid|
|
215
234
|
it "should return false for `#{invalid_uuid}`" do
|
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.1.
|
4
|
+
version: 0.1.1
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|