Class: Anemone::Page
- Inherits:
-
Object
- Object
- Anemone::Page
- Defined in:
- lib/anemone/page.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
The raw HTTP response body of the page.
-
#code ⇒ Object
Integer response code of the page.
-
#data ⇒ Object
OpenStruct for user-stored data.
-
#depth ⇒ Object
Depth of this page from the root of the crawl.
-
#error ⇒ Object
readonly
Exception object, if one was raised during HTTP#fetch_page.
-
#headers ⇒ Object
readonly
Headers of the HTTP response.
-
#redirect_to ⇒ Object
readonly
URL of the page this one redirected to, if any.
-
#referer ⇒ Object
URL of the page that brought us to this page.
-
#response_time ⇒ Object
Response time of the request for this page in milliseconds.
-
#url ⇒ Object
readonly
The URL of the page.
-
#visited ⇒ Object
Boolean indicating whether or not this page has been visited in PageStore#shortest_paths!.
Class Method Summary collapse
Instance Method Summary collapse
-
#base ⇒ Object
Base URI from the HTML doc head element www.w3.org/TR/html4/struct/links.html#edef-BASE.
-
#content_type ⇒ Object
The content-type returned by the HTTP request for this page.
-
#cookies ⇒ Object
Array of cookies received with this page as WEBrick::Cookie objects.
-
#discard_doc! ⇒ Object
Delete the Nokogiri document and response body to conserve memory.
-
#doc ⇒ Object
Nokogiri document for the HTML body.
-
#fetched? ⇒ Boolean
Was the page successfully fetched?
true
if the page was fetched with no error,false
otherwise. -
#html? ⇒ Boolean
Returns
true
if the page is a HTML document, returnsfalse
otherwise. -
#in_domain?(uri) ⇒ Boolean
Returns
true
if uri is in the same domain as the page, returnsfalse
otherwise. -
#initialize(url, params = {}) ⇒ Page
constructor
Create a new page.
-
#links ⇒ Object
Array of distinct A tag HREFs from the page.
- #marshal_dump ⇒ Object
- #marshal_load(ary) ⇒ Object
-
#not_found? ⇒ Boolean
Returns
true
if the page was not found (returned 404 code), returnsfalse
otherwise. -
#redirect? ⇒ Boolean
Returns
true
if the page is a HTTP redirect, returnsfalse
otherwise. -
#to_absolute(link) ⇒ Object
Converts relative URL link into an absolute URL based on the location of the page.
- #to_hash ⇒ Object
Constructor Details
#initialize(url, params = {}) ⇒ Page
Create a new page
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/anemone/page.rb', line 36 def initialize(url, params = {}) @url = url @data = OpenStruct.new @code = params[:code] @headers = params[:headers] || {} @headers['content-type'] ||= [''] @aliases = Array(params[:aka]).compact @referer = params[:referer] @depth = params[:depth] || 0 @redirect_to = to_absolute(params[:redirect_to]) @response_time = params[:response_time] @body = params[:body] @error = params[:error] @fetched = !params[:code].nil? end |
Instance Attribute Details
#body ⇒ Object (readonly)
The raw HTTP response body of the page
11 12 13 |
# File 'lib/anemone/page.rb', line 11 def body @body end |
#code ⇒ Object
Integer response code of the page
22 23 24 |
# File 'lib/anemone/page.rb', line 22 def code @code end |
#data ⇒ Object
OpenStruct for user-stored data
20 21 22 |
# File 'lib/anemone/page.rb', line 20 def data @data end |
#depth ⇒ Object
Depth of this page from the root of the crawl. This is not necessarily the shortest path; use PageStore#shortest_paths! to find that value.
27 28 29 |
# File 'lib/anemone/page.rb', line 27 def depth @depth end |
#error ⇒ Object (readonly)
Exception object, if one was raised during HTTP#fetch_page
17 18 19 |
# File 'lib/anemone/page.rb', line 17 def error @error end |
#headers ⇒ Object (readonly)
Headers of the HTTP response
13 14 15 |
# File 'lib/anemone/page.rb', line 13 def headers @headers end |
#redirect_to ⇒ Object (readonly)
URL of the page this one redirected to, if any
15 16 17 |
# File 'lib/anemone/page.rb', line 15 def redirect_to @redirect_to end |
#referer ⇒ Object
URL of the page that brought us to this page
29 30 31 |
# File 'lib/anemone/page.rb', line 29 def referer @referer end |
#response_time ⇒ Object
Response time of the request for this page in milliseconds
31 32 33 |
# File 'lib/anemone/page.rb', line 31 def response_time @response_time end |
#url ⇒ Object (readonly)
The URL of the page
9 10 11 |
# File 'lib/anemone/page.rb', line 9 def url @url end |
#visited ⇒ Object
Boolean indicating whether or not this page has been visited in PageStore#shortest_paths!
24 25 26 |
# File 'lib/anemone/page.rb', line 24 def visited @visited end |
Class Method Details
.from_hash(hash) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/anemone/page.rb', line 198 def self.from_hash(hash) page = self.new(URI(hash['url'])) {'@headers' => Marshal.load(hash['headers']), '@data' => Marshal.load(hash['data']), '@body' => hash['body'], '@links' => hash['links'].map { |link| URI(link) }, '@code' => hash['code'].to_i, '@visited' => hash['visited'], '@depth' => hash['depth'].to_i, '@referer' => hash['referer'], '@redirect_to' => (!!hash['redirect_to'] && !hash['redirect_to'].empty?) ? URI(hash['redirect_to']) : nil, '@response_time' => hash['response_time'].to_i, '@fetched' => hash['fetched'] }.each do |var, value| page.instance_variable_set(var, value) end page end |
Instance Method Details
#base ⇒ Object
Base URI from the HTML doc head element www.w3.org/TR/html4/struct/links.html#edef-BASE
138 139 140 141 142 143 144 145 146 |
# File 'lib/anemone/page.rb', line 138 def base @base = if doc href = doc.search('//head/base/@href') URI(href.to_s) unless href.nil? rescue nil end unless @base return nil if @base && @base.to_s().empty? @base end |
#content_type ⇒ Object
The content-type returned by the HTTP request for this page
106 107 108 |
# File 'lib/anemone/page.rb', line 106 def content_type headers['content-type'].first end |
#cookies ⇒ Object
Array of cookies received with this page as WEBrick::Cookie objects.
99 100 101 |
# File 'lib/anemone/page.rb', line 99 def WEBrick::Cookie.(@headers['Set-Cookie']) rescue [] end |
#discard_doc! ⇒ Object
Delete the Nokogiri document and response body to conserve memory
83 84 85 86 |
# File 'lib/anemone/page.rb', line 83 def discard_doc! links # force parsing of page links before we trash the document @doc = @body = nil end |
#doc ⇒ Object
Nokogiri document for the HTML body
75 76 77 78 |
# File 'lib/anemone/page.rb', line 75 def doc return @doc if @doc @doc = Nokogiri::HTML(@body) if @body && html? rescue nil end |
#fetched? ⇒ Boolean
Was the page successfully fetched? true
if the page was fetched with no error, false
otherwise.
92 93 94 |
# File 'lib/anemone/page.rb', line 92 def fetched? @fetched end |
#html? ⇒ Boolean
Returns true
if the page is a HTML document, returns false
otherwise.
114 115 116 |
# File 'lib/anemone/page.rb', line 114 def html? !!(content_type =~ %r{^(text/html|application/xhtml+xml)\b}) end |
#in_domain?(uri) ⇒ Boolean
Returns true
if uri is in the same domain as the page, returns false
otherwise
171 172 173 |
# File 'lib/anemone/page.rb', line 171 def in_domain?(uri) uri.host == @url.host end |
#links ⇒ Object
Array of distinct A tag HREFs from the page
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/anemone/page.rb', line 57 def links return @links unless @links.nil? @links = [] return @links if !doc doc.search("//a[@href]").each do |a| u = a['href'] next if u.nil? or u.empty? abs = to_absolute(URI(URI.escape(u))) rescue next @links << abs if in_domain?(abs) end @links.uniq! @links end |
#marshal_dump ⇒ Object
175 176 177 |
# File 'lib/anemone/page.rb', line 175 def marshal_dump [@url, @headers, @data, @body, @links, @code, @visited, @depth, @referer, @redirect_to, @response_time, @fetched] end |
#marshal_load(ary) ⇒ Object
179 180 181 |
# File 'lib/anemone/page.rb', line 179 def marshal_load(ary) @url, @headers, @data, @body, @links, @code, @visited, @depth, @referer, @redirect_to, @response_time, @fetched = ary end |
#not_found? ⇒ Boolean
Returns true
if the page was not found (returned 404 code), returns false
otherwise.
130 131 132 |
# File 'lib/anemone/page.rb', line 130 def not_found? 404 == @code end |
#redirect? ⇒ Boolean
Returns true
if the page is a HTTP redirect, returns false
otherwise.
122 123 124 |
# File 'lib/anemone/page.rb', line 122 def redirect? (300..307).include?(@code) end |
#to_absolute(link) ⇒ Object
Converts relative URL link into an absolute URL based on the location of the page
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/anemone/page.rb', line 153 def to_absolute(link) return nil if link.nil? # remove anchor link = URI.encode(URI.decode(link.to_s.gsub(/#[a-zA-Z0-9_-]*$/,''))) relative = URI(link) absolute = base ? base.merge(relative) : @url.merge(relative) absolute.path = '/' if absolute.path.empty? return absolute end |
#to_hash ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/anemone/page.rb', line 183 def to_hash {'url' => @url.to_s, 'headers' => Marshal.dump(@headers), 'data' => Marshal.dump(@data), 'body' => @body, 'links' => links.map(&:to_s), 'code' => @code, 'visited' => @visited, 'depth' => @depth, 'referer' => @referer.to_s, 'redirect_to' => @redirect_to.to_s, 'response_time' => @response_time, 'fetched' => @fetched} end |