Last active
May 6, 2016 16:08
-
-
Save j0ni/70a889a73d1ac0d644475e4d86f13ee3 to your computer and use it in GitHub Desktop.
This file contains 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
require 'mechanize' | |
class BaconSeeker | |
def queue; @queue ||= Queue.new; end | |
def visited; @visited ||= []; end | |
def agent; @agent ||= Mechanize.new; end | |
attr_reader :target | |
def initialize(url1, url2) | |
@target = url2 | |
queue.push([url1, 1]) | |
end | |
def parse(page) | |
agent | |
.get("http://en.wikipedia.org#{page}") | |
.links | |
.map(&:href) | |
.select { |link| /^\/wiki\//.match(link) } | |
.reject { |link| link.include?(':') } | |
.map { |link| link.split(/#/).first } | |
.uniq | |
end | |
def search | |
new_links = [] | |
depth = nil | |
until new_links.include?(target) | |
url, depth = queue.pop | |
puts "Processing #{url}, depth: #{depth}" | |
unless visited.include?(url) | |
self.visited << url | |
new_links = parse(url) | |
new_links.each do |link| | |
self.queue << [link, depth + 1] | |
end | |
end | |
end | |
depth | |
end | |
end | |
puts BaconSeeker.new("/wiki/Ruby", "/wiki/David_Bowie").search |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment