Class: HTTPkit::Response

Inherits:
Object
  • Object
show all
Includes:
Adamantium, Support::Message
Defined in:
lib/httpkit/response.rb

Constant Summary collapse

NOT_INCLUDED =
[204, 304]
NOT_PRESENT =
[205]
UNKNOWN_STATUS =
'Unknown Status'.freeze
STATUS_NAMES =

TODO: double-check with RFC7230

{ 100 => 'Continue'.freeze,
101 => 'Switching Protocols'.freeze,
102 => 'Processing'.freeze,
200 => 'OK'.freeze,
201 => 'Created'.freeze,
202 => 'Accepted'.freeze,
203 => 'Non-Authoritative Information'.freeze,
204 => 'No Content'.freeze,
205 => 'Reset Content'.freeze,
206 => 'Partial Content'.freeze,
207 => 'Multi-Status'.freeze,
208 => 'Already Reported'.freeze,
226 => 'IM Used'.freeze,
300 => 'Multiple Choices'.freeze,
301 => 'Moved Permanently'.freeze,
302 => 'Found'.freeze,
303 => 'See Other'.freeze,
304 => 'Not Modified'.freeze,
305 => 'Use Proxy'.freeze,
306 => 'Reserved'.freeze,
307 => 'Temporary Redirect'.freeze,
308 => 'Permanent Redirect'.freeze,
400 => 'Bad Request'.freeze,
401 => 'Unauthorized'.freeze,
402 => 'Payment Required'.freeze,
403 => 'Forbidden'.freeze,
404 => 'Not Found'.freeze,
405 => 'Method Not Allowed'.freeze,
406 => 'Not Acceptable'.freeze,
407 => 'Proxy Authentication Required'.freeze,
408 => 'Request Timeout'.freeze,
409 => 'Conflict'.freeze,
410 => 'Gone'.freeze,
411 => 'Length Required'.freeze,
412 => 'Precondition Failed'.freeze,
413 => 'Request Entity Too Large'.freeze,
414 => 'Request-URI Too Long'.freeze,
415 => 'Unsupported Media Type'.freeze,
416 => 'Requested Range Not Satisfiable'.freeze,
417 => 'Expectation Failed'.freeze,
422 => 'Unprocessable Entity'.freeze,
423 => 'Locked'.freeze,
424 => 'Failed Dependency'.freeze,
425 =>
  'Reserved for WebDAV advanced collections expired proposal'.freeze,
426 => 'Upgrade Required'.freeze,
427 => 'Unassigned'.freeze,
428 => 'Precondition Required'.freeze,
429 => 'Too Many Requests'.freeze,
430 => 'Unassigned'.freeze,
431 => 'Request Header Fields Too Large'.freeze,
500 => 'Internal Server Error'.freeze,
501 => 'Not Implemented'.freeze,
502 => 'Bad Gateway'.freeze,
503 => 'Service Unavailable'.freeze,
504 => 'Gateway Timeout'.freeze,
505 => 'HTTP Version Not Supported'.freeze,
506 => 'Variant Also Negotiates (Experimental)'.freeze,
507 => 'Insufficient Storage'.freeze,
508 => 'Loop Detected'.freeze,
509 => 'Unassigned'.freeze,
510 => 'Not Extended'.freeze,
511 => 'Network Authentication Required'.freeze }

Constants included from Support::Message

Support::Message::CHUNKED, Support::Message::CONTENT_LENGTH, Support::Message::TRANSFER_ENCODING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Message

#body_headers, build, #close, #closed, #closed!, #closed?, #reject_closed

Constructor Details

#initialize(status, headers = {}, body = '', http_version = 1.1) ⇒ Response

Returns a new instance of Response.



13
14
15
16
17
18
# File 'lib/httpkit/response.rb', line 13

def initialize(status, headers = {}, body = '', http_version = 1.1)
  @status = status
  @headers = headers
  @body = Body.build(body)
  @http_version = http_version
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



11
12
13
# File 'lib/httpkit/response.rb', line 11

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



11
12
13
# File 'lib/httpkit/response.rb', line 11

def headers
  @headers
end

#http_versionObject (readonly)

Returns the value of attribute http_version.



11
12
13
# File 'lib/httpkit/response.rb', line 11

def http_version
  @http_version
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/httpkit/response.rb', line 11

def status
  @status
end

Instance Method Details

#body_included?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/httpkit/response.rb', line 38

def body_included?
  !informational? && !NOT_INCLUDED.include?(status)
end

#body_present?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/httpkit/response.rb', line 42

def body_present?
  !informational? && !NOT_PRESENT.include?(status)
end

#client_error?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/httpkit/response.rb', line 66

def client_error?
  status_class == 4
end

#informational?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/httpkit/response.rb', line 54

def informational?
  status_class == 1
end

#new_self(args) ⇒ Object



34
35
36
# File 'lib/httpkit/response.rb', line 34

def new_self(args)
  self.class.new(*args)
end

#redirection?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/httpkit/response.rb', line 62

def redirection?
  status_class == 3
end

#server_error?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/httpkit/response.rb', line 70

def server_error?
  status_class == 5
end

#status_classObject



50
51
52
# File 'lib/httpkit/response.rb', line 50

def status_class
  status / 100
end

#status_nameObject



46
47
48
# File 'lib/httpkit/response.rb', line 46

def status_name
  STATUS_NAMES[status.to_i] || UNKNOWN_STATUS
end

#successful?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/httpkit/response.rb', line 58

def successful?
  status_class == 2
end

#with(index, argument) ⇒ Object



28
29
30
31
32
# File 'lib/httpkit/response.rb', line 28

def with(index, argument)
  args = [status, headers, body, http_version]
  args[index] = argument
  new_self(args)
end

#with_body(new_body) ⇒ Object



24
25
26
# File 'lib/httpkit/response.rb', line 24

def with_body(new_body)
  with(2, Body.build(new_body))
end

#with_headers(new_headers) ⇒ Object



20
21
22
# File 'lib/httpkit/response.rb', line 20

def with_headers(new_headers)
  with(1, headers.merge(new_headers))
end