Class: UrlTracker::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/url_tracker/page.rb

Overview

Class representing a single web page to be tracked. It is capable of fetching the page’s content and verifying if it was changed since last time it was fetched

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, page_fetcher = Net::HTTP) ⇒ Page

Creates a new instance of UrlTracker::Page. The first argument is the URI that corresponds to the page and will be lazily fetched. The second parameter is an object that is responsible for fetching the page itself. It mus respond to the get method with the given uri parameter and return a string with the page contents; this parameter defaults to Net::HTTP, so you should by default pass uri as an instance of URI::Generic



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

def initialize(uri, page_fetcher = Net::HTTP)
  @uri          = uri.dup
  @page_fetcher = page_fetcher
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



8
9
10
# File 'lib/url_tracker/page.rb', line 8

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
# File 'lib/url_tracker/page.rb', line 53

def ==(other)
  @uri == other.uri
end

#changed?Boolean

Verifies if a page has changed since last the last time it was fetched

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/url_tracker/page.rb', line 34

def changed?
  if @content # we have a cached copy
    old_content = @content
    @content = fetch
    @content != old_content
  else
    @content = fetch
    false
  end
end

#contentObject

Returns a string containing the page content. If not yet fetched, this method will fetch the page for you.



23
24
25
# File 'lib/url_tracker/page.rb', line 23

def content
  @content ||= fetch
end

#content!Object

This method returns a string containing the page content, but always fetches the page again



29
30
31
# File 'lib/url_tracker/page.rb', line 29

def content!
  @content = fetch
end

#eql?(other) ⇒ Boolean

Two pages are considered the same if they have the same URI. Right, that might not be true if the content is different (which shouldn’t if you are building a RESTful service), but we will just ignore that and pretend we we live in a better world.

Returns:

  • (Boolean)


49
50
51
# File 'lib/url_tracker/page.rb', line 49

def eql?(other)
  @uri.eql?(other.uri)
end

#hashObject



57
58
59
# File 'lib/url_tracker/page.rb', line 57

def hash
  @uri.hash
end