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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 383f96781da456cf785f37008b07cea302bc6465
4
- data.tar.gz: 63ad6a4540f8f1740b5e9fb0477c610203c93a32
3
+ metadata.gz: 9bc980217243ad4ab182d95a4bc4e1c32ca04dc9
4
+ data.tar.gz: 6bad0870b6e0cf45e6fc8f3d783c0253d9e072d0
5
5
  SHA512:
6
- metadata.gz: 98d5f4460aade04d5bd5b3d458057de3e04321968d7a65194ff26161ca219b6e2a6b1e0b259641eb24e9c8e5a55d8080f123f6821f1449ce5751ad9266531c4f
7
- data.tar.gz: 769b6f76f82a8add80421b20ac845487cfa07138cd1286a5e6fb62d982120da90b9f3f90e2ec4c33a19ca2db85cdc0ec9f7c4a8a008bb72b18d7387f602bcf21
6
+ metadata.gz: 1715bbde2ec432104c9d3012940fa4e9f51bd5e930171885136b288507de1b76f80905a15973158d7b30e978ade65e06221a55028cfd0f2b539659ad49a3a2a2
7
+ data.tar.gz: 11a1b217560ebfb1142b9405df2d186a8a60131161802530145dbbf86b534ff53804a99672dcc117a3ffa3104458bd551da586b2ff0dc2bc9ddbbb774ac15242
data/README.md CHANGED
@@ -1,13 +1,31 @@
1
- # Youyouaidi
2
- [![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)
3
3
  [![Build Status](https://travis-ci.org/nicolas-fricke/youyouaidi.svg)](https://travis-ci.org/nicolas-fricke/youyouaidi)
4
4
  [![Coverage Status](http://img.shields.io/coveralls/nicolas-fricke/youyouaidi.svg)](https://coveralls.io/r/nicolas-fricke/youyouaidi)
5
5
  [![Code Climate](http://img.shields.io/codeclimate/github/nicolas-fricke/youyouaidi.svg)](https://codeclimate.com/github/nicolas-fricke/youyouaidi)
6
6
  [![License](http://img.shields.io/badge/license-MIT-brightgreen.svg)](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
- 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.
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:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
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:0x0000010150bb60 @converter=Youyouaidi::Converter, @uuid="550e8400-e29b-41d4-a716-446655440000">
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 # Returns the string representation of the UUID object
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 # Returns the short string representation of the UUID object
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 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
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/<my-github-username>/youyouaidi/fork )
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
@@ -1,7 +1,7 @@
1
1
  require 'youyouaidi'
2
2
 
3
3
  module Kernel
4
- def UUID(uuid_param)
4
+ def UUID(uuid_param = nil)
5
5
  Youyouaidi::UUID.parse uuid_param
6
6
  end
7
7
  end
@@ -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
- 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.downcase
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 =~ /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i) == 0
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
@@ -1,3 +1,3 @@
1
1
  module Youyouaidi
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -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
- let(:param) { '550e8400-e29b-41d4-a716-446655440000' }
217
+ valid_uuids = ['550e8400-e29b-41d4-a716-446655440000',
218
+ '27f8bc29-be8e-4dc7-8b30-0295b2a5e902']
204
219
 
205
- it { should be_true }
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.0
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-08 00:00:00.000000000 Z
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler