class UrlTracker::Page
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
Attributes
Public Class Methods
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
# File lib/url_tracker/page.rb, line 16 def initialize(uri, page_fetcher = Net::HTTP) @uri = uri.dup @page_fetcher = page_fetcher end
Public Instance Methods
# File lib/url_tracker/page.rb, line 53 def ==(other) @uri == other.uri end
Verifies if a page has changed since last the last time it was fetched
# 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
Returns a string containing the page content. If not yet fetched, this method will fetch the page for you.
# File lib/url_tracker/page.rb, line 23 def content @content ||= fetch end
This method returns a string containing the page content, but always fetches the page again
# File lib/url_tracker/page.rb, line 29 def content! @content = fetch end
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.
# File lib/url_tracker/page.rb, line 49 def eql?(other) @uri.eql?(other.uri) end
# File lib/url_tracker/page.rb, line 57 def hash @uri.hash end
Private Instance Methods
# File lib/url_tracker/page.rb, line 63 def fetch @page_fetcher.get(@uri) end