metlinkr 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/metlinkr.rb +4 -79
- data/lib/metlinkr/journey.rb +70 -16
- data/lib/metlinkr/trip.rb +33 -0
- data/lib/metlinkr/version.rb +1 -1
- data/spec/{journey_spec.rb → trip_spec.rb} +3 -3
- metadata +10 -9
data/lib/metlinkr.rb
CHANGED
@@ -1,89 +1,14 @@
|
|
1
|
-
require 'mechanize'
|
2
|
-
require 'nokogiri'
|
3
|
-
|
4
1
|
class Metlinkr
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@instance ||= new
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
agent.user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; chromeframe/12.0.742.112)'
|
13
|
-
end
|
14
|
-
|
15
|
-
def route(from, to, options = {:methods => :all, :ignore_earlier_journey => true, :limit => 1})
|
16
|
-
page = agent.get(START_URL)
|
17
|
-
|
18
|
-
f = page.form('tripRequest')
|
19
|
-
|
20
|
-
# Shitty hack for forcing address
|
21
|
-
|
22
|
-
f.anyObjFilter_origin = 29
|
23
|
-
f.execIdentifiedLoc_origin = 1
|
24
|
-
f.execStopList_origin = 0
|
25
|
-
|
26
|
-
f.anyObjFilter_destination = 29
|
27
|
-
f.execIdentifiedLoc_destination = 1
|
28
|
-
f.execStopList_destination = 0
|
29
|
-
|
30
|
-
f.name_origin = from
|
31
|
-
f.name_destination = to
|
32
|
-
|
33
|
-
select_methods(f, options[:methods])
|
34
|
-
|
35
|
-
results = f.click_button
|
36
|
-
|
37
|
-
body = results.body
|
38
|
-
|
39
|
-
doc = Nokogiri::HTML(body)
|
40
|
-
|
41
|
-
links = doc.search('tr.p4 td.dontprint a, tr.p2 td.dontprint a')
|
42
|
-
|
43
|
-
links.shift if options[:ignore_earlier_journey]
|
44
|
-
|
45
|
-
links = links.slice(0, options[:limit])
|
46
|
-
links.map do |link|
|
47
|
-
href = link.attributes['href'].value
|
48
|
-
fetch_and_parse_journey_from_href(href)
|
2
|
+
def self.plan(from, to, options = nil)
|
3
|
+
Journey.new(from, to, options).tap do |journey|
|
4
|
+
journey.plan
|
49
5
|
end
|
50
6
|
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def agent
|
55
|
-
@agent ||= Mechanize.new
|
56
|
-
end
|
57
|
-
|
58
|
-
def fetch_and_parse_journey_from_href(href)
|
59
|
-
Journey.parse(agent.get(href).body)
|
60
|
-
end
|
61
|
-
|
62
|
-
METHOD_MAPPING = {
|
63
|
-
:train => 'inclMOT_1',
|
64
|
-
:tram => 'inclMOT_4',
|
65
|
-
:bus => 'inclMOT_5',
|
66
|
-
:vline => 'inclMOT_0',
|
67
|
-
:regional_bus => 'inclMOT_6',
|
68
|
-
:skybus => 'inclMOT_3'
|
69
|
-
}
|
70
|
-
|
71
|
-
ALL_METHODS = [:tram, :train, :bus, :vline, :regional_bus, :skybus]
|
72
|
-
|
73
|
-
def select_methods(form, methods = :all)
|
74
|
-
if methods == :all
|
75
|
-
methods = ALL_METHODS
|
76
|
-
end
|
77
|
-
|
78
|
-
(ALL_METHODS - [methods].flatten).each do |method|
|
79
|
-
form.checkbox_with(:name => METHOD_MAPPING[method]).uncheck
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
7
|
end
|
84
8
|
|
85
9
|
$LOAD_PATH << File.dirname(__FILE__)
|
86
10
|
require 'metlinkr/version'
|
87
11
|
require 'metlinkr/journey'
|
12
|
+
require 'metlinkr/trip'
|
88
13
|
require 'metlinkr/step'
|
89
14
|
|
data/lib/metlinkr/journey.rb
CHANGED
@@ -1,33 +1,87 @@
|
|
1
|
+
require 'mechanize'
|
1
2
|
require 'nokogiri'
|
3
|
+
|
2
4
|
class Metlinkr
|
5
|
+
|
3
6
|
class Journey
|
4
|
-
attr_accessor :steps
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
+
START_URL = "http://jp.metlinkmelbourne.com.au/metlink/XSLT_TRIP_REQUEST2?language=en&itdLPxx_view=advanced"
|
9
|
+
|
10
|
+
attr_reader :from, :to, :options, :trips
|
11
|
+
|
12
|
+
def initialize(from, to, options = {:methods => :all, :ignore_earlier_trip => true, :limit => 1})
|
13
|
+
@from = from
|
14
|
+
@to = to
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
def plan
|
19
|
+
|
20
|
+
agent = Mechanize.new
|
21
|
+
agent.user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; chromeframe/12.0.742.112)'
|
22
|
+
|
23
|
+
page = agent.get(START_URL)
|
24
|
+
|
25
|
+
f = page.form('tripRequest')
|
26
|
+
|
27
|
+
# Shitty hack for forcing address
|
28
|
+
|
29
|
+
f.anyObjFilter_origin = 29
|
30
|
+
f.execIdentifiedLoc_origin = 1
|
31
|
+
f.execStopList_origin = 0
|
8
32
|
|
9
|
-
|
33
|
+
f.anyObjFilter_destination = 29
|
34
|
+
f.execIdentifiedLoc_destination = 1
|
35
|
+
f.execStopList_destination = 0
|
10
36
|
|
11
|
-
|
37
|
+
f.name_origin = from
|
38
|
+
f.name_destination = to
|
12
39
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
40
|
+
select_methods(f, options[:methods])
|
41
|
+
|
42
|
+
results = f.click_button
|
43
|
+
|
44
|
+
body = results.body
|
45
|
+
|
46
|
+
doc = Nokogiri::HTML(body)
|
47
|
+
|
48
|
+
links = doc.search('tr.p4 td.dontprint a, tr.p2 td.dontprint a')
|
49
|
+
|
50
|
+
links.shift if options[:ignore_earlier_trip]
|
51
|
+
|
52
|
+
links = links.slice(0, options[:limit])
|
53
|
+
@trips = links.map do |link|
|
54
|
+
href = link.attributes['href'].value
|
55
|
+
fetch_and_parse_trip_from_href(agent, href)
|
17
56
|
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def fetch_and_parse_trip_from_href(agent, href)
|
62
|
+
Trip.parse(agent.get(href).body)
|
63
|
+
end
|
64
|
+
|
65
|
+
METHOD_MAPPING = {
|
66
|
+
:train => 'inclMOT_1',
|
67
|
+
:tram => 'inclMOT_4',
|
68
|
+
:bus => 'inclMOT_5',
|
69
|
+
:vline => 'inclMOT_0',
|
70
|
+
:regional_bus => 'inclMOT_6',
|
71
|
+
:skybus => 'inclMOT_3'
|
72
|
+
}
|
18
73
|
|
19
|
-
|
74
|
+
ALL_METHODS = [:tram, :train, :bus, :vline, :regional_bus, :skybus]
|
20
75
|
|
21
|
-
|
22
|
-
|
76
|
+
def select_methods(form, methods = :all)
|
77
|
+
if methods == :all
|
78
|
+
methods = ALL_METHODS
|
23
79
|
end
|
24
80
|
|
25
|
-
|
26
|
-
|
27
|
-
journey.steps << Step.parse(row_set)
|
81
|
+
(ALL_METHODS - [methods].flatten).each do |method|
|
82
|
+
form.checkbox_with(:name => METHOD_MAPPING[method]).uncheck
|
28
83
|
end
|
29
84
|
|
30
|
-
journey
|
31
85
|
end
|
32
86
|
end
|
33
87
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
class Metlinkr
|
3
|
+
class Trip
|
4
|
+
attr_accessor :steps
|
5
|
+
|
6
|
+
def self.parse(html)
|
7
|
+
trip = new
|
8
|
+
|
9
|
+
doc = Nokogiri::HTML(html)
|
10
|
+
|
11
|
+
rows = doc.xpath("//table[@text-align='top']/tr")
|
12
|
+
|
13
|
+
rows = rows.to_a.reject do |row|
|
14
|
+
# Reject the hidden ones
|
15
|
+
klass = row.attributes['class'].value rescue ""
|
16
|
+
klass =~ /addinfo|jpText/
|
17
|
+
end
|
18
|
+
|
19
|
+
rows.shift # Get rid of header row
|
20
|
+
|
21
|
+
if rows.length % 3 != 0
|
22
|
+
raise "Rows not a multiple of 3"
|
23
|
+
end
|
24
|
+
|
25
|
+
trip.steps = []
|
26
|
+
rows.each_slice(3) do |row_set|
|
27
|
+
trip.steps << Step.parse(row_set)
|
28
|
+
end
|
29
|
+
|
30
|
+
trip
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/metlinkr/version.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Metlinkr::
|
3
|
+
describe Metlinkr::Trip do
|
4
4
|
let(:raw_shitty_html) do
|
5
5
|
File.read(File.dirname(__FILE__) + "/fixtures/multiple_journey.html")
|
6
6
|
end
|
7
7
|
|
8
8
|
subject do
|
9
|
-
Metlinkr::
|
9
|
+
Metlinkr::Trip.parse(raw_shitty_html)
|
10
10
|
end
|
11
11
|
|
12
|
-
it "parses
|
12
|
+
it "parses Trip effectively" do
|
13
13
|
subject.steps.length.should == 7
|
14
14
|
|
15
15
|
step = subject.steps.first
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metlinkr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-01-06 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70327901643460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70327901643460
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: capybara-mechanize
|
27
|
-
requirement: &
|
27
|
+
requirement: &70327901643040 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70327901643040
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: nokogiri
|
38
|
-
requirement: &
|
38
|
+
requirement: &70327901642620 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70327901642620
|
47
47
|
description: A gem to operate the Metlink journey planner
|
48
48
|
email: []
|
49
49
|
executables: []
|
@@ -56,13 +56,14 @@ files:
|
|
56
56
|
- lib/metlinkr.rb
|
57
57
|
- lib/metlinkr/journey.rb
|
58
58
|
- lib/metlinkr/step.rb
|
59
|
+
- lib/metlinkr/trip.rb
|
59
60
|
- lib/metlinkr/version.rb
|
60
61
|
- metlinkr.gemspec
|
61
62
|
- spec/fixtures/multiple_journey.html
|
62
63
|
- spec/fixtures/tram_snippet.html
|
63
|
-
- spec/journey_spec.rb
|
64
64
|
- spec/spec_helper.rb
|
65
65
|
- spec/step_spec.rb
|
66
|
+
- spec/trip_spec.rb
|
66
67
|
homepage: ''
|
67
68
|
licenses: []
|
68
69
|
post_install_message:
|
@@ -90,6 +91,6 @@ summary: Web-scrapin' the Metlink journey planner
|
|
90
91
|
test_files:
|
91
92
|
- spec/fixtures/multiple_journey.html
|
92
93
|
- spec/fixtures/tram_snippet.html
|
93
|
-
- spec/journey_spec.rb
|
94
94
|
- spec/spec_helper.rb
|
95
95
|
- spec/step_spec.rb
|
96
|
+
- spec/trip_spec.rb
|