class HttpJob

Constants

HTTP_DELETE
HTTP_GET
HTTP_METHODS
HTTP_POST
HTTP_PUT

Public Class Methods

create_request(method, uri, headers, body) click to toggle source
# File lib/piscina/http_job.rb, line 38
def self.create_request(method, uri, headers, body)
  raise "http_method or uri not defined" unless method && uri
  raise "#{method} is not a valid HTTP method" unless HTTP_METHODS.include?(method)

  begin
    uri_obj = URI.parse(uri)
  rescue => e
    raise "There was an error parsing #{uri}. ErrorMessage: #{e.message}"
  end

  req = "Net::HTTP::#{method.capitalize}".constantize.new(uri_obj.request_uri)

  if headers
    headers.each do |header, val|
      req[header] = val
    end
  end

  if [HTTP_PUT, HTTP_POST].include?(method) && body
    req.body = body
  end

  req
end
get_response(req, uri) click to toggle source
# File lib/piscina/http_job.rb, line 63
def self.get_response(req, uri)
  uri = URI.parse(uri)

  Net::HTTP.start(uri.hostname,
                  uri.port,
                  :use_ssl => uri.scheme == 'https') do |http|

    http.request(req)
  end
end
perform(msg) click to toggle source
# File lib/piscina/http_job.rb, line 17
def self.perform(msg)
  params  = JSON.parse(msg.body)

  method  = params["http_method"]
  uri     = params["uri"]
  headers = params["headers"]
  body    = params["body"]

  req = HttpJob.create_request(method, uri, headers, body)
  res = HttpJob.get_response(req, uri)

  # Net::HTTPSuccess covers all 2XX responses
  unless res.is_a?(Net::HTTPSuccess)
    # Messages not explicitly deleted will be placed back into the queue. DLQ policies will take
    # effect if a message cannot be processed a number of times.
    raise "Could not perform request to:#{uri.to_s}"
  end

  msg.delete
end