class ResponseHandler

Attributes

responseHash[R]

Public Class Methods

new(responseQueue) click to toggle source
# File lib/handlers/response_handler.rb, line 7
def initialize(responseQueue)
  @responseHash = queueToHash(responseQueue)
end

Public Instance Methods

clearResponseHash() click to toggle source

Clears out the response queue but is self explanatory :)

# File lib/handlers/response_handler.rb, line 19
def clearResponseHash
  @responseHash.clear
end
getResponsesByURI(uri) click to toggle source

Returns a hash of Responses based on the input URI

# File lib/handlers/response_handler.rb, line 25
def getResponsesByURI(uri)
  returnHash = Hash.new
  @responseHash.each do |index,value|
    if value.getRequestURI.to_s.casecmp(uri)
      returnHash[index] = value
    end
  end
  returnHash
end
listResponses() click to toggle source
# File lib/handlers/response_handler.rb, line 41
def listResponses
  @responseHash.each do |key,value|
    puts "Request ID: #{key} had a response code of #{value.code}, response body of #{value.body}, response message of #{value.msg}"
    puts "For URI: #{value.getRequestURI}, and Post Body: #{value.getPostBody}"
  end
end
updateHash(queue) click to toggle source

Merges the inputed queue into the responseHash using the queueToHash method and the Hash's .merge method

# File lib/handlers/response_handler.rb, line 13
def updateHash(queue)
  @responseHash = queueToHash(queue).merge(@responseHash)
end

Private Instance Methods

clearResponsesByURI(uri) click to toggle source
# File lib/handlers/response_handler.rb, line 36
def clearResponsesByURI(uri)
  #TODO: Not implemented
end
queueToHash(queue) click to toggle source

Adds responses into a hash. The Key is the Request ID of the request that was made that gave the response (Should be unique), the value is the response object.

# File lib/handlers/response_handler.rb, line 50
def queueToHash(queue)
  returnHash = Hash.new
  current = Thread.new do
    while queue.length > 0
      response = queue.pop
      returnHash[response.getRequestID] = response
    end
  end
  current.join
  returnHash
end