Class: Tilia::Http::Response

Inherits:
Object
  • Object
show all
Includes:
Message, ResponseInterface
Defined in:
lib/tilia/http/response.rb

Overview

This class represents a single HTTP response.

Instance Attribute Summary collapse

Attributes included from Message

#body, #headers, #http_version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MessageInterface

#add_header, #add_headers, #body, #body=, #body_as_stream, #body_as_string, #header, #header?, #header_as_array, #headers, #http_version, #http_version=, #remove_header, #update_header, #update_headers

Methods included from Message

#add_header, #add_headers, #body_as_stream, #body_as_string, #header, #header?, #header_as_array, #initialize_copy, #initialize_message, #remove_header, #update_header, #update_headers

Constructor Details

#initialize(status = nil, headers = nil, body = nil) ⇒ void

Creates the response object

Parameters:

  • status (String, Fixnum) (defaults to: nil)
  • array

    headers

  • resource

    body



97
98
99
100
101
102
103
# File 'lib/tilia/http/response.rb', line 97

def initialize(status = nil, headers = nil, body = nil)
  initialize_message # RUBY

  self.status = status if status
  update_headers(headers) if headers
  self.body = body if body
end

Instance Attribute Details

#statusObject

Returns the current HTTP status code.

Returns:

  • int



82
83
84
# File 'lib/tilia/http/response.rb', line 82

def status
  @status
end

#status_textString

Returns the human-readable status string.

In the case of a 200, this may for example be ‘OK’.

Returns:

  • (String)


87
88
89
# File 'lib/tilia/http/response.rb', line 87

def status_text
  @status_text
end

Class Method Details

.status_codesObject

This is the list of currently registered HTTP status codes.

Returns:

  • array



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tilia/http/response.rb', line 11

def self.status_codes
  {
    100 => 'Continue',
    101 => 'Switching Protocols',
    102 => 'Processing',
    200 => 'OK',
    201 => 'Created',
    202 => 'Accepted',
    203 => 'Non-Authorative Information',
    204 => 'No Content',
    205 => 'Reset Content',
    206 => 'Partial Content',
    207 => 'Multi-Status', # RFC 4918
    208 => 'Already Reported', # RFC 5842
    226 => 'IM Used', # RFC 3229
    300 => 'Multiple Choices',
    301 => 'Moved Permanently',
    302 => 'Found',
    303 => 'See Other',
    304 => 'Not Modified',
    305 => 'Use Proxy',
    307 => 'Temporary Redirect',
    308 => 'Permanent 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',
    418 => 'I\'m a teapot', # RFC 2324
    421 => 'Misdirected Request', # RFC7540 (HTTP/2)
    422 => 'Unprocessable Entity', # RFC 4918
    423 => 'Locked', # RFC 4918
    424 => 'Failed Dependency', # RFC 4918
    426 => 'Upgrade Required',
    428 => 'Precondition Required', # RFC 6585
    429 => 'Too Many Requests', # RFC 6585
    431 => 'Request Header Fields Too Large', # RFC 6585
    451 => 'Unavailable For Legal Reasons', # draft-tbray-http-legally-restricted-status
    500 => 'Internal Server Error',
    501 => 'Not Implemented',
    502 => 'Bad Gateway',
    503 => 'Service Unavailable',
    504 => 'Gateway Timeout',
    505 => 'HTTP Version not supported',
    506 => 'Variant Also Negotiates',
    507 => 'Insufficient Storage', # RFC 4918
    508 => 'Loop Detected', # RFC 5842
    509 => 'Bandwidth Limit Exceeded', # non-standard
    510 => 'Not extended',
    511 => 'Network Authentication Required' # RFC 6585
  }
end

Instance Method Details

#to_sString

Serializes the response object as a string.

This is useful for debugging purposes.

Returns:

  • (String)


150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tilia/http/response.rb', line 150

def to_s
  str = "HTTP/#{http_version} #{status} #{status_text}\r\n"
  headers.each do |key, value|
    value.each do |v|
      str << "#{key}: #{v}\r\n"
    end
  end

  str << "\r\n"
  str << body_as_string
  str
end