Class: SqsPoller::Process::MessageHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sqspoller/process/message_handler.rb

Constant Summary collapse

HEADERS =
{
  'Content-Type' => 'application/json',
  'Accept' => 'application/json'
}

Instance Method Summary collapse

Constructor Details

#initialize(worker_configuration) ⇒ MessageHandler

Returns a new instance of MessageHandler.



17
18
19
20
21
22
23
# File 'lib/sqspoller/process/message_handler.rb', line 17

def initialize(worker_configuration)
  @logger = SqsPoller::Logger.get_new_logger(self.class.name)
  @http_method = worker_configuration[:http_method]
  @http_url = worker_configuration[:http_url]
  @timeout = worker_configuration[:timeout] ? worker_configuration[:timeout].to_i : 450
  @uri = URI(@http_url)
end

Instance Method Details

#handle(message, message_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sqspoller/process/message_handler.rb', line 25

def handle(message, message_id)
  parsed_message = JSON.parse(message)
  # @logger.info parsed_message
  if @http_method.downcase == "post"
    RestClient::Request.execute(:method => :post, :url => @http_url, :payload => parsed_message.to_json, :headers => HEADERS, :timeout => @timeout, :open_timeout => 5) do |response, request, result|
      process_http_response response
    end
  elsif @http_method.downcase == "get"
    RestClient::Request.execute(:method => :get, :url => @http_url, :payload => parsed_message.to_json, :headers => HEADERS, :timeout => @timeout, :open_timeout => 5) do |response, request, result|
      process_http_response response
    end
  else
    raise "Invalid http_method provided. #{http_method}"
  end
  parsed_message["MessageType"]
end

#process_http_response(response) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/sqspoller/process/message_handler.rb', line 42

def process_http_response(response)
  case response.code
  when 200
    return "OK"
  else
    raise "Service did not return 200 OK response. #{response.code}"
  end
end