Module: Bidi2pdf::Bidi::Commands::Base

Overview

Base module for defining WebSocket commands in the Bidi2pdf library. This module provides common functionality for creating, comparing, and inspecting WebSocket command payloads.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

Compares the current command with another command for equality.

rubocop: disable Metrics/AbcSize

Parameters:

  • other (Object)

    The other command to compare.

Returns:

  • (Boolean)

    True if the commands are equal, false otherwise.



38
39
40
41
42
43
44
45
46
# File 'lib/bidi2pdf/bidi/commands/base.rb', line 38

def ==(other)
  return false unless other.respond_to?(:method_name) && other.respond_to?(:params)

  return false unless method_name == other.method_name

  return false unless params.keys.sort == other.params.keys.sort

  params.all? { |key, value| other.params.key?(key) && value == other.params[key] }
end

#as_payload(id) ⇒ Hash

Constructs the payload for the WebSocket command.

Parameters:

  • id (Integer)

    The unique identifier for the command.

Returns:

  • (Hash)

    The payload containing the command ID, method name, and parameters.



25
26
27
28
29
30
31
# File 'lib/bidi2pdf/bidi/commands/base.rb', line 25

def as_payload(id)
  {
    id: id,
    method: method_name,
    params: params
  }
end

#eql?(other) ⇒ Boolean

Checks if the current command is hash-equal to another command.

Parameters:

  • other (Object)

    The other command to compare.

Returns:

  • (Boolean)

    True if the commands are hash-equal, false otherwise.



54
55
56
57
58
# File 'lib/bidi2pdf/bidi/commands/base.rb', line 54

def eql?(other)
  return false unless other.is_a?(Bidi2pdf::Bidi::Commands::Base)

  self == other
end

#hashInteger

Computes the hash value for the command.

Returns:

  • (Integer)

    The hash value based on the method name and parameters.



63
64
65
# File 'lib/bidi2pdf/bidi/commands/base.rb', line 63

def hash
  [method_name, params].hash
end

#inspectString

Returns a string representation of the command, with sensitive fields redacted.

Returns:

  • (String)

    The string representation of the command.



70
71
72
73
74
# File 'lib/bidi2pdf/bidi/commands/base.rb', line 70

def inspect
  attributes = redact_sensitive_fields({ method_name: method_name, params: params })

  "#<#{self.class}:#{object_id} #{attributes}>"
end

#method_nameObject

Abstract method that must be implemented in subclasses to define the WebSocket command method name.

Raises:

  • (NotImplementedError)

    If the method is not implemented in a subclass.



14
# File 'lib/bidi2pdf/bidi/commands/base.rb', line 14

def method_name = raise(NotImplementedError, "method_name must be implemented in subclass")

#paramsHash

Returns the parameters for the WebSocket command.

Returns:

  • (Hash)

    The parameters for the command. Defaults to an empty hash.



19
# File 'lib/bidi2pdf/bidi/commands/base.rb', line 19

def params = {}