Class: Whatsapp::MessageDispatcher Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Dispatches incoming messages to the configured MessageHandler.

Resolves the handler from Configuration#message_handler (Class or String), instantiates it, and calls +#call+ with the message and session name. Errors are caught and logged, never propagated.

Instance Method Summary collapse

Constructor Details

#initialize(session_name) ⇒ MessageDispatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MessageDispatcher.

Parameters:

  • session_name (String)

    name of the session receiving messages



11
12
13
# File 'lib/whatsapp/message_dispatcher.rb', line 11

def initialize(session_name)
  @session_name = session_name
end

Instance Method Details

#dispatch(message) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Dispatch a message to the configured handler.

Parameters:

  • message (Message)

    the incoming message



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/whatsapp/message_dispatcher.rb', line 19

def dispatch(message)
  handler = resolve_handler
  return unless handler

  handler.new.call(message, session: @session_name)
rescue => e
  Whatsapp.logger.error "MessageHandler error",
    handler: handler&.name || handler.to_s,
    session: @session_name,
    error: e.message
end