Skip to content

Instantly share code, notes, and snippets.

@envp
Created December 24, 2013 20:12
Show Gist options
  • Select an option

  • Save envp/8117414 to your computer and use it in GitHub Desktop.

Select an option

Save envp/8117414 to your computer and use it in GitHub Desktop.
What it's supposed to look like
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'net/http'
class NokoLinkSet < Nokogiri::XML::NodeSet
# Create new LinkSet object from the url and the selector
def initialize(url, selector)
if not ( url.nil? and selector.nil? )
@raw_html = open(url)
@elements = Nokogiri::HTML(@raw_html).css(selector)
else
raise ArgumentException, 'Need to supply URL and Selector to initialize LinkSet Object'
end
end
# Validate and clean those elements that do not correspond to given subject
# Tutorialspoint links that pertain to the subject contain the subject name
# Eg: www.tutorialspoint.com/java/sometut.htm
def clean(subject)
# Filter out anything that is not course material
@elements = @elements.map{ |@element| @element if @element['href'].include?(subject) }
# Get rid of nils at the end of the Array
while @elements[-1].nil?
@elements.pop
end
end
# Get the count of relevant links
@link_count = @elements.length
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment