Class: Snowflake

Inherits:
Object
  • Object
show all
Defined in:
lib/disrb.rb

Overview

Contains functions related to Discord snowflakes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snowflake) ⇒ Snowflake

Creates a new Snowflake instance.

Parameters:

  • snowflake (Integer)

    The snowflake to be used.



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')
  @discord_epoch_timestamp = snowflake[0..41]
  @internal_worker_id = snowflake[42..46]
  @internal_process_id = snowflake[47..51]
  @gen_id_on_process = snowflake[52..64]
  @unix_timestamp = snowflake[0..41].to_i(2) + 1_420_070_400_000
  @timestamp = Time.at((snowflake[0..41].to_i(2) + 1_420_070_400_000) / 1000).utc
end

Instance Attribute Details

#discord_epoch_timestampString

Returns binary representation of the discord epoch timestamp (milliseconds since the first second of 2015).

Returns:

  • (String)

    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_processString

Returns Nº of the ID generated on the process. This is incremented every time a new snowflake is generated on the same process.

Returns:

  • (String)

    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_idString

Returns Internal process ID.

Returns:

  • (String)

    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_idString

Returns Internal worker ID.

Returns:

  • (String)

    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)

#snowflakeString

Returns 64-bit binary representation of the snowflake as a string.

Returns:

  • (String)

    64-bit binary representation of the snowflake as a string



32
33
34
# File 'lib/disrb.rb', line 32

def snowflake
  @snowflake
end

#timestampTime

Returns Timestamp of the snowflake in UTC as a Time object.

Returns:

  • (Time)

    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_timestampInteger

Returns Unix timestamp of the snowflake in milliseconds.

Returns:

  • (Integer)

    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)