class Muxer::Request

Muxer::Request is designed to wrap the requests that Muxer uses to parallelize the web requests and handle timeouts.

@!attribute url

@return [String] URL to use

@!attribute timeout

@return [Number] Seconds for the timeout

@!attribute headers

@return [Hash] Request headers to use with the request

@!attribute params

@return [Hash] request parameters

@!attribute redirects

@return [Integer] How many redirects to follow

@!attribute method

@return [Symbol] HTTP method to use

@!attribute completed

@return [Boolean] Is the request completed

@!attribute error

@return [Boolean] Have we had an error?

@!attribute id

@return [Symbol] ID for this request, the ID is arbitrary and to
be assigned by the user

@!attribute runtime

@return  [Float] Runtime for the request

Attributes

completed[R]
completed?[R]
error[R]
headers[RW]
id[RW]
method[R]
params[RW]
redirects[RW]
runtime[R]
timeout[RW]
url[RW]

Public Class Methods

new() click to toggle source
# File lib/muxer/request.rb, line 31
def initialize
  @method = :get
  @completed = false
  @timeout = 10
  @headers = {}
  @params = {}
  @request = nil
  @error = nil
end

Public Instance Methods

method=(method) click to toggle source

sets the HTTP method of the request as long as the method is one off the standard http methods. The method can be sent in as a string or a symbol. The valid options are: :get, :post, :head, :options, :put, :delete

@param method [string, symbol] HTTP Method of the request @return true

# File lib/muxer/request.rb, line 49
def method=(method)
  method = method.downcase.to_sym

  @method = method if [:get, :post, :head, :options, :put, :delete].include? method
  true
end
process!() click to toggle source

process! executes the web request. It cannot be called from outside of an EventMachine loop.

@return self

# File lib/muxer/request.rb, line 60
def process!
  @start = Time.now
  http = EventMachine::HttpRequest.new(url,
    connect_timeout: timeout,
    inactivity_timeout: timeout,
  )

  @request = http.public_send(method,
    request_options
  )

  @request.callback { @completed = true; @runtime = Time.now - @start; @start = nil }
  @request.errback { @completed = @error = true; @runtime = Time.now - @start; @start = nil}
  self
end
response() click to toggle source

response is the actual http request’s response.

# File lib/muxer/request.rb, line 77
def response
  if @request
    @request.response
  end
end

Private Instance Methods

empty?(opt) click to toggle source
# File lib/muxer/request.rb, line 100
def empty?(opt)
  (opt.nil? || opt == {} || opt == [] || opt == '')
end
request_options() click to toggle source
# File lib/muxer/request.rb, line 85
def request_options
  options = {
    head: headers
  }
  if [:post, :put].include? method
    options[:body] = params
  else
    options[:query] = params
  end

  options[:redirects] = redirects
  options.reject!{|_,v| empty? v}
  options
end