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.



47
48
49
# File 'lib/wgit/response.rb', line 47

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.



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

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.



69
70
71
# File 'lib/wgit/response.rb', line 69

def failure?
  !success?
end

#inspectString

Overrides String#inspect to shorten the printed output of a Response.

Returns:

  • (String)

    A short textual representation of this Response.



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

def inspect
  "#<Wgit::Response url=\"#{@url}\" status=#{status}>"
end

#no_index?Boolean

Returns whether or not Wgit is banned from indexing this site.

Returns:

  • (Boolean)

    True if Wgit should not index this site, false otherwise.



148
149
150
# File 'lib/wgit/response.rb', line 148

def no_index?
  headers.fetch(:x_robots_tag, '').downcase.strip == 'noindex'
end

#not_found?Boolean

Returns whether or not the response is 404 Not Found.

Returns:

  • (Boolean)

    True if 404 Not Found, false otherwise.



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

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.



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

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.



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

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.



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

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.



123
124
125
# File 'lib/wgit/response.rb', line 123

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.



138
139
140
141
142
# File 'lib/wgit/response.rb', line 138

def success?
  return false unless @status

  @status.positive?
end