Class: Restfully::HTTP::Response

Inherits:
Object
  • Object
show all
Includes:
Headers, Parsing
Defined in:
lib/restfully/http/response.rb

Overview

Container for an HTTP Response. Has status, headers and body properties.

Constant Summary

Constants included from Parsing

Parsing::PARSERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Headers

#sanitize_http_headers

Methods included from Parsing

#select_parser_for, #serialize, #unserialize

Constructor Details

#initialize(status, headers, body) ⇒ Response

body

may be a string (that will be parsed when calling the #body function), or an object (that will be returned as is when calling the #body function)



9
10
11
12
13
# File 'lib/restfully/http/response.rb', line 9

def initialize(status, headers, body)
  @status = status.to_i
  @headers = sanitize_http_headers(headers)
  @body = body
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/restfully/http/response.rb', line 6

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/restfully/http/response.rb', line 6

def status
  @status
end

Instance Method Details

#bodyObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/restfully/http/response.rb', line 15

def body
  if @body.kind_of?(String)
    if @body.empty?
      nil
    else
      @unserialized_body ||= unserialize(@body, :content_type => @headers['Content-Type'])
    end
  else
    @body
  end
end

#raw_bodyObject



27
28
29
30
31
32
33
# File 'lib/restfully/http/response.rb', line 27

def raw_body
  if @body.kind_of?(String)
    @body
  else
    @serialized_body ||= serialize(@body, :content_type => @headers['Content-Type']) unless @body.nil?
  end
end