Class: Spidr::Page

Inherits:
Object
  • Object
show all
Includes:
Body, Headers, Links
Defined in:
lib/spidr/page.rb

Overview

Represents a requested page from a website.

Constant Summary

Constants included from Headers

Headers::RESERVED_COOKIE_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Links

#each_link, #each_meta_redirect, #each_redirect, #each_url, #links, #meta_redirect?, #meta_redirects, #redirects_to, #to_absolute, #urls

Methods included from Body

#at, #body, #doc, #search, #title

Methods included from Headers

#atom?, #bad_request?, #code, #content_type, #content_types, #cookie, #cookie_params, #cookies, #css?, #directory?, #had_internal_server_error?, #html?, #is_content_type?, #is_forbidden?, #is_missing?, #is_ok?, #is_unauthorized?, #javascript?, #json?, #ms_word?, #pdf?, #plain_text?, #rss?, #timedout?, #xml?, #xsl?, #zip?

Constructor Details

#initialize(url, response) ⇒ Page

Creates a new Page object.

Parameters:

  • url (URI::HTTP)

    The URL of the page.

  • response (Net::HTTP::Response)

    The response from the request for the page.



33
34
35
36
37
38
# File 'lib/spidr/page.rb', line 33

def initialize(url,response)
  @url = url
  @response = response
  @headers = response.to_hash
  @doc = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ String (protected)

Provides transparent access to the values in #headers.

Parameters:

  • name (Symbol)

    The name of the missing method.

  • arguments (Array)

    Additional arguments for the missing method.

Returns:

  • (String)

    The missing method mapped to a header in #headers.

Raises:

  • (NoMethodError)

    The missing method did not map to a header in #headers.



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/spidr/page.rb', line 95

def method_missing(name,*arguments,&block)
  if (arguments.empty? && block.nil?)
    header_name = name.to_s.sub('_','-')

    if @response.key?(header_name)
      return @response[header_name]
    end
  end

  return super(name,*arguments,&block)
end

Instance Attribute Details

#headersObject (readonly)

Headers returned with the body



22
23
24
# File 'lib/spidr/page.rb', line 22

def headers
  @headers
end

#responseObject (readonly)

HTTP Response



19
20
21
# File 'lib/spidr/page.rb', line 19

def response
  @response
end

#urlObject (readonly)

URL of the page



16
17
18
# File 'lib/spidr/page.rb', line 16

def url
  @url
end

Instance Method Details

#is_redirect?Boolean Also known as: redirect?

Determines if the response code is 300, 301, 302, 303 or 307. Also checks for "soft" redirects added at the page level by a meta refresh tag.

Returns:

  • (Boolean)

    Specifies whether the response code is a HTTP Redirect code.



65
66
67
68
69
70
71
72
73
74
# File 'lib/spidr/page.rb', line 65

def is_redirect?
  case code
  when 300..303, 307
    true
  when 200
    meta_redirect?
  else
    false
  end
end

#meta_redirectArray<String>

Deprecated.

Deprecated in 0.3.0 and will be removed in 0.4.0. Use Links#meta_redirects instead.

The meta-redirect links of the page.

Returns:

  • (Array<String>)

    All meta-redirect links in the page.



50
51
52
53
54
55
# File 'lib/spidr/page.rb', line 50

def meta_redirect
  STDERR.puts 'DEPRECATION: Spidr::Page#meta_redirect will be removed in 0.3.0'
  STDERR.puts 'DEPRECATION: Use Spidr::Page#meta_redirects instead'

  meta_redirects
end