<?xml version="1.0" encoding="UTF-8"?>
<table xmlns:i="http://query.yahooapis.com/v1/schema/internalTable.xsd" xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
    <meta>
        <author>@manhinli</author>
        <description>Fetches NXTBUS departure information. `departureDate` is ISO8601, and accepts time zones.</description>
        <sampleQuery>select * from {table} where stopId="6350786798486030227"</sampleQuery>
    </meta>
    <bindings>
        <select itemPath="" produces="XML">
            <inputs>
               <key id="stopId" type="xs:string" paramType="variable" required="true"/>
               <key id="departureDate" type="xs:string" paramType="variable" required="false"/>
               <key id="departureOrArrival" type="xs:string" paramType="variable" required="false"/>
               <key id="stopType" type="xs:string" paramType="variable" required="false"/>
            </inputs>
            <execute>
                <![CDATA[
                    var req,
                        resp,
                        message,
                        
                        maximumAge,
                        
                        departureDate,
                        departureType,
                        stopType;

                    // Settings
                    maximumAge = 60;    // Cache seconds
                    
                    
                    // Defaults
                    var currDate = new Date();
                    departureDate = departureDate || currDate.toISOString();
                    departureType = departureType || 'DEPARTURE';
                    stopType = stopType || 'BUS_STOP';
                        
                        
                    // API
                    url = 'http://nxtbus.act.gov.au/departures';
                    req = y.rest(url);

                    // Accept and Content-Type = 'app/json'
                    req.header('Accept', 'application/json');
                    req.header('Content-Type', 'application/json;charset=UTF-8');
                    
                    // Compression
                    req.header('Accept-Encoding', 'gzip,deflate');
                    req.decompress(true);

                    // POST
                    message = {
                                stopId: stopId,
                                stopType: stopType,
                                departureDate: departureDate,
                                departureTime: departureDate,
                                departureOrArrival: departureType
                              }
                                   
                    resp = req.post(y.jsToString(message));

                    
                    
                    
                    
                    response.maxAge = maximumAge;
                    response.object = resp.response;
                    
                    
                    
                    var internal = <internal/>;
                    internal.departureDate = departureDate;
                    
                    response.object.internal = internal;
                ]]>
            </execute>
        </select>
    </bindings>
</table>
