Class: DiscordRDA::Snowflake
- Inherits:
-
Object
- Object
- DiscordRDA::Snowflake
- Includes:
- Comparable
- Defined in:
- lib/discord_rda/core/snowflake.rb
Overview
Discord Snowflake ID value object. Provides extraction of timestamp, worker ID, process ID, and increment.
Discord snowflakes are 64-bit integers with the following structure:
-
41 bits: timestamp (milliseconds since Discord epoch)
-
5 bits: worker ID
-
5 bits: process ID
-
12 bits: increment
Constant Summary collapse
- DISCORD_EPOCH =
Discord epoch (January 1, 2015)
1_420_070_400_000- WORKER_ID_BITS =
Bit masks for snowflake components
0x3E0000- PROCESS_ID_BITS =
0x1F000- INCREMENT_BITS =
0xFFF
Instance Attribute Summary collapse
-
#value ⇒ Integer
readonly
The raw snowflake value.
Class Method Summary collapse
-
.generate(time = Time.now.utc) ⇒ Snowflake
Generate a new snowflake (for testing only - Discord generates real snowflakes).
-
.parse(value) ⇒ Snowflake
Parse a snowflake from string or integer.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare snowflakes by timestamp.
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality with another snowflake or value.
-
#hash ⇒ Integer
Get the hash code.
-
#increment ⇒ Integer
Get the increment from the snowflake.
-
#initialize(value) ⇒ Snowflake
constructor
Create a new snowflake.
-
#inspect ⇒ String
Inspect the snowflake.
-
#process_id ⇒ Integer
Get the process ID from the snowflake.
-
#timestamp ⇒ Time
(also: #time)
Get the timestamp from the snowflake.
-
#to_i ⇒ Integer
Convert to integer.
-
#to_s ⇒ String
Convert to string.
-
#worker_id ⇒ Integer
Get the worker ID from the snowflake.
Constructor Details
#initialize(value) ⇒ Snowflake
Create a new snowflake
50 51 52 53 |
# File 'lib/discord_rda/core/snowflake.rb', line 50 def initialize(value) @value = value.to_i freeze end |
Instance Attribute Details
#value ⇒ Integer (readonly)
Returns The raw snowflake value.
28 29 30 |
# File 'lib/discord_rda/core/snowflake.rb', line 28 def value @value end |
Class Method Details
.generate(time = Time.now.utc) ⇒ Snowflake
Generate a new snowflake (for testing only - Discord generates real snowflakes)
34 35 36 37 38 |
# File 'lib/discord_rda/core/snowflake.rb', line 34 def generate(time = Time.now.utc) = ((time.to_f * 1000).to_i - DISCORD_EPOCH) << 22 increment = rand(0..INCREMENT_BITS) new( | increment) end |
.parse(value) ⇒ Snowflake
Parse a snowflake from string or integer
43 44 45 |
# File 'lib/discord_rda/core/snowflake.rb', line 43 def parse(value) new(value) end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare snowflakes by timestamp
83 84 85 |
# File 'lib/discord_rda/core/snowflake.rb', line 83 def <=>(other) <=> other. end |
#==(other) ⇒ Boolean Also known as: eql?
Check equality with another snowflake or value
92 93 94 |
# File 'lib/discord_rda/core/snowflake.rb', line 92 def ==(other) other.is_a?(Snowflake) && @value == other.value end |
#hash ⇒ Integer
Get the hash code
99 100 101 |
# File 'lib/discord_rda/core/snowflake.rb', line 99 def hash @value.hash end |
#increment ⇒ Integer
Get the increment from the snowflake
76 77 78 |
# File 'lib/discord_rda/core/snowflake.rb', line 76 def increment @value & INCREMENT_BITS end |
#inspect ⇒ String
Inspect the snowflake
117 118 119 |
# File 'lib/discord_rda/core/snowflake.rb', line 117 def inspect "#<Snowflake value=#{@value} time=#{.iso8601}>" end |
#process_id ⇒ Integer
Get the process ID from the snowflake
70 71 72 |
# File 'lib/discord_rda/core/snowflake.rb', line 70 def process_id (@value & PROCESS_ID_BITS) >> 12 end |
#timestamp ⇒ Time Also known as: time
Get the timestamp from the snowflake
57 58 59 |
# File 'lib/discord_rda/core/snowflake.rb', line 57 def Time.at(((@value >> 22) + DISCORD_EPOCH) / 1000.0).utc end |
#to_i ⇒ Integer
Convert to integer
105 106 107 |
# File 'lib/discord_rda/core/snowflake.rb', line 105 def to_i @value end |
#to_s ⇒ String
Convert to string
111 112 113 |
# File 'lib/discord_rda/core/snowflake.rb', line 111 def to_s @value.to_s end |
#worker_id ⇒ Integer
Get the worker ID from the snowflake
64 65 66 |
# File 'lib/discord_rda/core/snowflake.rb', line 64 def worker_id (@value & WORKER_ID_BITS) >> 17 end |