Class: Wgit::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/wgit/response.rb

Overview

Response class modeling a generic HTTP GET response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Defaults some values and returns a "blank" Wgit::Response object.


29
30
31
32
33
34
# File 'lib/wgit/response.rb', line 29

def initialize
  @body         = ''
  @headers      = {}
  @redirections = {}
  @total_time   = 0.0
end

Instance Attribute Details

#adapter_responseObject

The underlying HTTP adapter/library response object.


5
6
7
# File 'lib/wgit/response.rb', line 5

def adapter_response
  @adapter_response
end

#bodyObject Also known as: content, to_s

The HTML response body.


8
9
10
# File 'lib/wgit/response.rb', line 8

def body
  @body
end

#headersObject

The HTTP response headers.


11
12
13
# File 'lib/wgit/response.rb', line 11

def headers
  @headers
end

#ip_addressObject

The servers IP address.


14
15
16
# File 'lib/wgit/response.rb', line 14

def ip_address
  @ip_address
end

#redirectionsObject (readonly) Also known as: redirects

The redirections of the response.


17
18
19
# File 'lib/wgit/response.rb', line 17

def redirections
  @redirections
end

#statusObject Also known as: code

The HTTP response status code.


20
21
22
# File 'lib/wgit/response.rb', line 20

def status
  @status
end

#total_timeObject (readonly) Also known as: crawl_duration

The total crawl/network time for the response.


23
24
25
# File 'lib/wgit/response.rb', line 23

def total_time
  @total_time
end

#urlObject

The HTTP request URL.


26
27
28
# File 'lib/wgit/response.rb', line 26

def url
  @url
end

Instance Method Details

#add_total_time(time) ⇒ Float

Adds time to @total_time (incrementally).

Parameters:

  • time (Float)

    The time to add to @total_time.

Returns:

  • (Float)

    @total_time's new value.


40
41
42
# File 'lib/wgit/response.rb', line 40

def add_total_time(time)
  @total_time += (time || 0.0)
end

#body_or_nilString, NilClass

Returns the HTML response body or nil (if it's empty).

Returns:

  • (String, NilClass)

    The HTML body or nil if empty.


55
56
57
# File 'lib/wgit/response.rb', line 55

def body_or_nil
  @body.empty? ? nil : @body
end

#failure?Boolean

Returns whether or not a server response is absent.

Returns:

  • (Boolean)

    True if the status is nil or < 1, false otherwise.


62
63
64
# File 'lib/wgit/response.rb', line 62

def failure?
  !success?
end

#not_found?Boolean

Returns whether or not the response is 404 Not Found.

Returns:

  • (Boolean)

    True if 404 Not Found, false otherwise.


86
87
88
# File 'lib/wgit/response.rb', line 86

def not_found?
  @status == 404
end

#ok?Boolean

Returns whether or not the response is 200 OK.

Returns:

  • (Boolean)

    True if 200 OK, false otherwise.


93
94
95
# File 'lib/wgit/response.rb', line 93

def ok?
  @status == 200
end

#redirect?Boolean

Returns whether or not the response is a 3xx Redirect.

Returns:

  • (Boolean)

    True if 3xx Redirect, false otherwise.


100
101
102
103
104
# File 'lib/wgit/response.rb', line 100

def redirect?
  return false unless @status

  @status.between?(300, 399)
end

#redirect_countInteger

Returns the number of redirects this response has had.

Returns:

  • (Integer)

    The number of response redirects.


109
110
111
# File 'lib/wgit/response.rb', line 109

def redirect_count
  @redirections.size
end

#sizeInteger Also known as: length

Returns the size of the response body.

Returns:

  • (Integer)

    The response body size in bytes.


116
117
118
# File 'lib/wgit/response.rb', line 116

def size
  @body.size
end

#success?Boolean

Returns whether or not a server response is present.

Returns:

  • (Boolean)

    True if the status is > 0, false otherwise.


131
132
133
134
135
# File 'lib/wgit/response.rb', line 131

def success?
  return false unless @status

  @status.positive?
end