Class: SnowflakeId
- Inherits:
-
Object
- Object
- SnowflakeId
- Defined in:
- lib/snowflake-id.rb,
lib/snowflake-id/version.rb
Overview
Provides Twitter's snowflake-encoded status ID to Time conversion
Constant Summary collapse
- SNOWFLAKE_EPOCH_MILLIS =
1288834974657- VERSION =
"0.0.1"
Class Method Summary collapse
-
.to_snowflake(time) ⇒ Object
Converts Time object into Snowflake object.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#id ⇒ Object
Returns snowflake-encoded ID.
-
#initialize(id) ⇒ SnowflakeId
constructor
Generates Snowflake object from Twitter's status ID.
-
#to_s ⇒ Object
Returns string representation of ID.
-
#to_time ⇒ Object
Returns Time object which is included in snowflake-encoded ID.
Constructor Details
#initialize(id) ⇒ SnowflakeId
Generates Snowflake object from Twitter's status ID.
12 13 14 |
# File 'lib/snowflake-id.rb', line 12 def initialize(id) @id = id.to_i end |
Class Method Details
.to_snowflake(time) ⇒ Object
Converts Time object into Snowflake object.
45 46 47 48 49 50 51 52 53 |
# File 'lib/snowflake-id.rb', line 45 def self.to_snowflake(time) millis = (time.to_f * 1000).to_i if millis < SNOWFLAKE_EPOCH_MILLIS raise "#{time} is before Snowflake epoch" end return SnowflakeId.new((millis - SNOWFLAKE_EPOCH_MILLIS) << 22) end |
Instance Method Details
#==(other) ⇒ Object
38 39 40 |
# File 'lib/snowflake-id.rb', line 38 def ==(other) return @id == other.id end |
#id ⇒ Object
Returns snowflake-encoded ID.
19 20 21 |
# File 'lib/snowflake-id.rb', line 19 def id return @id end |
#to_s ⇒ Object
Returns string representation of ID
26 27 28 |
# File 'lib/snowflake-id.rb', line 26 def to_s return @id.to_s end |
#to_time ⇒ Object
Returns Time object which is included in snowflake-encoded ID.
33 34 35 36 |
# File 'lib/snowflake-id.rb', line 33 def to_time t = ((@id >> 22) + SNOWFLAKE_EPOCH_MILLIS) / 1000.0 return Time.at(t) end |