Class: Metlinkr::Journey

Inherits:
Object
  • Object
show all
Defined in:
lib/metlinkr/journey.rb

Constant Summary collapse

START_URL =
"http://jp.metlinkmelbourne.com.au/metlink/XSLT_TRIP_REQUEST2?language=en&itdLPxx_view=advanced"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, options = {:methods => :all, :ignore_earlier_trip => true, :limit => 1}) ⇒ Journey

Returns a new instance of Journey.



12
13
14
15
16
# File 'lib/metlinkr/journey.rb', line 12

def initialize(from, to, options = {:methods => :all, :ignore_earlier_trip => true, :limit => 1})
  @from = from
  @to = to
  @options = options
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



10
11
12
# File 'lib/metlinkr/journey.rb', line 10

def from
  @from
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/metlinkr/journey.rb', line 10

def options
  @options
end

#toObject (readonly)

Returns the value of attribute to.



10
11
12
# File 'lib/metlinkr/journey.rb', line 10

def to
  @to
end

#tripsObject (readonly)

Returns the value of attribute trips.



10
11
12
# File 'lib/metlinkr/journey.rb', line 10

def trips
  @trips
end

Instance Method Details

#planObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metlinkr/journey.rb', line 18

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

  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_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