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



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ap4r/dispatcher.rb', line 227

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|
    @response, = http.post(uri.path, @message.object, headers)
  end
end

#make_headerObject



240
241
242
243
244
245
246
247
248
# File 'lib/ap4r/dispatcher.rb', line 240

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



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

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.



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/ap4r/dispatcher.rb', line 272

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.



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ap4r/dispatcher.rb', line 258

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