Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Job to get tram data for dashing
require "rexml/document"
SCHEDULER.every '1m', :first_in => 0 do |job|
stop_no = 1819
url = URI.parse('http://ws.tramtracker.com.au/pidsservice/pids.asmx')
request =Net::HTTP::Post.new(url.path)
request.body = %Q{<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<PidsClientHeader xmlns="http://www.yarratrams.com.au/pidsservice/">
<ClientGuid>391f09fd-3b98-44e0-b6cd-575ca2b97e2b</ClientGuid>
<ClientType>IPHONEPID</ClientType>
<ClientWebServiceVersion>6.4.0.0</ClientWebServiceVersion>
<ClientVersion>1.5</ClientVersion>
<OSVersion>7.0</OSVersion>
</PidsClientHeader>
</soap12:Header>
<soap12:Body>
<GetNextPredictedRoutesCollection xmlns="http://www.yarratrams.com.au/pidsservice/">
<lowFloor>false</lowFloor>
<stopNo>#{stop_no}</stopNo>
<routeNo>0</routeNo>
</GetNextPredictedRoutesCollection>
</soap12:Body>
</soap12:Envelope>
}
request.add_field("Content-Type", "application/soap+xml; charset=utf-8")
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
services = []
doc = REXML::Document.new response.body
REXML::XPath.each(doc, '//ToReturn').to_a.each do |toreturn|
data = {}
data[:value] = ((Time.parse(toreturn.elements['PredictedArrivalDateTime'].text).to_i - Time.now.to_i) / 60).to_s + " minutes"
data[:label] = toreturn.elements['RouteNo'].text
services << data
end
send_event('trams', {items: services })
end
@maks

This comment has been minimized.

Copy link

@maks maks commented Jul 25, 2013

Thanks Alan, thats about the simpliest/smallest example for the tramtracker api that I've seen so far, gotta love SOAP :-)

@sgleadow

This comment has been minimized.

Copy link

@sgleadow sgleadow commented Aug 1, 2013

Nice Alan, particularly now I'm working on a tram line again... are there any docs about the tram tracker API out there or did you just sniff enough to get this working? I'd like to write a little Mac app that sends OS X Notifications when it's time for me to get the tram home!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment