Class: Rapid::Baps::Responses::Responder

Inherits:
Services::HandlerSet show all
Extended by:
Forwardable
Defined in:
lib/ury_rapid/baps/responses/responder.rb

Overview

An object that responds to BAPS server responses by updating the model

The responder subscribes to a response parser's output channel, and responds to incoming responses from that channel by firing methods that update the model to reflect the exposed state of the BAPS server.

The BAPS responder also has the ability to respond directly to the BAPS server by sending requests to the outgoing requests queue. This is used, for example, to complete the BAPS login procedure.

Constant Summary collapse

HANDLER_MODULE =
Responses::Handlers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, requester) ⇒ Responder

Initialises the responder

Examples:

Initialise a responder

responder = Responder.new(model, queue)

Parameters:

  • model (Rapid::Model::ServiceView)

    The model view on this Responder will operate.

  • requester (Requester)

    The Requester via which this Responder can send BAPS login requests.


52
53
54
55
56
57
58
59
# File 'lib/ury_rapid/baps/responses/responder.rb', line 52

def initialize(model, requester)
  @model = model
  @requester = requester

  log(:info, 'Initialising BAPS responder.')

  super()
end

Instance Attribute Details

#modelRapid::Model::ServiceView (readonly)

Retrieves the model on which this Responder will operate

Examples:

Gets the model.

responder = Responder.new(model, queue)
model = responder.model

Returns:

  • (Rapid::Model::ServiceView)

    The model view on which this Responder operates.


39
40
41
# File 'lib/ury_rapid/baps/responses/responder.rb', line 39

def model
  @model
end

Instance Method Details

#handle_response(response) ⇒ void

This method returns an undefined value.

Handles a response

If the response has no handler, the null handler #unhandled is used.

Examples:

Get the handler for a specific BAPS code

responder = Responder.new(model, queue)
responder.handle_response(a_response)

Parameters:

  • response (Object)

    A BAPS response to handle.


91
92
93
# File 'lib/ury_rapid/baps/responses/responder.rb', line 91

def handle_response(response)
  handler_for_code(response.code).call(response)
end

#handler_for_code(code) ⇒ Proc

Finds the handler for a specific BAPS response code

If there is no handler for the given code, #unhandled is returned.

Examples:

Get the handler for a specific BAPS code

responder = Responder.new(model, queue)
responder.handler_for_code(0x100)

Parameters:

  • code (Code)

    A BAPS response code.

Returns:

  • (Proc)

    A handler function that may be called with the response to handle.


109
110
111
# File 'lib/ury_rapid/baps/responses/responder.rb', line 109

def handler_for_code(code)
  @handlers.fetch(code, method(:unhandled))
end

#register(channel) ⇒ void

This method returns an undefined value.

Registers the responder's callbacks with a incoming responses channel

Examples:

Register an EventMachine channel

responder = Responder.new(model, queue)
channel = EventMachine::Channel.new
# Attach channel to rest of BAPS here
responder.register(channel)

Parameters:

  • channel (Channel)

    The source channel for responses coming from BAPS's chat system.


74
75
76
# File 'lib/ury_rapid/baps/responses/responder.rb', line 74

def register(channel)
  channel.subscribe(&method(:handle_response))
end

#unhandled(response) ⇒ void

This method returns an undefined value.

Mock handler for when a BAPS code has no registered handler

This handler logs a warning, and does nothing else.

Examples:

'Handle' an unhandled response

responder = Responder.new(model, queue)
responder.unhandled(a_response)

Parameters:

  • response (Object)

    A response to 'handle'.


126
127
128
129
130
131
132
133
134
# File 'lib/ury_rapid/baps/responses/responder.rb', line 126

def unhandled(response)
  message = "Unhandled response: #{response.name}"
  if response.code.is_a?(Numeric)
    hexcode = response.code.to_s(16)
    message << " (0x#{hexcode})"
  end

  log(:warn, message)
end