Class: Ap4r::Dispatchers::Base

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

Overview

A base class for dispathcer classes associated with each dispatch_mode. Responsibilities of subclasses are to implement following methods, only invoke is mandatory and others are optional (no operations by default).

  • modify_message to preprocess a message, e.g. rewirte URL or discard message.

  • invoke to execute main logic, e.g. HTTP POST call. mandatory

  • validate_response to judge whether invoke finished successfully.

  • response to return the result of invoke process.

Direct Known Subclasses

Druby, Http, SOAP, XmlRpc

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, conf) ⇒ Base

Takes

  • message: from a queue

  • conf: configuration from dispatchers section

– TODO: Subclass should have conf instead of instance? 2007/06/06 by shino



157
158
159
160
# File 'lib/ap4r/dispatcher.rb', line 157

def initialize(message, conf)
  @message = message
  @conf = conf
end

Class Method Details

.dispatch_mode(mode_symbol) ⇒ Object

Difine a constant DISPATCH_MODE to value ‘mode_symbol’ and add self to a Converters list.



147
148
149
150
# File 'lib/ap4r/dispatcher.rb', line 147

def self.dispatch_mode(mode_symbol)
  self.const_set(:DISPATCH_MODE, mode_symbol)
  ::Ap4r::Dispatchers.register_dispatcher_class(mode_symbol, self)
end

Instance Method Details

#callObject

Entry facade for each message processing. Modifies, invokes (maybe remote), validates, and responds.



164
165
166
167
168
169
170
171
# File 'lib/ap4r/dispatcher.rb', line 164

def call
  # TODO: rename to more appropriate one 2007/05/10 by shino
  self.modify_message
  logger.debug{"Ap4r::Dispatcher after modification\n#{@message.to_yaml}"}
  self.invoke
  self.validate_response
  self.response
end

#invokeObject

Main logic of message processing. Maybe calls remote, e.g. HTTP request.



183
184
185
186
# File 'lib/ap4r/dispatcher.rb', line 183

def invoke
  # TODO: rename to more appropriate one 2007/05/10 by shino
  raise 'must be implemented in subclasses'
end

#modify_messageObject

Modifies message. Now only URL modification is implemented.



175
176
177
178
179
# File 'lib/ap4r/dispatcher.rb', line 175

def modify_message
  modification_rules = @conf["modify_rules"]
  return unless modification_rules
  modify_url(modification_rules)
end

#responseObject

Returns response. The return value is also the return value of call method. By default impl, the instance variable @response is used.



195
196
197
# File 'lib/ap4r/dispatcher.rb', line 195

def response
  @response
end

#validate_responseObject



188
189
190
# File 'lib/ap4r/dispatcher.rb', line 188

def validate_response
  # nop
end