snowflakey 0.1.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/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/exe/snowflakey +16 -0
- data/lib/snowflakey/snowflake.rb +27 -0
- data/lib/snowflakey/version.rb +3 -0
- data/lib/snowflakey.rb +25 -0
- data/snowflakey.gemspec +28 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f58d10fa9eeef4755b60ef18deff6a4d58850206
|
4
|
+
data.tar.gz: 43db5c3e1027b07cd459663a074f93c063c85102
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4636f31b093cf76e08e0783a272a0171199a342a63d19a6f2875e30e4fe300ae7fc540fb0e8bb9ce366db0218f99a53e0d317aae0bc9616da62ee961e0f0ac6a
|
7
|
+
data.tar.gz: cb1ce094d7f206450d97a39d6e5635d0ef3a23f69be3b5f34a5c6f670fb07f5000518e7e7037e1588738948f4bf3887fe605504aa0a20af3282008f81d1ba5de
|
data/.gitignore
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 Robin Clart
|
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,71 @@
|
|
1
|
+
# Snowflakey
|
2
|
+
|
3
|
+
Unique ID generator.
|
4
|
+
|
5
|
+
## Generation
|
6
|
+
|
7
|
+
You can generate a simple snowflake by calling `#generate` without
|
8
|
+
any arguments.
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
Snowflakey.generate # => "567wz82coauesrlb522"
|
12
|
+
```
|
13
|
+
|
14
|
+
You can also pass a prefix that will be prepended to the snowflake.
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
Snowflakey.generate("snow") # => "snow_567wz9ox8b8p58tngzu"
|
18
|
+
```
|
19
|
+
|
20
|
+
Finally it also works with multiple prefixes.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
Snowflakey.generate(["snow", "flake"]) # => "snow_flake_567wz6ecywb6d6ruor9"
|
24
|
+
```
|
25
|
+
|
26
|
+
You can also change the size of the snowflake.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
Snowflakey.generate("snow", size: 64) # => "snow_2mdov6imct3o4"
|
30
|
+
```
|
31
|
+
|
32
|
+
You can also use another base.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
Snowflakey.generate("snow", base: 16) # => "snow_ac6591aa22063660af0e05d4"
|
36
|
+
```
|
37
|
+
|
38
|
+
## Verification
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
snowflake = Snowflakey.verify("snow_567z7pfvdq47fswkt52")
|
42
|
+
```
|
43
|
+
|
44
|
+
This will return the snowflake as if it had been created with the low level API.
|
45
|
+
You can then fetch the size, the time, the ID, the base, etc.
|
46
|
+
|
47
|
+
Note that if the snowflake was created with another base than 36 and with another size than 96 you will have to declare those when calling `#verify`.
|
48
|
+
|
49
|
+
## Manual Initialization
|
50
|
+
|
51
|
+
If you need you can also initialize a snowflake without using the `#generate` method. This gives you more control over every parameters.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
prefix = "snow"
|
55
|
+
size = 96
|
56
|
+
time = Time.parse("2016-12-04T22:22:22Z").utc
|
57
|
+
id = 3104654282887302
|
58
|
+
base = 36
|
59
|
+
|
60
|
+
snowflake = Snowflakey.new(prefix, size, time, id, base)
|
61
|
+
```
|
62
|
+
|
63
|
+
You can then call `#to_s` to get the snowflake as a string.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
snowflake.to_s # => "snow_567z7pfvdq47fswkt52"
|
67
|
+
```
|
68
|
+
|
69
|
+
## Important Remark
|
70
|
+
|
71
|
+
> You are not special. You're not a beautiful and unique snowflake. You're the same decaying organic matter as everything else. We're all part of the same compost heap. We're all singing, all dancing crap of the world.
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/exe/snowflakey
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "snowflakey"
|
4
|
+
|
5
|
+
prefix = ARGV[0].to_s.gsub(/[[:space:]]/, "")
|
6
|
+
size = ARGV[1].to_i
|
7
|
+
|
8
|
+
if prefix == ""
|
9
|
+
prefix = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
if size.zero?
|
13
|
+
size = 96
|
14
|
+
end
|
15
|
+
|
16
|
+
puts Snowflakey.generate(prefix, size: size)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Snowflakey
|
2
|
+
class Snowflake
|
3
|
+
BASE = 36
|
4
|
+
|
5
|
+
def initialize(prefix, size, time, id, base)
|
6
|
+
@prefix = prefix
|
7
|
+
@size = size
|
8
|
+
@time = time
|
9
|
+
@id = id
|
10
|
+
@base = base.to_i
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_reader :prefix, :size, :time, :id, :base
|
14
|
+
def to_s
|
15
|
+
t = (@time.to_f * 1e3).round
|
16
|
+
id = t << (@size - 41)
|
17
|
+
id = id | @id % (2 ** (@size - 41))
|
18
|
+
id = Baseconv.convert(id, from_base: 10, to_base: @base)
|
19
|
+
|
20
|
+
[*@prefix, id].compact.join("_")
|
21
|
+
end
|
22
|
+
|
23
|
+
def inspect
|
24
|
+
to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/snowflakey.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "time"
|
2
|
+
require "securerandom"
|
3
|
+
require "baseconv"
|
4
|
+
require "snowflakey/snowflake"
|
5
|
+
require "snowflakey/version"
|
6
|
+
|
7
|
+
module Snowflakey
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def generate(prefix = nil, size: 96, time: Time.now, base: Snowflake::BASE)
|
11
|
+
r = (SecureRandom.random_number * 1e16).round
|
12
|
+
|
13
|
+
Snowflake.new(prefix, size, time.utc, r, base).to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def verify(snowflake, size: 96, base: Snowflake::BASE)
|
17
|
+
id, prefix = snowflake.reverse.split("_", 2).map { |s| s.reverse }
|
18
|
+
ms = id.to_i(base) >> (size - 41)
|
19
|
+
time = Time.at((ms / 1e3)).utc
|
20
|
+
id = Baseconv.convert(id, from_base: base.to_i, to_base: 10)
|
21
|
+
id = id.to_i % (2 ** (size - 41))
|
22
|
+
|
23
|
+
Snowflake.new(prefix, size, time, id, base)
|
24
|
+
end
|
25
|
+
end
|
data/snowflakey.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 'snowflakey/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "snowflakey"
|
8
|
+
spec.version = Snowflakey::VERSION
|
9
|
+
spec.authors = ["Robin Clart"]
|
10
|
+
spec.email = ["robin@omise.co"]
|
11
|
+
|
12
|
+
spec.summary = %q{Unique ID Generator}
|
13
|
+
spec.homepage = "https://github.com/robinclart/snowflakey"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "baseconv", "~> 0.1"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snowflakey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robin Clart
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: baseconv
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
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.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
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: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- robin@omise.co
|
72
|
+
executables:
|
73
|
+
- snowflakey
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- exe/snowflakey
|
86
|
+
- lib/snowflakey.rb
|
87
|
+
- lib/snowflakey/snowflake.rb
|
88
|
+
- lib/snowflakey/version.rb
|
89
|
+
- snowflakey.gemspec
|
90
|
+
homepage: https://github.com/robinclart/snowflakey
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.5.1
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Unique ID Generator
|
114
|
+
test_files: []
|