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.



75
76
77
78
79
80
81
82
83
# File 'lib/restify/response.rb', line 75

def initialize(request, uri, code, headers, body)
  @request = request
  @uri     = uri
  @code    = code
  @status  = STATUS_CODE_TO_SYMBOL[code]
  @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.



32
33
34
# File 'lib/restify/response.rb', line 32

def body
  @body
end

#codeFixnum (readonly)

Response status code.

Returns:

  • (Fixnum)

    Status code.



44
45
46
# File 'lib/restify/response.rb', line 44

def code
  @code
end

#headersHash<String, String> (readonly)

Response headers as hash.

Returns:

  • (Hash<String, String>)

    Response headers.



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

def headers
  @headers
end

#messageString (readonly)

Response status message.

Returns:

  • (String)

    Status message.



59
60
61
# File 'lib/restify/response.rb', line 59

def message
  @message
end

#requestRequest (readonly)

The request that led to this response.

Returns:



65
66
67
# File 'lib/restify/response.rb', line 65

def request
  @request
end

#statusSymbol (readonly)

Response status symbol.

Examples:

response.status #=> :ok

Returns:

  • (Symbol)

    Status symbol.



53
54
55
# File 'lib/restify/response.rb', line 53

def status
  @status
end

#uriAddressable::URI (readonly)

Last effective URI.

Returns:

  • (Addressable::URI)

    Last effective URI.



71
72
73
# File 'lib/restify/response.rb', line 71

def uri
  @uri
end

Instance Method Details

#decoded_bodyArray, ...

Return decoded body according to content type. Will return ‘nil` if content cannot be decoded.

Returns:

  • (Array, Hash, NilClass)

    Decoded response body.



109
110
111
112
113
114
115
116
# File 'lib/restify/response.rb', line 109

def decoded_body
  @decoded_body ||= begin
    case headers['Content-Type']
      when /\Aapplication\/json($|;)/
        MultiJson.load body
    end
  end
end

Return list of links from the Link header.

Returns:

  • (Array<Link>)

    Links.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/restify/response.rb', line 89

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.



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

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