Class: Ap4r::Dispatchers::Http

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

Overview

Dispatches via a raw HTTP protocol. Current implementation uses only a POST method, irrespective of options[:target_method].

Determination of “success” is two fold:

  • status code should be exactly 200, other codes (including 201-2xx) are treated as error, and

  • body should include a string “true”

Instance Method Summary collapse

Methods inherited from Base

#call, dispatch_mode, #initialize, #modify_message, #response

Constructor Details

This class inherits a constructor from Ap4r::Dispatchers::Base

Instance Method Details

#invokeObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/ap4r/dispatcher.rb', line 228

def invoke
  # TODO: should be added some request headers 2006/10/12 shino
  #       e.g. X-Ap4r-Version, Accept(need it?)
  # TODO: Now supports POST only, 2006/10/12 shino
  @response = nil
  uri = URI.parse(@message[:target_url])
  headers = make_header

  Net::HTTP.start(uri.host, uri.port) do |http|
    # TODO: global configuration over dispatchers for each protocol should be considered, 2008/02/06 by kiwamu
    # TODO: http open timeout should be considered, 2008/02/06 by kiwamu
    if @conf['http'] && @conf['http']['timeout']
      http.read_timeout = @conf['http']['timeout']
      logger.info "set HTTP read timeout to #{http.read_timeout}s"
    end
    @response, = http.post(uri.path, @message.object, headers)
  end
end

#make_headerObject



247
248
249
250
251
252
253
254
255
# File 'lib/ap4r/dispatcher.rb', line 247

def make_header
  headers = { }
  @message.headers.map do |k,v|
    s = StringScanner.new(k.to_s)
    s.scan(/\Ahttp_header_/)
    headers[s.post_match] = v if s.post_match
  end
  headers
end

#validate_responseObject



257
258
259
260
261
# File 'lib/ap4r/dispatcher.rb', line 257

def validate_response
  logger.debug{"response status [#{@response.code} #{@response.message}]"}
  validate_response_status(Net::HTTPOK)
  validate_response_body(/true/)
end

#validate_response_body(pattern) ⇒ Object

Checks whether the response body includes pattern. pattern should be a regular expression.



279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ap4r/dispatcher.rb', line 279

def validate_response_body(pattern)
  unless @response.body =~ pattern
    error_message = "HTTP Response FAILURE, status" +
      " [#{@response.code} #{@response.message}], body [#{@response.body}]"
    #TODO: Refactor error logging, 2006/10/13 shino
    logger.error(error_message)
    logger.info{@response.to_yaml}
    #TODO: must create AP4R specific Exception class, 2006/10/12 shino
    raise StandardError.new(error_message)
  end
end

#validate_response_status(status_kind) ⇒ Object

Checks whether the response status is a kind of status_kind. status_kind should be one of Net::HTTPRespose‘s subclasses.



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ap4r/dispatcher.rb', line 265

def validate_response_status(status_kind)
  #TODO: make the difinition of success variable, 2006/10/13 shino
  unless @response.kind_of?(status_kind)
    error_message = "HTTP Response FAILURE, " +
      "status [#{@response.code} #{@response.message}]"
    logger.error(error_message)
    logger.info{@response.to_yaml}
    #TODO: must create AP4R specific Exception class, 2006/10/12 shino
    raise StandardError.new(error_message)
  end
end