snowflake-id 0.0.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.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/lib/snowflake-id.rb +66 -0
- data/lib/snowflake-id/version.rb +3 -0
- data/snowflake-id.gemspec +24 -0
- data/spec/snowflake-id_spec.rb +40 -0
- data/spec/spec_helper.rb +2 -0
- metadata +114 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
snowflake-id
|
2
|
+
============
|
3
|
+
|
4
|
+
Twitter's Snowflake ID to Time conversion module.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'snowflake-id'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install snowflake-id
|
20
|
+
|
21
|
+
Usage
|
22
|
+
-----
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'snowflake-id'
|
26
|
+
|
27
|
+
# snowflake to Time object
|
28
|
+
puts SnowflakeId.new(321239371296694272).to_time # => 'Mon Apr 08 12:33:29.113 +0000 2013'
|
29
|
+
|
30
|
+
# Time object to snowflake
|
31
|
+
require 'time'
|
32
|
+
puts Time.parse('Mon Apr 08 12:33:29.113 +0000 2013').to_snowflake # => 321239370822582272
|
33
|
+
```
|
34
|
+
|
35
|
+
Contributing
|
36
|
+
------------
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/snowflake-id.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'snowflake-id/version'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Provides Twitter's snowflake-encoded status ID to Time conversion
|
5
|
+
#
|
6
|
+
class SnowflakeId
|
7
|
+
SNOWFLAKE_EPOCH_MILLIS = 1288834974657
|
8
|
+
|
9
|
+
#
|
10
|
+
# Generates Snowflake object from Twitter's status ID.
|
11
|
+
#
|
12
|
+
def initialize(id)
|
13
|
+
@id = id.to_i
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# Returns snowflake-encoded ID.
|
18
|
+
#
|
19
|
+
def id
|
20
|
+
return @id
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Returns string representation of ID
|
25
|
+
#
|
26
|
+
def to_s
|
27
|
+
return @id.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Returns Time object which is included in snowflake-encoded ID.
|
32
|
+
#
|
33
|
+
def to_time
|
34
|
+
t = ((@id >> 22) + SNOWFLAKE_EPOCH_MILLIS) / 1000.0
|
35
|
+
return Time.at(t)
|
36
|
+
end
|
37
|
+
|
38
|
+
def ==(other)
|
39
|
+
return @id == other.id
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Converts Time object into Snowflake object.
|
44
|
+
#
|
45
|
+
def self.to_snowflake(time)
|
46
|
+
millis = (time.to_f * 1000).to_i
|
47
|
+
|
48
|
+
if millis < SNOWFLAKE_EPOCH_MILLIS
|
49
|
+
raise "#{time} is before Snowflake epoch"
|
50
|
+
end
|
51
|
+
|
52
|
+
return SnowflakeId.new((millis - SNOWFLAKE_EPOCH_MILLIS) << 22)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# Extends Time class.
|
58
|
+
#
|
59
|
+
class Time
|
60
|
+
#
|
61
|
+
# Converts Time object into Snowflake object.
|
62
|
+
#
|
63
|
+
def to_snowflake
|
64
|
+
return SnowflakeId.to_snowflake(self)
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'snowflake-id/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "snowflake-id"
|
8
|
+
spec.version = SnowflakeId::VERSION
|
9
|
+
spec.authors = ["KOMIYA Atsushi"]
|
10
|
+
spec.email = ["komiya.atsushi@gmail.com"]
|
11
|
+
spec.description = %q{Twitter's Snowflake ID to Time conversion module}
|
12
|
+
spec.summary = %q{Twitter's Snowflake ID to Time conversion module}
|
13
|
+
spec.homepage = "https://github.com/komiya-atsushi/snowflake-id"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
describe SnowflakeId do
|
5
|
+
it 'should have a version number' do
|
6
|
+
SnowflakeId::VERSION.should_not be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'of 321239371296694272' do
|
10
|
+
before { @snowflake = SnowflakeId.new(321239371296694272) }
|
11
|
+
|
12
|
+
describe '#to_s' do
|
13
|
+
subject { @snowflake.to_s }
|
14
|
+
it { should eq '321239371296694272' }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#id' do
|
18
|
+
subject { @snowflake.id }
|
19
|
+
it { should eq 321239371296694272 }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#to_time' do
|
23
|
+
subject { @snowflake.to_time }
|
24
|
+
it { should be_within(0.0001).of(Time.parse('Mon Apr 08 12:33:29.113 +0000 2013')) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Time do
|
30
|
+
TIME = "2013-04-08 21:33:29 +0900"
|
31
|
+
|
32
|
+
context "of #{TIME}" do
|
33
|
+
before { @time = Time.parse(TIME) }
|
34
|
+
|
35
|
+
describe '#to_snowflake' do
|
36
|
+
subject { @time.to_snowflake }
|
37
|
+
it { should eq SnowflakeId.new((1365424409000 - 1288834974657) << 22) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snowflake-id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- KOMIYA Atsushi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Twitter's Snowflake ID to Time conversion module
|
63
|
+
email:
|
64
|
+
- komiya.atsushi@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/snowflake-id.rb
|
75
|
+
- lib/snowflake-id/version.rb
|
76
|
+
- snowflake-id.gemspec
|
77
|
+
- spec/snowflake-id_spec.rb
|
78
|
+
- spec/snowflake-id_spec.rb~
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: https://github.com/komiya-atsushi/snowflake-id
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: 4520183643517811643
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
hash: 4520183643517811643
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.8.25
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Twitter's Snowflake ID to Time conversion module
|
111
|
+
test_files:
|
112
|
+
- spec/snowflake-id_spec.rb
|
113
|
+
- spec/snowflake-id_spec.rb~
|
114
|
+
- spec/spec_helper.rb
|