Skip to content

Instantly share code, notes, and snippets.

@ryanhburbank
Created July 6, 2013 00:23
Show Gist options
  • Save ryanhburbank/5938005 to your computer and use it in GitHub Desktop.
Save ryanhburbank/5938005 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'nokogiri'
require 'uri'
require_relative 'util'
class Browser
def run!
get_url
end
def get_url
URI.parse(gets.chomp)
end
# Run the browser
# Display a prompt for a user
# Parse their input
# Display useful results by instantiating
# a new Page and calling methods on it.
# Questions:
# 1. How can a user quit the browser gracefully?
# 2. How can a user ask for help, i.e., how do they know what commands are available to them?
end
end
Browser.new.run!
class Page
def initialize(url)
@url = url
@page = fetch!
@page_as_object= nokogiri_parser
end
def fetch!
Net::HTTP.get(URI.parse(@url))
end
def nokogiri_parser
Nokogiri::HTML(@page)
end
def title
@page_as_object.search('head title').text
end
def links
@page_as_object.search('a').map{|link| link['href'].match(/https?:.+/)}.compact.map{|link| link[0]}
# Research alert!
# How do you use Nokogiri to extract all the link URLs on a page?
#
# These should only be URLs that look like
# <a href="http://somesite.com/page.html">Click here!</a>
# This would pull out "http://somesite.com/page.html"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment