ulid-ruby 0.1.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +89 -0
- data/Rakefile +13 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ulid.rb +86 -0
- data/lib/ulid/compare.rb +36 -0
- data/lib/ulid/constants.rb +21 -0
- data/lib/ulid/generate.rb +71 -0
- data/lib/ulid/identifier.rb +64 -0
- data/lib/ulid/parse.rb +52 -0
- data/lib/ulid/version.rb +4 -0
- data/logo.png +0 -0
- data/ulid-ruby.gemspec +28 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f27c1f9f3fee9102f8c53b8ba84a345ddc549c09
|
4
|
+
data.tar.gz: 53310f5d7c8704de8655f05adb40b42ca59e3215
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6ea6845c4f1acd02f8252dd3e1918bc5d08039a0c44a923a6675ab096c5d800cda8e295383d81dc777d51645897754fb40e42d6be9a9744a9fe993ac555cf273
|
7
|
+
data.tar.gz: 8789d36731cddf75cdeb30c5c6176ef6f2b1c770c026c2019b2fcc747529c0cc38be89ca54ba3d7e1c7db1ed5bbe847004fd3db963737668618ab06090183f37
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.0
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at adam.bachman@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Adam Bachman
|
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,89 @@
|
|
1
|
+
<h1 align="center">
|
2
|
+
<br>
|
3
|
+
<br>
|
4
|
+
<img width="360" src="logo.png" alt="ulid">
|
5
|
+
<br>
|
6
|
+
<br>
|
7
|
+
<br>
|
8
|
+
</h1>
|
9
|
+
|
10
|
+
## -- unofficial --
|
11
|
+
|
12
|
+
# ULID
|
13
|
+
|
14
|
+
A ULID is a "Universally Unique Lexicographically-sortable Identifier." This is a thin Ruby library for generating and parsing ULID values. This code is based on the original concept presented at https://github.com/alizain/ulid and in part based on code from the C# and Go projects at https://github.com/RobThree/NUlid and https://github.com/oklog/ulid respectively.
|
15
|
+
|
16
|
+
**NOTE:** while the ULID values generated are compatible with the existing Ruby ULID library located at https://github.com/rafaelsales/ulid, this library is not code-compatible. I needed some additional features for a project and it was easier to just rebuild the functionality. May not be useful for anyone else but it's working for us in production at https://io.adafruit.com.
|
17
|
+
|
18
|
+
In its string representation, it's a compact, URL-friendly, Base32, unique ID string that encodes its time of creation and sorts according the time value it encodes.
|
19
|
+
|
20
|
+
A ULID string looks like this:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'ulid'
|
24
|
+
ULID.generate #=> "0DHWZV7PGEAFYDYH04X7Y468GQ"
|
25
|
+
```
|
26
|
+
|
27
|
+
## Short Explanation
|
28
|
+
|
29
|
+
The two parts of a ULID are **Timestamp** and **Entropy**.
|
30
|
+
|
31
|
+
0DHWZV7PGE AFYDYH04X7Y468GQ
|
32
|
+
|----------| |----------------|
|
33
|
+
Timestamp Entropy
|
34
|
+
48bits 80bits
|
35
|
+
|
36
|
+
### Timestamp
|
37
|
+
|
38
|
+
- Encoded in first 48 bits of ULID. In Base32 it's the first 10 ASCII characters.
|
39
|
+
- UNIX-time with a precision of milliseconds.
|
40
|
+
- Won't run out of space till the year 10895 AD.
|
41
|
+
|
42
|
+
### Entropy
|
43
|
+
|
44
|
+
- Encoded in last 80 bits of ULID. In Base32 it's the last 16 ASCII characters.
|
45
|
+
- Should use cryptographically secure source of randomness (this library uses the Ruby Standard Library's `SecureRandom` class)
|
46
|
+
- Unlikely to duplicate even with millions of IDs at the same millisecond
|
47
|
+
|
48
|
+
### Sorting
|
49
|
+
|
50
|
+
The left-most character must be sorted first, and the right-most character sorted last (lexical order). The default ASCII character set must be used. Within the same millisecond, sort order is not guaranteed.
|
51
|
+
|
52
|
+
## Installation
|
53
|
+
|
54
|
+
Add this line to your application's Gemfile:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
gem 'ulid-ruby', require: 'ulid'
|
58
|
+
```
|
59
|
+
|
60
|
+
And then execute:
|
61
|
+
|
62
|
+
$ bundle
|
63
|
+
|
64
|
+
Or install it yourself as:
|
65
|
+
|
66
|
+
$ gem install ulid-ruby
|
67
|
+
|
68
|
+
And require from your project with:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require 'rubygems'
|
72
|
+
require 'ulid'
|
73
|
+
```
|
74
|
+
|
75
|
+
## Development
|
76
|
+
|
77
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
78
|
+
|
79
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/abachman/ulid-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
84
|
+
|
85
|
+
|
86
|
+
## License
|
87
|
+
|
88
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
89
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require 'yard'
|
4
|
+
|
5
|
+
YARD::Rake::YardocTask.new do |t|
|
6
|
+
t.files = ['lib/**/*.rb'] # optional
|
7
|
+
t.options = ['--any', '--extra', '--opts'] # optional
|
8
|
+
t.stats_options = ['--list-undoc'] # optional
|
9
|
+
end
|
10
|
+
|
11
|
+
RSpec::Core::RakeTask.new(:spec)
|
12
|
+
|
13
|
+
task :default => :spec
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ulid"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/ulid.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
|
2
|
+
require 'time'
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
require 'ulid/version'
|
6
|
+
require 'ulid/constants'
|
7
|
+
require 'ulid/identifier'
|
8
|
+
require 'ulid/generate'
|
9
|
+
require 'ulid/parse'
|
10
|
+
require 'ulid/compare'
|
11
|
+
|
12
|
+
module ULID
|
13
|
+
include Constants
|
14
|
+
|
15
|
+
# Get a new, randomized ULID::Identifier instance at the current time.
|
16
|
+
#
|
17
|
+
# @return [ULID::Identifier] new ULID::Identifier instance for the current system time
|
18
|
+
#
|
19
|
+
# @example Generate a ULID
|
20
|
+
# ULID.new #=> #<ULID::Identifier:0x007f83f90aecc0 ....>
|
21
|
+
#
|
22
|
+
def self.new
|
23
|
+
Identifier.new
|
24
|
+
end
|
25
|
+
|
26
|
+
# Get a new, randomized ULID string at the current time.
|
27
|
+
#
|
28
|
+
# @return [String] 26 character ULID value for the current system time
|
29
|
+
#
|
30
|
+
# @example Generate a ULID string
|
31
|
+
# ULID.generate #=> 0DHWZV7PGEAFYDYH04X7Y468GQ
|
32
|
+
#
|
33
|
+
def self.generate
|
34
|
+
Identifier.new.ulid
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get a string ULID encoding the given time.
|
38
|
+
#
|
39
|
+
# @param at_time [Time] the time to encode
|
40
|
+
# @return [String] new ULID string encoding the given time
|
41
|
+
#
|
42
|
+
# @example Generate a ULID string for a given time
|
43
|
+
# ULID.at(Time.at(1_000_000)) #=> 0009A0QS00SGEFTGMFFEAS6B9A
|
44
|
+
#
|
45
|
+
def self.at(at_time)
|
46
|
+
Identifier.new(at_time).ulid
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get the Time value encoded by the given ULID string.
|
50
|
+
#
|
51
|
+
# @param ulid [String or ULID::Identifier]
|
52
|
+
# @return [Time] UTC time value encoded by the ULID
|
53
|
+
#
|
54
|
+
# @example Parse a ULID string and get a time value
|
55
|
+
# ULID.time '0009A0QS00SGEFTGMFFEAS6B9A' #=> 1970-01-12 13:46:40 UTC
|
56
|
+
#
|
57
|
+
def self.time(ulid)
|
58
|
+
Identifier.new(ulid).time.utc
|
59
|
+
end
|
60
|
+
|
61
|
+
# Get the first possible ULID string for the given time in sort order ascending.
|
62
|
+
#
|
63
|
+
# @param at_time [Time] a Time value to encode in the ULID
|
64
|
+
# @return [String] the lexicographically minimum ULID value for the given time
|
65
|
+
#
|
66
|
+
# @example Get minimal ULID at time
|
67
|
+
# ULID.min_ulid_at Time.at(1_000_000) #=> "0009A0QS000000000000000000"
|
68
|
+
#
|
69
|
+
def self.min_ulid_at(at_time)
|
70
|
+
Identifier.new(at_time, MIN_ENTROPY).ulid
|
71
|
+
end
|
72
|
+
|
73
|
+
# Get the first possible ULID string for the given time in sort order descending.
|
74
|
+
#
|
75
|
+
# @param at_time [Time] a Time value to encode in the ULID
|
76
|
+
# @return [String] the lexicographically maximum ULID value for the given time
|
77
|
+
#
|
78
|
+
# @example Get minimal ULID at time
|
79
|
+
# ULID.max_ulid_at Time.at(1_000_000) #=> "0009A0QS00ZZZZZZZZZZZZZZZZ"
|
80
|
+
#
|
81
|
+
def self.max_ulid_at(at_time)
|
82
|
+
Identifier.new(at_time, MAX_ENTROPY).ulid
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
data/lib/ulid/compare.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module ULID
|
2
|
+
module Compare
|
3
|
+
def >(other)
|
4
|
+
case other
|
5
|
+
when self.class
|
6
|
+
self.ulid > other.ulid
|
7
|
+
when Time
|
8
|
+
self.time > other
|
9
|
+
when String
|
10
|
+
self.ulid > other
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def <(other)
|
15
|
+
case other
|
16
|
+
when self.class
|
17
|
+
self.ulid < other.ulid
|
18
|
+
when Time
|
19
|
+
self.time < other
|
20
|
+
when String
|
21
|
+
self.ulid < other
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def <=>(other)
|
26
|
+
case other
|
27
|
+
when self.class
|
28
|
+
self.ulid <=> other.ulid
|
29
|
+
when Time
|
30
|
+
self.time <=> other
|
31
|
+
when String
|
32
|
+
self.ulid <=> other
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ULID
|
2
|
+
module Constants
|
3
|
+
# smallest representable time
|
4
|
+
MIN_TIME = ([0] * 6).pack('C' * 6)
|
5
|
+
# largest representable time
|
6
|
+
MAX_TIME = ([255] * 6).pack('C' * 6)
|
7
|
+
|
8
|
+
# smallest possible seed value
|
9
|
+
MIN_ENTROPY = ([0] * 10).pack('C' * 10)
|
10
|
+
# largest possible seed value
|
11
|
+
MAX_ENTROPY = ([255] * 10).pack('C' * 10)
|
12
|
+
|
13
|
+
# Crockford's Base32. Alphabet portion is missing I, L, O, and U.
|
14
|
+
ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
15
|
+
|
16
|
+
# Byte to index table for O(1) lookups when unmarshaling.
|
17
|
+
# We rely on nil as sentinel value for invalid indexes.
|
18
|
+
B32REF = Hash[ ENCODING.split('').each_with_index.to_a ]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
require 'ulid/constants'
|
3
|
+
|
4
|
+
module ULID
|
5
|
+
|
6
|
+
module Generate
|
7
|
+
include Constants
|
8
|
+
|
9
|
+
# returns the binary ULID as Base32 encoded string.
|
10
|
+
def encode32
|
11
|
+
# Optimized unrolled loop ahead.
|
12
|
+
# From https://github.com/RobThree/NUlid
|
13
|
+
# via https://github.com/oklog/ulid/blob/c3c01856f7e43fa64133819126887124d5f05e39/ulid.go#L154
|
14
|
+
|
15
|
+
id = @bytes.bytes
|
16
|
+
|
17
|
+
output = ''
|
18
|
+
|
19
|
+
# Base32 encodes 5 bits of each byte of the input in each byte of the
|
20
|
+
# output. That means each input byte produces >1 output byte and some
|
21
|
+
# output bytes encode parts of multiple input bytes.
|
22
|
+
#
|
23
|
+
# The end result is a human / URL friendly output string that can be
|
24
|
+
# decoded into the original bytes.
|
25
|
+
|
26
|
+
# 10 byte timestamp
|
27
|
+
output << ENCODING[(id[0]&224)>>5]
|
28
|
+
output << ENCODING[id[0]&31]
|
29
|
+
output << ENCODING[(id[1]&248)>>3]
|
30
|
+
output << ENCODING[((id[1]&7)<<2)|((id[2]&192)>>6)]
|
31
|
+
output << ENCODING[(id[2]&62)>>1]
|
32
|
+
output << ENCODING[((id[2]&1)<<4)|((id[3]&240)>>4)]
|
33
|
+
output << ENCODING[((id[3]&15)<<1)|((id[4]&128)>>7)]
|
34
|
+
output << ENCODING[(id[4]&124)>>2]
|
35
|
+
output << ENCODING[((id[4]&3)<<3)|((id[5]&224)>>5)]
|
36
|
+
output << ENCODING[id[5]&31]
|
37
|
+
|
38
|
+
# 16 bytes of entropy
|
39
|
+
output << ENCODING[(id[6]&248)>>3]
|
40
|
+
output << ENCODING[((id[6]&7)<<2)|((id[7]&192)>>6)]
|
41
|
+
output << ENCODING[(id[7]&62)>>1]
|
42
|
+
output << ENCODING[((id[7]&1)<<4)|((id[8]&240)>>4)]
|
43
|
+
output << ENCODING[((id[8]&15)<<1)|((id[9]&128)>>7)]
|
44
|
+
output << ENCODING[(id[9]&124)>>2]
|
45
|
+
output << ENCODING[((id[9]&3)<<3)|((id[10]&224)>>5)]
|
46
|
+
output << ENCODING[id[10]&31]
|
47
|
+
output << ENCODING[(id[11]&248)>>3]
|
48
|
+
output << ENCODING[((id[11]&7)<<2)|((id[12]&192)>>6)]
|
49
|
+
output << ENCODING[(id[12]&62)>>1]
|
50
|
+
output << ENCODING[((id[12]&1)<<4)|((id[13]&240)>>4)]
|
51
|
+
output << ENCODING[((id[13]&15)<<1)|((id[14]&128)>>7)]
|
52
|
+
output << ENCODING[(id[14]&124)>>2]
|
53
|
+
output << ENCODING[((id[14]&3)<<3)|((id[15]&224)>>5)]
|
54
|
+
output << ENCODING[id[15]&31]
|
55
|
+
|
56
|
+
output
|
57
|
+
end
|
58
|
+
|
59
|
+
def random_bytes
|
60
|
+
SecureRandom.random_bytes(10)
|
61
|
+
end
|
62
|
+
|
63
|
+
def hundred_micro_time
|
64
|
+
(@time.to_f * 10_000).to_i
|
65
|
+
end
|
66
|
+
|
67
|
+
def time_48bit
|
68
|
+
[hundred_micro_time].pack("Q>")[2..-1]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'ulid/parse'
|
2
|
+
require 'ulid/generate'
|
3
|
+
require 'ulid/compare'
|
4
|
+
|
5
|
+
module ULID
|
6
|
+
class Identifier
|
7
|
+
include Parse
|
8
|
+
include Generate
|
9
|
+
include Compare
|
10
|
+
|
11
|
+
attr_reader :seed, :bytes, :time, :ulid
|
12
|
+
|
13
|
+
# Create a new instance of a ULID::Identifier.
|
14
|
+
#
|
15
|
+
# @param start [ULID::Instance, String, Time, or nil] if ULID instance or
|
16
|
+
# string is given, initialize to the exact same value. If Time is given,
|
17
|
+
# generate a new ULID for that time, if no argument is given, generate a
|
18
|
+
# new ULID at the current system time.
|
19
|
+
# @param seed [String or nil] a 10-byte, Encoding::ASCII_8BIT encoded string.
|
20
|
+
# The easiest way to generate a seed is to call SecureRandom.random_bytes(10)
|
21
|
+
# @return [ULID::Identifier]
|
22
|
+
def initialize(start = nil, seed = nil)
|
23
|
+
case start
|
24
|
+
when self.class
|
25
|
+
@time = start.time
|
26
|
+
@seed = start.seed
|
27
|
+
when NilClass, Time
|
28
|
+
@time = (start || Time.now).utc
|
29
|
+
if seed == nil
|
30
|
+
@seed = random_bytes
|
31
|
+
else
|
32
|
+
if seed.size != 10 || seed.encoding != Encoding::ASCII_8BIT
|
33
|
+
raise ArgumentError.new("seed error, seed value must be 10 bytes encoded as Encoding::ASCII_8BIT")
|
34
|
+
end
|
35
|
+
@seed = seed
|
36
|
+
end
|
37
|
+
when String
|
38
|
+
if start.size != 26
|
39
|
+
raise ArgumentError.new("invalid ULID string, must be 26 characters")
|
40
|
+
end
|
41
|
+
|
42
|
+
# parse string into bytes
|
43
|
+
@ulid = start.upcase
|
44
|
+
@bytes = decode(@ulid)
|
45
|
+
|
46
|
+
@time, @seed = unpack_decoded_bytes(@bytes)
|
47
|
+
else
|
48
|
+
# unrecognized initial values type given, just generate fresh ULID
|
49
|
+
@time = Time.now.utc
|
50
|
+
@seed = random_bytes
|
51
|
+
end
|
52
|
+
|
53
|
+
if @bytes.nil?
|
54
|
+
# an ASCII_8BIT encoded string, should be 16 bytes
|
55
|
+
@bytes = time_48bit + @seed
|
56
|
+
end
|
57
|
+
|
58
|
+
if @ulid.nil?
|
59
|
+
# the lexically sortable Base32 string we actually care about
|
60
|
+
@ulid = encode32
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/ulid/parse.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'ulid/constants'
|
2
|
+
|
3
|
+
module ULID
|
4
|
+
module Parse
|
5
|
+
include Constants
|
6
|
+
|
7
|
+
# v should be a Base32 encoded ULID string. This method decodes it into a
|
8
|
+
# 16 byte raw ULID with 48 bit time and 64 bit random parts.
|
9
|
+
def decode(v)
|
10
|
+
out = []
|
11
|
+
|
12
|
+
# hand optimized, unrolled ULID-Base32 format decoder based on
|
13
|
+
# https://github.com/oklog/ulid/blob/c3c01856f7e43fa64133819126887124d5f05e39/ulid.go#L234
|
14
|
+
|
15
|
+
# 6 bytes timestamp (48 bits)
|
16
|
+
out << ((B32REF[v[0]] << 5) | B32REF[v[1]])
|
17
|
+
out << ((B32REF[v[2]] << 3) | (B32REF[v[3]] >> 2))
|
18
|
+
out << ((B32REF[v[3]] << 6) | (B32REF[v[4]] << 1) | (B32REF[v[5]] >> 4))
|
19
|
+
out << ((B32REF[v[5]] << 4) | (B32REF[v[6]] >> 1))
|
20
|
+
out << ((B32REF[v[6]] << 7) | (B32REF[v[7]] << 2) | (B32REF[v[8]] >> 3))
|
21
|
+
out << ((B32REF[v[8]] << 5) | B32REF[v[9]])
|
22
|
+
|
23
|
+
# 10 bytes of entropy (80 bits)
|
24
|
+
out << ((B32REF[v[10]] << 3) | (B32REF[v[11]] >> 2))
|
25
|
+
out << ((B32REF[v[11]] << 6) | (B32REF[v[12]] << 1) | (B32REF[v[13]] >> 4))
|
26
|
+
out << ((B32REF[v[13]] << 4) | (B32REF[v[14]] >> 1))
|
27
|
+
out << ((B32REF[v[14]] << 7) | (B32REF[v[15]] << 2) | (B32REF[v[16]] >> 3))
|
28
|
+
out << ((B32REF[v[16]] << 5) | B32REF[v[17]])
|
29
|
+
out << ((B32REF[v[18]] << 3) | B32REF[v[19]]>>2)
|
30
|
+
out << ((B32REF[v[19]] << 6) | (B32REF[v[20]] << 1) | (B32REF[v[21]] >> 4))
|
31
|
+
out << ((B32REF[v[21]] << 4) | (B32REF[v[22]] >> 1))
|
32
|
+
out << ((B32REF[v[22]] << 7) | (B32REF[v[23]] << 2) | (B32REF[v[24]] >> 3))
|
33
|
+
out << ((B32REF[v[24]] << 5) | B32REF[v[25]])
|
34
|
+
|
35
|
+
# get the array as a string, coercing each value to a single byte
|
36
|
+
out = out.pack('C' * 16)
|
37
|
+
|
38
|
+
out
|
39
|
+
end
|
40
|
+
|
41
|
+
def unpack_decoded_bytes(packed_bytes)
|
42
|
+
time_bytes = packed_bytes[0..5]
|
43
|
+
seed = packed_bytes[6..-1]
|
44
|
+
|
45
|
+
# and unpack it immedieately into the original milliseconds timestamp
|
46
|
+
time_int = ("\x00\x00" + time_bytes).unpack('Q>')[0]
|
47
|
+
|
48
|
+
[ Time.at( time_int / 10_000.0 ).utc, seed ]
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
data/lib/ulid/version.rb
ADDED
data/logo.png
ADDED
Binary file
|
data/ulid-ruby.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ulid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ulid-ruby"
|
8
|
+
spec.version = ULID::VERSION
|
9
|
+
spec.authors = ["adam bachman"]
|
10
|
+
spec.email = ["adam.bachman@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ruby library providing support for universally unique lexicographically sortable identifiers}
|
13
|
+
spec.description = %q{todo: write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/abachman/ulid-ruby"
|
15
|
+
spec.license = "mit"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| file.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "yard", "~> 0.9"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ulid-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- adam bachman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
description: 'todo: write a longer description or delete this line.'
|
70
|
+
email:
|
71
|
+
- adam.bachman@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-version"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/ulid.rb
|
88
|
+
- lib/ulid/compare.rb
|
89
|
+
- lib/ulid/constants.rb
|
90
|
+
- lib/ulid/generate.rb
|
91
|
+
- lib/ulid/identifier.rb
|
92
|
+
- lib/ulid/parse.rb
|
93
|
+
- lib/ulid/version.rb
|
94
|
+
- logo.png
|
95
|
+
- ulid-ruby.gemspec
|
96
|
+
homepage: https://github.com/abachman/ulid-ruby
|
97
|
+
licenses:
|
98
|
+
- mit
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.6.8
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: ruby library providing support for universally unique lexicographically sortable
|
120
|
+
identifiers
|
121
|
+
test_files: []
|