Class: Snowflake
- Inherits:
-
Object
- Object
- Snowflake
- Defined in:
- lib/disrb.rb
Overview
Contains functions related to Discord snowflakes.
Instance Attribute Summary collapse
-
#discord_epoch_timestamp ⇒ String
Binary representation of the discord epoch timestamp (milliseconds since the first second of 2015).
-
#gen_id_on_process ⇒ String
Nº of the ID generated on the process.
-
#internal_process_id ⇒ String
Internal process ID.
-
#internal_worker_id ⇒ String
Internal worker ID.
-
#snowflake ⇒ String
64-bit binary representation of the snowflake as a string.
-
#timestamp ⇒ Time
Timestamp of the snowflake in UTC as a Time object.
-
#unix_timestamp ⇒ Integer
Unix timestamp of the snowflake in milliseconds.
Instance Method Summary collapse
-
#initialize(snowflake) ⇒ Snowflake
constructor
Creates a new Snowflake instance.
Constructor Details
#initialize(snowflake) ⇒ Snowflake
Creates a new Snowflake instance.
38 39 40 41 42 43 44 45 46 |
# File 'lib/disrb.rb', line 38 def initialize(snowflake) @snowflake = snowflake.to_s(2).rjust(64, '0') = snowflake[0..41] @internal_worker_id = snowflake[42..46] @internal_process_id = snowflake[47..51] @gen_id_on_process = snowflake[52..64] = snowflake[0..41].to_i(2) + 1_420_070_400_000 = Time.at((snowflake[0..41].to_i(2) + 1_420_070_400_000) / 1000).utc end |
Instance Attribute Details
#discord_epoch_timestamp ⇒ String
Returns binary representation of the discord epoch timestamp (milliseconds since the first second of 2015).
32 33 |
# File 'lib/disrb.rb', line 32 attr_accessor(:snowflake, :discord_epoch_timestamp, :internal_worker_id, :internal_process_id, :gen_id_on_process, :unix_timestamp, :timestamp) |
#gen_id_on_process ⇒ String
Returns Nº of the ID generated on the process. This is incremented every time a new snowflake is generated on the same process.
32 33 |
# File 'lib/disrb.rb', line 32 attr_accessor(:snowflake, :discord_epoch_timestamp, :internal_worker_id, :internal_process_id, :gen_id_on_process, :unix_timestamp, :timestamp) |
#internal_process_id ⇒ String
Returns Internal process ID.
32 33 |
# File 'lib/disrb.rb', line 32 attr_accessor(:snowflake, :discord_epoch_timestamp, :internal_worker_id, :internal_process_id, :gen_id_on_process, :unix_timestamp, :timestamp) |
#internal_worker_id ⇒ String
Returns Internal worker ID.
32 33 |
# File 'lib/disrb.rb', line 32 attr_accessor(:snowflake, :discord_epoch_timestamp, :internal_worker_id, :internal_process_id, :gen_id_on_process, :unix_timestamp, :timestamp) |
#snowflake ⇒ String
Returns 64-bit binary representation of the snowflake as a string.
32 33 34 |
# File 'lib/disrb.rb', line 32 def snowflake @snowflake end |
#timestamp ⇒ Time
Returns Timestamp of the snowflake in UTC as a Time object.
32 33 |
# File 'lib/disrb.rb', line 32 attr_accessor(:snowflake, :discord_epoch_timestamp, :internal_worker_id, :internal_process_id, :gen_id_on_process, :unix_timestamp, :timestamp) |
#unix_timestamp ⇒ Integer
Returns Unix timestamp of the snowflake in milliseconds.
32 33 |
# File 'lib/disrb.rb', line 32 attr_accessor(:snowflake, :discord_epoch_timestamp, :internal_worker_id, :internal_process_id, :gen_id_on_process, :unix_timestamp, :timestamp) |