Skip to content

Instantly share code, notes, and snippets.

@StephenFiser
Created December 2, 2015 17:43
Show Gist options
  • Save StephenFiser/62e10972a3b8003c1bcf to your computer and use it in GitHub Desktop.
Save StephenFiser/62e10972a3b8003c1bcf to your computer and use it in GitHub Desktop.
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