Skip to content

Instantly share code, notes, and snippets.

@jbonhag
Created June 29, 2014 07:52
Show Gist options
  • Save jbonhag/1af64638148623e0f90f to your computer and use it in GitHub Desktop.
Save jbonhag/1af64638148623e0f90f to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'open-uri'
require_relative 'lib/thing'
pattern = ARGV[0]
urls = ARGV[1..-1]
thing = Thing.new
urls.each do |url|
doc = Nokogiri::HTML(open(url))
doc.css("a").each do |link|
if thing.match pattern, link
puts link["href"]
end
end
end
class Thing
def match pattern, link
begin
link["href"].include? pattern
rescue NoMethodError
false
end
end
end
require_relative '../lib/thing.rb'
describe Thing do
describe 'match' do
it 'matches if the href matches' do
link = { "href" => "/goose/gander" }
thing = Thing.new
expect(thing.match("goose", link)).to be(true)
end
it 'does not match if the anchor has no href' do
link = {}
thing = Thing.new
expect(thing.match("banana", link)).to be(false)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment