Class: Wpxf::Net::HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/wpxf/net/http_response.rb

Overview

A response from a request made by a HttpClient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ HttpResponse

Returns a new instance of HttpResponse

Parameters:

  • res (Object)

    a response to parse.



8
9
10
# File 'lib/wpxf/net/http_response.rb', line 8

def initialize(res)
  parse_typhoeus_response(res) if res.is_a? Typhoeus::Response
end

Instance Attribute Details

#bodyString

Returns the response body.

Returns:

  • (String)

    the response body.



32
33
34
# File 'lib/wpxf/net/http_response.rb', line 32

def body
  @body
end

#codeInteger

Returns the HTTP status code.

Returns:

  • (Integer)

    the HTTP status code.



29
30
31
# File 'lib/wpxf/net/http_response.rb', line 29

def code
  @code
end

#cookiesHash

Returns a CookieJar with all returned cookies.

Returns:

  • (Hash)

    a CookieJar with all returned cookies.



41
42
43
# File 'lib/wpxf/net/http_response.rb', line 41

def cookies
  @cookies
end

#headersHash

Returns a hash of all returned headers.

Returns:

  • (Hash)

    a hash of all returned headers.



35
36
37
# File 'lib/wpxf/net/http_response.rb', line 35

def headers
  @headers
end

#timed_outBoolean

Returns a boolean that indicates whether a request timed out.

Returns:

  • (Boolean)

    a boolean that indicates whether a request timed out.



38
39
40
# File 'lib/wpxf/net/http_response.rb', line 38

def timed_out
  @timed_out
end

Instance Method Details

#parse_typhoeus_response(res) ⇒ nil

Parse a Typhoeus response into the object.

Parameters:

  • res (Typhoeus::Response)

    the response object to parse.

Returns:

  • (nil)


15
16
17
18
19
20
21
# File 'lib/wpxf/net/http_response.rb', line 15

def parse_typhoeus_response(res)
  self.code = res.code
  self.body = res.body.nil? ? '' : res.body
  self.headers = res.headers
  self.timed_out = res.timed_out? || res.return_code == :couldnt_connect
  self.cookies = CookieJar.new.parse(res.headers['Set-Cookie']) if res.headers
end

#timed_out?Boolean

Returns a boolean that indicates whether a request timed out.

Returns:

  • (Boolean)

    a boolean that indicates whether a request timed out.



24
25
26
# File 'lib/wpxf/net/http_response.rb', line 24

def timed_out?
  timed_out
end