class Timetabler
Public Class Methods
new(parser=nil)
click to toggle source
# File lib/timetabler.rb, line 5 def initialize(parser=nil) @lines = [] @routes = [] @stops = [] @times = [] @parser = parser if !@parser.nil? use_parser @parser end end
Public Instance Methods
fetch(parser=nil)
click to toggle source
# File lib/timetabler.rb, line 22 def fetch(parser=nil) if @parser.nil? and parser.nil? raise "Parser not specified" elsif @parser.nil? and !parser.nil? use_parser parser end fetch_all end
fetch_all()
click to toggle source
# File lib/parsers/mzk-tychy/parser.rb, line 7 def fetch_all {:lines => fetch_lines, :routes => fetch_routes, :stops => fetch_stops} end
fetch_lines()
click to toggle source
# File lib/parsers/mzk-tychy/parser.rb, line 11 def fetch_lines line_list = Nokogiri::HTML(open('http://www.mzk.pl/rozklady/?co=lista_linii')) lines = line_list.css('ul.linie_typu a').select{|l| l.content.length <= 5} @lines = lines.map{ |link| l = Timetabler::Line.new l[:name] = link.content.gsub(/\*/, "") l[:href] = link[:href] l } end
fetch_routes()
click to toggle source
# File lib/parsers/mzk-tychy/parser.rb, line 25 def fetch_routes if @lines.nil? fetch_lines end @lines.each_index do |id| link = @lines[id] line = Nokogiri::HTML(open('http://www.mzk.pl/rozklady/'+link[:href])) line.css("div#trasy div.trasa").each do |route| r = Timetabler::Route.new r[:line_id] = id; r[:name] = route.css("h4 span").first.content @routes << r @route_nodes << route end end return @routes end
fetch_stops()
click to toggle source
# File lib/parsers/mzk-tychy/parser.rb, line 47 def fetch_stops if @routes.nil? or @route_nodes.nil? fetch_routes end @route_nodes.each_index do |id| route = @route_nodes[id] route.css("ul li a").map{|stop| s = Timetabler::Stop.new s[:line_id] = @routes[id][:line_id] s[:route_id] = id s[:name] = stop.content.strip s[:href] = stop[:href] @stops << s } end return @stops end
parsers()
click to toggle source
# File lib/timetabler.rb, line 18 def parsers Dir.entries(File.join(File.dirname(__FILE__), "parsers")).select{|f| !["..", "."].include?(f)} end
subinitialize()
click to toggle source
# File lib/parsers/mzk-tychy/parser.rb, line 2 def subinitialize # Used for temporary route storage since we would have to load the same page twice to get the times and routes @route_nodes = [] end
use_parser(parser)
click to toggle source
# File lib/timetabler.rb, line 32 def use_parser(parser) if parsers.include? parser require File.join(File.dirname(__FILE__), "parsers", parser, "parser") subinitialize else raise "Unknown parser: " + parser.to_s end return self end