Class: Restify::Response

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

Overview

A Response is returned from an Adapter and described a HTTP response. That includes status code, headers and body.

A Response is also responsible for decoding its body according its content type.

Constant Summary collapse

SYMBOL_TO_STATUS_CODE =

Map of status symbols to codes. From Rack::Utils.

Examples:

SYMBOL_TO_STATUS_CODE[:ok] #=> 200
Rack::Utils::SYMBOL_TO_STATUS_CODE
STATUS_CODE_TO_SYMBOL =

Map of status codes to symbols.

Examples:

STATUS_CODE_TO_SYMBOL[200] #=> :ok
SYMBOL_TO_STATUS_CODE.invert

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, uri, code, headers, body) ⇒ Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Response.



78
79
80
81
82
83
84
85
86
# File 'lib/restify/response.rb', line 78

def initialize(request, uri, code, headers, body)
  @request = request
  @uri     = uri
  @code    = code
  @status  = STATUS_CODE_TO_SYMBOL[code]
  @headers = convert_headers(headers)
  @body    = body
  @message = Rack::Utils::HTTP_STATUS_CODES[code]
end

Instance Attribute Details

#bodyString (readonly)

Response body as string.

Returns:

  • (String)

    Response body.



35
36
37
# File 'lib/restify/response.rb', line 35

def body
  @body
end

#codeFixnum (readonly)

Response status code.

Returns:

  • (Fixnum)

    Status code.



47
48
49
# File 'lib/restify/response.rb', line 47

def code
  @code
end

#headersHash<String, String> (readonly)

Response headers as hash.

Returns:

  • (Hash<String, String>)

    Response headers.



41
42
43
# File 'lib/restify/response.rb', line 41

def headers
  @headers
end

#messageString (readonly)

Response status message.

Returns:

  • (String)

    Status message.



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

def message
  @message
end

#requestRequest (readonly)

The request that led to this response.

Returns:



68
69
70
# File 'lib/restify/response.rb', line 68

def request
  @request
end

#statusSymbol (readonly)

Response status symbol.

Examples:

response.status #=> :ok

Returns:

  • (Symbol)

    Status symbol.



56
57
58
# File 'lib/restify/response.rb', line 56

def status
  @status
end

#uriAddressable::URI (readonly)

Last effective URI.

Returns:

  • (Addressable::URI)

    Last effective URI.



74
75
76
# File 'lib/restify/response.rb', line 74

def uri
  @uri
end

Instance Method Details

#content_typeString

Return content type header from response headers.

Returns:

  • (String)

    Content type header.



113
114
115
# File 'lib/restify/response.rb', line 113

def content_type
  headers['CONTENT_TYPE']
end

#decoded_bodyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



136
137
138
139
140
141
142
143
# File 'lib/restify/response.rb', line 136

def decoded_body
  @decoded_body ||= begin
    case content_type
      when %r{\Aapplication/json($|;)}
        ::JSON.parse(body)
    end
  end
end

#errored?Boolean

Check if response is erroneous e.g. the status code is one of 4XX or 5XX.

Returns:

  • (Boolean)

    True if status code is 2XX otherwise false.



131
132
133
# File 'lib/restify/response.rb', line 131

def errored?
  (400...600).cover? code
end

#follow_locationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



146
147
148
# File 'lib/restify/response.rb', line 146

def follow_location
  headers['LOCATION'] || headers['CONTENT_LOCATION']
end

Return list of links from the Link header.

rubocop:disable Metrics/MethodLength

Returns:

  • (Array<Link>)

    Links.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/restify/response.rb', line 93

def links
  @links ||= begin
    if headers['LINK']
      begin
        Link.parse(headers['LINK'])
      rescue ArgumentError => e
        warn e
        []
      end
    else
      []
    end
  end
end

#success?Boolean

Check if response is successful e.g. the status code is on of 2XX.

Returns:

  • (Boolean)

    True if status code is 2XX otherwise false.



122
123
124
# File 'lib/restify/response.rb', line 122

def success?
  (200...300).cover? code
end