/
transperth_client.rb
82 lines (72 loc) · 2.5 KB
/
transperth_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'httparty'
require 'nokogiri'
require 'open-uri'
class TransperthClient
URL_SCHEME = "http://www.transperth.wa.gov.au/TimetablesMaps/LiveTrainTimes/tabid/436/stationname/%s/Default.aspx"
# http://136213.mobi/SmartRider/SmartRiderResult.aspx?SRN=
SMART_RIDER_SCHEME = "http://136213.mobi/SmartRider/SmartRiderResult.aspx?SRN=%s"
class TrainTime < APISmith::Smash
property :time
property :line
property :pattern
property :cars
property :status
property :on_time
property :platform
end
class SmartRiderStatus < APISmith::Smash
property :balance
property :concession_type
property :concession_expires
property :autoload
end
def self.smart_rider(code)
code = code.to_s.gsub /\D/, ''
return nil unless code =~ /^\d{9}$/
url = SMART_RIDER_SCHEME % URI.escape(code)
raw = open(url).read
return nil if raw =~ /smartrider number not found/i
doc = Nokogiri::HTML raw
nbsp = Nokogiri::HTML(" ").text
SmartRiderStatus.new({
:balance => doc.at_css('span#lblCurrentBalance').text[/(\d+)\.(\d+)/].to_f,
:autoload => doc.at_css('span#lblAutoload').text.downcase.include?("true"),
:concession_type => doc.at_css('span#lblType').text.strip,
:concession_expires => doc.at_css('span#lblExpires').text.strip.presence
})
end
def self.live_times(station)
url = URL_SCHEME % URI.escape(station.to_s)
doc = Nokogiri::HTML HTTParty.get(url)
nbsp = Nokogiri::HTML(" ").text
container = doc.css('#dnn_ctr1608_ModuleContent table table tr')
return [] if container.blank?
times = container[1..-2].map do |row|
tds = row.css('td').map { |x| x.text.gsub(nbsp, " ").squeeze(' ').strip }
[tds[1], tds[2], tds[3], tds[5]]
time = tds[1]
line = tds[2].gsub(/To /, '')
extra = tds[3].gsub(/\(\d+ cars\)/, '')
platform = extra[/platform (\w+)/, 1].to_i
pattern = extra[/(\w+) pattern/, 1].to_s.strip.presence
cars = tds[3][/(\d+) cars/].to_i
status = tds[5]
on_time = !!(status =~ /On Time/i)
TrainTime.new({
:time => time,
:line => line,
:pattern => pattern,
:cars => cars,
:status => status,
:on_time => on_time,
:platform => platform
})
end
times
end
def self.train_stations
url = URL_SCHEME % URI.escape("Perth Stn")
doc = Nokogiri::HTML HTTParty.get(url)
doc.css('#dnn_ctr1610_DynamicForms_tblQuestions select option').map { |r| r[:value] }
end
end