Class: HTTY::Response

Inherits:
Payload show all
Defined in:
lib/htty/response.rb

Overview

Encapsulates an HTTP(S) response.

Constant Summary

COOKIES_HEADER_NAME =
'Set-Cookie'
LOCATION_HEADER_NAME =
'Location'

Instance Attribute Summary (collapse)

Attributes inherited from Payload

#body

Instance Method Summary (collapse)

Methods inherited from Payload

#==, #cookies_from, #header, #headers, #headers_with_key

Constructor Details

- (Response) initialize(attributes = {})

Initializes a new HTTY::Response with attribute values specified in the attributes hash.

Valid attributes keys include:

  • :body

  • :headers

  • :status



23
24
25
26
27
# File 'lib/htty/response.rb', line 23

def initialize(attributes={})
  super attributes
  @status = attributes[:status]
  @already_followed = false
end

Instance Attribute Details

- (Object) status (readonly)

Returns the HTTP status associated with the response.



13
14
15
# File 'lib/htty/response.rb', line 13

def status
  @status
end

Instance Method Details

- (Object) cookies

Returns an array of the cookies belonging to the response.



30
31
32
# File 'lib/htty/response.rb', line 30

def cookies
  cookies_from(COOKIES_HEADER_NAME)
end

- (Object) follow_relative_to(request)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/htty/response.rb', line 34

def follow_relative_to(request)
  return request if @already_followed
  location_uri = URI.parse(location_header = location)
  if location_uri.absolute?
    request.address location_header
  else
    request.
      path_set(location_uri.path).
      query_set_all(location_uri.query).
      fragment_set(location_uri.fragment)
  end
ensure
  @already_followed = true
end

- (Object) location



49
50
51
# File 'lib/htty/response.rb', line 49

def location
  header(LOCATION_HEADER_NAME, HTTY::NoLocationHeaderError.new)
end