Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Routing for reals works!

  • Loading branch information...
commit 9fdd52154031384b646a0ea3719bf0092d4b9217 1 parent d143b4a
@chendo authored
Showing with 62 additions and 3 deletions.
  1. +56 −3 lib/metlinkr.rb
  2. +6 −0 lib/metlinkr/journey.rb
View
59 lib/metlinkr.rb
@@ -1,5 +1,58 @@
-require "metlinkr/version"
+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
+
+ end
+
+ def route(from, to, options = {})
+ 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)
+
+ require 'pp'
+ 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
+ results = f.click_button
+
+ body = results.body
+
+ match = body.match(%r|<tr class="p4">(.*?)</tr>|)
+
+ doc = Nokogiri::HTML(body)
+
+ link = doc.search('tr.p4 td.dontprint a').first
+
+ href = link.attributes['href'].value
+
+ body = agent.get(href).body
+
+ Journey.parse(body)
+ end
-module Metlinkr
- # Your code goes here...
end
+
+$LOAD_PATH << File.dirname(__FILE__)
+require 'metlinkr/version'
+require 'metlinkr/journey'
+require 'metlinkr/step'
+
View
6 lib/metlinkr/journey.rb
@@ -10,6 +10,12 @@ def self.parse(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
Please sign in to comment.
Something went wrong with that request. Please try again.