Class: HTTPkit::Response

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

Constant Summary

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

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



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

- (Object) body (readonly)

Returns the value of attribute body



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

def body
  @body
end

- (Object) headers (readonly)

Returns the value of attribute headers



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

def headers
  @headers
end

- (Object) http_version (readonly)

Returns the value of attribute http_version



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

def http_version
  @http_version
end

- (Object) status (readonly)

Returns the value of attribute status



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

def status
  @status
end

Instance Method Details

- (Boolean) body_included?



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

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

- (Boolean) body_present?



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

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

- (Boolean) client_error?



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

def client_error?
  status_class == 4
end

- (Boolean) informational?



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

def informational?
  status_class == 1
end

- (Object) new_self(args)



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

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

- (Boolean) redirection?



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

def redirection?
  status_class == 3
end

- (Boolean) server_error?



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

def server_error?
  status_class == 5
end

- (Object) status_class



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

def status_class
  status / 100
end

- (Object) status_name



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

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

- (Boolean) successful?



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

def successful?
  status_class == 2
end

- (Object) with(index, argument)



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

- (Object) with_body(new_body)



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

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

- (Object) with_headers(new_headers)



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

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