Permalink
Browse files

Renamed Journey to Trip. Journey is now a collection of Trips

  • Loading branch information...
chendo committed Jan 6, 2012
1 parent e6fd1e7 commit c200cda62e2689245130cf7e24249ccb552677d0
Showing with 110 additions and 98 deletions.
  1. +4 −79 lib/metlinkr.rb
  2. +70 −16 lib/metlinkr/journey.rb
  3. +33 −0 lib/metlinkr/trip.rb
  4. +3 −3 spec/{journey_spec.rb → trip_spec.rb}
View
@@ -1,89 +1,14 @@
-require 'mechanize'
-require 'nokogiri'
-
class Metlinkr
-
- START_URL = "http://jp.metlinkmelbourne.com.au/metlink/XSLT_TRIP_REQUEST2?language=en&itdLPxx_view=advanced"
- def self.instance
- @instance ||= new
- end
-
- def initialize
- agent.user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; chromeframe/12.0.742.112)'
- end
-
- def route(from, to, options = {:methods => :all, :ignore_earlier_journey => true, :limit => 1})
- page = agent.get(START_URL)
-
- f = page.form('tripRequest')
-
- # Shitty hack for forcing address
-
- f.anyObjFilter_origin = 29
- f.execIdentifiedLoc_origin = 1
- f.execStopList_origin = 0
-
- f.anyObjFilter_destination = 29
- f.execIdentifiedLoc_destination = 1
- f.execStopList_destination = 0
-
- f.name_origin = from
- f.name_destination = to
-
- select_methods(f, options[:methods])
-
- results = f.click_button
-
- body = results.body
-
- doc = Nokogiri::HTML(body)
-
- links = doc.search('tr.p4 td.dontprint a, tr.p2 td.dontprint a')
-
- links.shift if options[:ignore_earlier_journey]
-
- links = links.slice(0, options[:limit])
- links.map do |link|
- href = link.attributes['href'].value
- fetch_and_parse_journey_from_href(href)
+ def self.plan(from, to, options = nil)
+ Journey.new(from, to, options).tap do |journey|
+ journey.plan
end
end
-
- private
-
- def agent
- @agent ||= Mechanize.new
- end
-
- def fetch_and_parse_journey_from_href(href)
- Journey.parse(agent.get(href).body)
- end
-
- METHOD_MAPPING = {
- :train => 'inclMOT_1',
- :tram => 'inclMOT_4',
- :bus => 'inclMOT_5',
- :vline => 'inclMOT_0',
- :regional_bus => 'inclMOT_6',
- :skybus => 'inclMOT_3'
- }
-
- ALL_METHODS = [:tram, :train, :bus, :vline, :regional_bus, :skybus]
-
- def select_methods(form, methods = :all)
- if methods == :all
- methods = ALL_METHODS
- end
-
- (ALL_METHODS - [methods].flatten).each do |method|
- form.checkbox_with(:name => METHOD_MAPPING[method]).uncheck
- end
-
- end
end
$LOAD_PATH << File.dirname(__FILE__)
require 'metlinkr/version'
require 'metlinkr/journey'
+require 'metlinkr/trip'
require 'metlinkr/step'
View
@@ -1,33 +1,87 @@
+require 'mechanize'
require 'nokogiri'
+
class Metlinkr
+
class Journey
- attr_accessor :steps
- def self.parse(html)
- journey = new
+ START_URL = "http://jp.metlinkmelbourne.com.au/metlink/XSLT_TRIP_REQUEST2?language=en&itdLPxx_view=advanced"
+
+ attr_reader :from, :to, :options, :trips
+
+ def initialize(from, to, options = {:methods => :all, :ignore_earlier_trip => true, :limit => 1})
+ @from = from
+ @to = to
+ @options = options
+ end
+
+ def plan
+
+ agent = Mechanize.new
+ agent.user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; chromeframe/12.0.742.112)'
+
+ page = agent.get(START_URL)
+
+ f = page.form('tripRequest')
+
+ # Shitty hack for forcing address
+
+ f.anyObjFilter_origin = 29
+ f.execIdentifiedLoc_origin = 1
+ f.execStopList_origin = 0
- doc = Nokogiri::HTML(html)
+ f.anyObjFilter_destination = 29
+ f.execIdentifiedLoc_destination = 1
+ f.execStopList_destination = 0
- rows = doc.xpath("//table[@text-align='top']/tr")
+ f.name_origin = from
+ f.name_destination = to
- rows = rows.to_a.reject do |row|
- # Reject the hidden ones
- klass = row.attributes['class'].value rescue ""
- klass =~ /addinfo|jpText/
+ select_methods(f, options[:methods])
+
+ results = f.click_button
+
+ body = results.body
+
+ doc = Nokogiri::HTML(body)
+
+ links = doc.search('tr.p4 td.dontprint a, tr.p2 td.dontprint a')
+
+ links.shift if options[:ignore_earlier_trip]
+
+ links = links.slice(0, options[:limit])
+ @trips = links.map do |link|
+ href = link.attributes['href'].value
+ fetch_and_parse_trip_from_href(agent, href)
end
+ end
+
+ private
+
+ def fetch_and_parse_trip_from_href(agent, href)
+ Trip.parse(agent.get(href).body)
+ end
+
+ METHOD_MAPPING = {
+ :train => 'inclMOT_1',
+ :tram => 'inclMOT_4',
+ :bus => 'inclMOT_5',
+ :vline => 'inclMOT_0',
+ :regional_bus => 'inclMOT_6',
+ :skybus => 'inclMOT_3'
+ }
- rows.shift # Get rid of header row
+ ALL_METHODS = [:tram, :train, :bus, :vline, :regional_bus, :skybus]
- if rows.length % 3 != 0
- raise "Rows not a multiple of 3"
+ def select_methods(form, methods = :all)
+ if methods == :all
+ methods = ALL_METHODS
end
- journey.steps = []
- rows.each_slice(3) do |row_set|
- journey.steps << Step.parse(row_set)
+ (ALL_METHODS - [methods].flatten).each do |method|
+ form.checkbox_with(:name => METHOD_MAPPING[method]).uncheck
end
- journey
end
end
end
View
@@ -0,0 +1,33 @@
+require 'nokogiri'
+class Metlinkr
+ class Trip
+ attr_accessor :steps
+
+ def self.parse(html)
+ trip = new
+
+ doc = Nokogiri::HTML(html)
+
+ rows = doc.xpath("//table[@text-align='top']/tr")
+
+ rows = rows.to_a.reject do |row|
+ # Reject the hidden ones
+ klass = row.attributes['class'].value rescue ""
+ klass =~ /addinfo|jpText/
+ end
+
+ rows.shift # Get rid of header row
+
+ if rows.length % 3 != 0
+ raise "Rows not a multiple of 3"
+ end
+
+ trip.steps = []
+ rows.each_slice(3) do |row_set|
+ trip.steps << Step.parse(row_set)
+ end
+
+ trip
+ end
+ end
+end
@@ -1,15 +1,15 @@
require 'spec_helper'
-describe Metlinkr::Journey do
+describe Metlinkr::Trip do
let(:raw_shitty_html) do
File.read(File.dirname(__FILE__) + "/fixtures/multiple_journey.html")
end
subject do
- Metlinkr::Journey.parse(raw_shitty_html)
+ Metlinkr::Trip.parse(raw_shitty_html)
end
- it "parses journey effectively" do
+ it "parses Trip effectively" do
subject.steps.length.should == 7
step = subject.steps.first

0 comments on commit c200cda

Please sign in to comment.