Created
October 16, 2009 22:13
-
-
Save marcus/212108 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
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