Class: HTTY::Response

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

Overview

Encapsulates an HTTP(S) response.

Constant Summary collapse

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

#initialize(attributes = {}) ⇒ Response

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
28
# File 'lib/htty/response.rb', line 23

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

Instance Attribute Details

#statusObject (readonly)

Returns the HTTP status associated with the response.



10
11
12
# File 'lib/htty/response.rb', line 10

def status
  @status
end

#timeObject (readonly)

Returns a benchmark time to receive the response.



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

def time
  @time
end

Instance Method Details

#cookiesObject

Returns an array of the cookies belonging to the response.



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

def cookies
  cookies_from(COOKIES_HEADER_NAME)
end

#follow_relative_to(request) ⇒ Object



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

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

#locationObject



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

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