Class: REST::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rest/response.rb

Overview

Response holds a HTTP response

Constant Summary collapse

CODES =

These codes are used to define convenience boolean accessors on the response object.

Examples

REST::Response.new(200).ok? #=> true
REST::Response.new(201).ok? #=> falses
REST::Response.new(403).forbidden? #=> true
[
  [200, :ok],
  [201, :created],
  [301, :moved_permanently],
  [302, :found],
  [400, :bad_request],
  [401, :unauthorized],
  [403, :forbidden],
  [422, :unprocessable_entity],
  [404, :not_found],
  [500, :internal_server_error]
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code, headers = {}, body = '') ⇒ Response

  • status_code: The status code of the response (ie. 200 or ‘404’)

  • headers: The headers of the response

  • body: The body of the response



29
30
31
32
33
# File 'lib/rest/response.rb', line 29

def initialize(status_code, headers={}, body='')
  @status_code = status_code.to_i
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#status_codeObject

Returns the value of attribute status_code.



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

def status_code
  @status_code
end

Instance Method Details

#success?Boolean

Returns true when the status code is in the 2XX range. Returns false otherwise.

Returns:

  • (Boolean)


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

def success?
  (status_code.to_s =~ /2../) ? true : false
end