Created
December 2, 2015 17:43
-
-
Save StephenFiser/62e10972a3b8003c1bcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
class Webpage | |
attr_reader :address | |
def initialize(address) | |
@address = address | |
end | |
def headline | |
unless page_content.index("<h1").nil? | |
p page_content[start..finish] | |
else | |
p "There were no h1 tags on #{address}." | |
end | |
end | |
def start | |
# <h1 class='some-class' id='some-id'> | |
OpeningTag.new("h1", page_content).finish + 1 | |
end | |
def finish | |
page_content.index("</h1>") - 1 | |
end | |
def page_content | |
@content ||= open(address).read | |
end | |
end | |
class OpeningTag | |
attr_reader :name, :context | |
def initialize(name, context) | |
@name = name | |
@context = context | |
end | |
def start | |
context.index("<#{name}") | |
end | |
def finish | |
context.index(">", start) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment