Sha256: a80f8482cbac832a7fc0c6cc03751e3ec93cf18c389f88120c5f155b73eefc0c

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require "concurrent"
require "net/http"
require "rest-client"
require "json"
require "sqspoller/logger/logger"

# This handles the SQS message and forward to local puma server and get the response.
module SqsPoller
  module Process
    class MessageHandler

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

      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

      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

      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

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fourkites-sqspoller-v2-1.0.0 lib/sqspoller/process/message_handler.rb