Class: M2R::Response

Inherits:
Object
  • Object
show all
Includes:
HTTP::Close
Defined in:
lib/m2r/response.rb,
lib/m2r/response/to_request.rb,
lib/m2r/response/always_close.rb,
lib/m2r/response/content_length.rb

Overview

Simplest possible abstraction layer over HTTP request

Direct Known Subclasses

Reply

Defined Under Namespace

Modules: AlwaysClose, ContentLength, ToRequest

Constant Summary collapse

VERSION =
"HTTP/1.1".freeze
CRLF =
"\r\n".freeze
STATUS_CODES =
{
  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',
  306 => 'Reserved',
  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',
  418 => "I'm a Teapot",
  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',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTP::Close

#close?

Constructor Details

#initializeResponse

Returns a new instance of Response.



74
75
76
77
78
79
# File 'lib/m2r/response.rb', line 74

def initialize
  status(200)
  headers(Headers.new)
  body("")
  http_version(VERSION)
end

Instance Attribute Details

#reasonObject (readonly)



72
73
74
# File 'lib/m2r/response.rb', line 72

def reason
  @reason
end

Instance Method Details

#body(value = GETTER) ⇒ Object

Parameters:

  • value (String, nil) (defaults to: GETTER)

    HTTP body



114
115
116
117
118
119
120
121
# File 'lib/m2r/response.rb', line 114

def body(value = GETTER)
  if value == GETTER
    @body
  else
    @body = value
    self
  end
end

#header(header, value = GETTER) ⇒ Object

Parameters:

  • header (Hash)

    HTTP header key

  • value (Hash) (defaults to: GETTER)

    HTTP header value



104
105
106
107
108
109
110
111
# File 'lib/m2r/response.rb', line 104

def header(header, value = GETTER)
  if value == GETTER
    @headers[header]
  else
    @headers[header] = value
    self
  end
end

#headers(value = GETTER) ⇒ Object

Parameters:

  • value (Hash) (defaults to: GETTER)

    HTTP headers



93
94
95
96
97
98
99
100
# File 'lib/m2r/response.rb', line 93

def headers(value = GETTER)
  if value == GETTER
    @headers
  else
    @headers = value
    self
  end
end

#http_version(value = GETTER) ⇒ Object

Parameters:

  • version (String, nil)

    HTTP body



124
125
126
127
128
129
130
131
# File 'lib/m2r/response.rb', line 124

def http_version(value = GETTER)
  if value == GETTER
    @version
  else
    @version = value
    self
  end
end

#status(value = GETTER) ⇒ Object

Parameters:

  • value (Fixnum, #to_i) (defaults to: GETTER)

    HTTP status code



82
83
84
85
86
87
88
89
90
# File 'lib/m2r/response.rb', line 82

def status(value = GETTER)
  if value == GETTER
    @status
  else
    @status = value.to_i
    @reason = STATUS_CODES[@status]
    self
  end
end

#to_sString

Returns HTTP Response.

Returns:

  • (String)

    HTTP Response



134
135
136
137
138
139
140
141
142
# File 'lib/m2r/response.rb', line 134

def to_s
  response = "#{http_version} #{status} #{reason}#{CRLF}"
  unless headers.empty?
    response << headers.map { |h, v| "#{h}: #{v}" }.join(CRLF) << CRLF
  end
  response << CRLF
  response << body.to_s
  response
end