Class: HTTPAdapter::NetHTTPResponseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/httpadapter/adapters/net_http.rb

Constant Summary collapse

STATUS_MESSAGES =
{
  100 => "Continue",
  101 => "Switching Protocols",
  102 => "Processing",

  200 => "OK",
  201 => "Created",
  202 => "Accepted",
  203 => "Non-Authoritative Information",
  204 => "No Content",
  205 => "Reset Content",
  206 => "Partial Content",
  207 => "Multi-Status",
  226 => "IM Used",

  300 => "Multiple Choices",
  301 => "Moved Permanently",
  302 => "Found",
  303 => "See Other",
  304 => "Not Modified",
  305 => "Use Proxy",
  307 => "Temporary Redirect",

  400 => "Bad Request",
  401 => "Unauthorized",
  402 => "Payment Required",
  403 => "Forbidden",
  404 => "Not Found",
  405 => "Method Not Allowed",
  406 => "Not Acceptable",
  407 => "Proxy Authentication Required",
  408 => "Request Timeout",
  409 => "Conflict",
  410 => "Gone",
  411 => "Length Required",
  412 => "Precondition Failed",
  413 => "Request Entity Too Large",
  414 => "Request-URI Too Long",
  415 => "Unsupported Media Type",
  416 => "Requested Range Not Satisfiable",
  417 => "Expectation Failed",
  422 => "Unprocessable Entity",
  423 => "Locked",
  424 => "Failed Dependency",
  426 => "Upgrade Required",

  500 => "Internal Server Error",
  501 => "Not Implemented",
  502 => "Bad Gateway",
  503 => "Service Unavailable",
  504 => "Gateway Timeout",
  505 => "HTTP Version Not Supported",
  507 => "Insufficient Storage",
  510 => "Not Extended"
}
STATUS_MAPPING =
Net::HTTPResponse::CODE_TO_OBJ

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ NetHTTPResponseAdapter

Returns a new instance of NetHTTPResponseAdapter.



208
209
210
211
212
213
# File 'lib/httpadapter/adapters/net_http.rb', line 208

def initialize(response)
  unless response.kind_of?(Net::HTTPResponse)
    raise TypeError, "Expected Net::HTTPResponse, got #{response.class}."
  end
  @response = response
end

Class Method Details

.from_ary(array) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/httpadapter/adapters/net_http.rb', line 225

def self.from_ary(array)
  status, headers, body = array
  message = STATUS_MESSAGES[status.to_i]
  response_class = STATUS_MAPPING[status.to_s]
  unless message && response_class
    raise ArgumentError, "Unknown status code: #{status}"
  end
  status = status.to_i
  response = response_class.new('1.1', status.to_s, message)
  headers.each do |header, value|
    response.add_field(header, value)
  end
  merged_body = ""
  body.each do |chunk|
    merged_body += chunk
  end

  # Ugh
  response.instance_variable_set('@read', true)
  response.instance_variable_set('@body', merged_body)

  return response
end

Instance Method Details

#to_aryObject



215
216
217
218
219
220
221
222
223
# File 'lib/httpadapter/adapters/net_http.rb', line 215

def to_ary
  status = @response.code.to_i
  headers = []
  @response.canonical_each do |header, value|
    headers << [header, value]
  end
  body = @response.body || ""
  return [status, headers, [body]]
end