Skip to content

Instantly share code, notes, and snippets.

@marcus
Created October 16, 2009 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcus/212108 to your computer and use it in GitHub Desktop.
Save marcus/212108 to your computer and use it in GitHub Desktop.
module Web
require 'rubygems'
require 'open-uri'
require 'nokogiri'
# @render_options :fields=>{:default=>[:title, :href],
# :values=>[:title, :link]}
# @options :external=>false
# Displays the links from a URL
def links_in(url, options={})
links = []
Nokogiri::HTML(open(url)).css('a').each do |link|
href = link.attributes["href"].to_s
add = true
if (options[:external]==true)
add = false if !is_external_link?(href)
end
links.push({:href => href, :title => link.content}) if add
end
links
end
private
def is_external_link?(url)
url[0..3] == 'http' ? true : false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment