uuid4 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +74 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/uuid4.rb +117 -0
- data/lib/uuid4/formatter/base62.rb +19 -0
- data/lib/uuid4/formatter/compact.rb +18 -0
- data/lib/uuid4/formatter/default.rb +18 -0
- data/lib/uuid4/formatter/urn.rb +18 -0
- data/lib/uuid4/version.rb +13 -0
- data/uuid4.gemspec +26 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ac25cad8ab5ca5765df723b76156865d2e00b74e
|
4
|
+
data.tar.gz: a82c2472bc503adb59d8a2e6f93acb60583d53a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 785f5539dc4d1eafc224da493f6d527f1a03655831090f581f687d88bbde9775fc367c6484f2907dbc63b20304d164786d2ac0b2a4e9ebf41b5a5be602899543
|
7
|
+
data.tar.gz: e8a0175a0caf5c729e0820521bd2636a9aa435c52c196e20d8c2b2f21ee40d21c8a392996047539b8f5585a8c5579530080fe944417eab785a71bef365cbbfd7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Jan Graichen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# UUID4
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/jgraichen/uuid4.svg?branch=master)](https://travis-ci.org/jgraichen/uuid4)
|
4
|
+
|
5
|
+
A UUID support library specialized on v4 UUIDs, parsing and formatting existing UUIDs. It can encode and decode dashed UUIDs, compact UUIDs, large integers, UUID URNs and Base62 coded UUIDs.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'uuid4'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install uuid4
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
uuid = UUID4('d40aa316-6e30-4bae-a673-7b2cf35a8f99')
|
25
|
+
# => <UUID4:d40aa316-6e30-4bae-a673-7b2cf35a8f99>
|
26
|
+
|
27
|
+
uuid.to_s
|
28
|
+
# => "d40aa316-6e30-4bae-a673-7b2cf35a8f99"
|
29
|
+
|
30
|
+
uuid.to_s(format: :compact)
|
31
|
+
# => "d40aa3166e304baea6737b2cf35a8f99"
|
32
|
+
|
33
|
+
uuid.to_s(format: :base62)
|
34
|
+
# => "6s7exj9M9mGqLs5KhhWWaB"
|
35
|
+
|
36
|
+
uuid.to_s(format: :urn)
|
37
|
+
# => "urn:uuid:d40aa316-6e30-4bae-a673-7b2cf35a8f99"
|
38
|
+
|
39
|
+
uuid.to_i
|
40
|
+
# => 281851565884874220786890141677225938841
|
41
|
+
|
42
|
+
uuid == 'd40aa316-6e30-4bae-a673-7b2cf35a8f99'
|
43
|
+
# => true
|
44
|
+
|
45
|
+
'd40aa316-6e30-4bae-a673-7b2cf35a8f99' == uuid
|
46
|
+
# => true
|
47
|
+
|
48
|
+
uuid == "d40aa3166e304baea6737b2cf35a8f99"
|
49
|
+
# => true
|
50
|
+
|
51
|
+
uuid == '6s7exj9M9mGqLs5KhhWWaB'
|
52
|
+
# => true
|
53
|
+
|
54
|
+
uuid == 281851565884874220786890141677225938841
|
55
|
+
# => true
|
56
|
+
|
57
|
+
uuid == 'urn:uuid:d40aa316-6e30-4bae-a673-7b2cf35a8f99'
|
58
|
+
# => true
|
59
|
+
|
60
|
+
uuid == UUID4('d40aa316-6e30-4bae-a673-7b2cf35a8f99')
|
61
|
+
# => true
|
62
|
+
```
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jgraichen/uuid4.
|
67
|
+
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
(c) Jan Graichen
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
74
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "uuid4"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/uuid4.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'uuid4/version'
|
2
|
+
|
3
|
+
class UUID4
|
4
|
+
module Formatter
|
5
|
+
require 'uuid4/formatter/default'
|
6
|
+
require 'uuid4/formatter/compact'
|
7
|
+
require 'uuid4/formatter/base62'
|
8
|
+
require 'uuid4/formatter/urn'
|
9
|
+
end
|
10
|
+
|
11
|
+
FORMATTERS = [
|
12
|
+
Formatter::Default.new,
|
13
|
+
Formatter::Compact.new,
|
14
|
+
Formatter::URN.new,
|
15
|
+
Formatter::Base62.new
|
16
|
+
]
|
17
|
+
|
18
|
+
def initialize(value)
|
19
|
+
@value = value
|
20
|
+
end
|
21
|
+
|
22
|
+
def ==(other)
|
23
|
+
return value === other.to_i if other.is_a?(UUID4)
|
24
|
+
return other.to_uuid4 == self if other.respond_to?(:to_uuid4)
|
25
|
+
|
26
|
+
self.class._parse(other) === value
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_str(format: :default, formatter: nil)
|
30
|
+
case format
|
31
|
+
when :default
|
32
|
+
formatter = FORMATTERS[0]
|
33
|
+
when :compact
|
34
|
+
formatter = FORMATTERS[1]
|
35
|
+
when :urn
|
36
|
+
formatter = FORMATTERS[2]
|
37
|
+
when :base62
|
38
|
+
formatter = FORMATTERS[3]
|
39
|
+
else
|
40
|
+
raise RuntimeError.new("Unknown format: #{format}")
|
41
|
+
end
|
42
|
+
|
43
|
+
formatter.encode(self)
|
44
|
+
end
|
45
|
+
|
46
|
+
alias_method :to_s, :to_str
|
47
|
+
alias_method :to_uuid, :to_str
|
48
|
+
|
49
|
+
def to_uuid4
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_int
|
54
|
+
@value
|
55
|
+
end
|
56
|
+
|
57
|
+
alias_method :to_i, :to_int
|
58
|
+
|
59
|
+
def inspect
|
60
|
+
"<UUID4:#{to_s}>"
|
61
|
+
end
|
62
|
+
|
63
|
+
def components
|
64
|
+
[
|
65
|
+
(value >> 96) & 0xFFFFFFFF,
|
66
|
+
(value >> 80) & 0xFFFF,
|
67
|
+
(value >> 64) & 0xFFFF,
|
68
|
+
(value >> 48) & 0xFFFF,
|
69
|
+
(value >> 0) & 0xFFFFFFFFFFFF
|
70
|
+
]
|
71
|
+
end
|
72
|
+
|
73
|
+
protected
|
74
|
+
|
75
|
+
attr_reader :value
|
76
|
+
|
77
|
+
class << self
|
78
|
+
def new(value = nil)
|
79
|
+
if value.nil?
|
80
|
+
super(SecureRandom.uuid.tr('-', '').hex)
|
81
|
+
elsif value.is_a?(UUID4)
|
82
|
+
value
|
83
|
+
elsif value.respond_to?(:to_uuid4)
|
84
|
+
value.to_uuid4
|
85
|
+
elsif value.respond_to?(:to_int) && valid_int?(value = value.to_int)
|
86
|
+
super(value)
|
87
|
+
elsif (ret = _parse(value))
|
88
|
+
super(ret)
|
89
|
+
else
|
90
|
+
raise TypeError.new "Invalid UUID: #{value.inspect}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def _parse(value)
|
95
|
+
if value.respond_to?(:to_int) && valid_int?(value = value.to_int)
|
96
|
+
return value
|
97
|
+
end
|
98
|
+
|
99
|
+
FORMATTERS.each do |formatter|
|
100
|
+
val = formatter.decode(value) if formatter.respond_to?(:decode)
|
101
|
+
return val if val
|
102
|
+
end
|
103
|
+
|
104
|
+
nil
|
105
|
+
end
|
106
|
+
|
107
|
+
def valid_int?(int)
|
108
|
+
int.to_s(16) =~ Formatter::Compact::REGEXP
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
module Kernel
|
114
|
+
def UUID4(value)
|
115
|
+
UUID4.new(value)
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'base62-rb'
|
2
|
+
|
3
|
+
class UUID4
|
4
|
+
module Formatter
|
5
|
+
class Base62
|
6
|
+
REGEXP = /^[0-9A-z]{22}$/i
|
7
|
+
|
8
|
+
def encode(uuid)
|
9
|
+
::Base62.encode(uuid.to_i)
|
10
|
+
end
|
11
|
+
|
12
|
+
def decode(value)
|
13
|
+
if value.respond_to?(:to_str) && (value = value.to_str) =~ REGEXP
|
14
|
+
::Base62.decode(value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class UUID4
|
2
|
+
module Formatter
|
3
|
+
class Compact
|
4
|
+
REGEXP = /^[\da-f]{12}[1-5][\da-f]{3}[89ab][\da-f]{15}$/i
|
5
|
+
FORMAT = '%032x'.freeze
|
6
|
+
|
7
|
+
def encode(uuid)
|
8
|
+
FORMAT % uuid.to_int
|
9
|
+
end
|
10
|
+
|
11
|
+
def decode(value)
|
12
|
+
if value.respond_to?(:to_str) && (value = value.to_str) =~ REGEXP
|
13
|
+
value.hex
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class UUID4
|
2
|
+
module Formatter
|
3
|
+
class Default
|
4
|
+
REGEXP = /^[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i
|
5
|
+
FORMAT = '%08x-%04x-%04x-%04x-%012x'.freeze
|
6
|
+
|
7
|
+
def encode(uuid)
|
8
|
+
FORMAT % uuid.components
|
9
|
+
end
|
10
|
+
|
11
|
+
def decode(value)
|
12
|
+
if value.respond_to?(:to_str) && (value = value.to_str) =~ REGEXP
|
13
|
+
value.tr('-', '').hex
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class UUID4
|
2
|
+
module Formatter
|
3
|
+
class URN
|
4
|
+
REGEXP = /^urn:uuid:[\da-f]{8}-[\da-f]{4}-?[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i
|
5
|
+
FORMAT = 'urn:uuid:%08x-%04x-%04x-%04x-%012x'.freeze
|
6
|
+
|
7
|
+
def encode(uuid)
|
8
|
+
FORMAT % uuid.components
|
9
|
+
end
|
10
|
+
|
11
|
+
def decode(value)
|
12
|
+
if value.respond_to?(:to_str) && (value = value.to_str) =~ REGEXP
|
13
|
+
value[9..-1].tr('-', '').hex
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/uuid4.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'uuid4/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'uuid4'
|
8
|
+
spec.version = UUID4::VERSION
|
9
|
+
spec.authors = ['Jan Graichen']
|
10
|
+
spec.email = ['jg@altimos.de']
|
11
|
+
|
12
|
+
spec.summary = %q{A UUIDv4 support library}
|
13
|
+
spec.homepage = 'https://github.com/jgraichen/uuid4'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = %w(lib)
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'base62-rb', '~> 0.3.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uuid4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Graichen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: base62-rb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- jg@altimos.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/uuid4.rb
|
86
|
+
- lib/uuid4/formatter/base62.rb
|
87
|
+
- lib/uuid4/formatter/compact.rb
|
88
|
+
- lib/uuid4/formatter/default.rb
|
89
|
+
- lib/uuid4/formatter/urn.rb
|
90
|
+
- lib/uuid4/version.rb
|
91
|
+
- uuid4.gemspec
|
92
|
+
homepage: https://github.com/jgraichen/uuid4
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.6.4
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: A UUIDv4 support library
|
116
|
+
test_files: []
|