-
Notifications
You must be signed in to change notification settings - Fork 11
/
perth_transit-1.0.js
93 lines (77 loc) · 2.71 KB
/
perth_transit-1.0.js
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
83
84
85
86
87
88
89
90
91
92
93
// Perth Transit JS API
//
// Version: 1.0
// Requires: jQuery
// License: MIT
// Copyright: Darcy Laycock
(function($, undefined) {
var PerthTransit = {
version: 1,
root: "http://api.perthtransit.com/",
};
var dataWithLocation = function(location, distance) {
if(!location) {
return null;
} else {
var lat = location.lat || location.latitude;
var lng = location.lng || location.lon || location.longitude;
var near = "" + lat + "," + lng;
var options = {"near": near};
if(distance !== undefined) options['distance'] = distance;
return options;
}
};
var extractIdentifier = function(object) {
var type = typeof object;
if(type === "number" || type === "string") {
return "" + object;
} else {
return object.identifier;
}
}
var locationDeferred = function(invoker) {
var deferred = $.Deferred();
var positionCallback = function(position) {
var newDeferred = invoker(position.coords);
newDeferred.then(deferred.resolve, deferred.reject);
};
navigator.geolocation.getCurrentPosition(positionCallback, null, {maximumAge:600000});
return deferred;
}
PerthTransit.get = function(path, options) {
var url = this.urlFor(path, options);
var deferred = $.getJSON(url);
var done = function(data) { return data.response; };
var failed = function(data) { return data; };
return deferred.pipe(done, failed);
};
PerthTransit.urlFor = function(path, options) {
var fullURL = this.root + this.version + "/" + path + "?callback=?";
// We include the params if specified.
if(options) fullURL = fullURL + "&" + $.param(options);
return fullURL;
}
PerthTransit.nearbyTrainStations = function(distance) {
return locationDeferred(function(location) { return PerthTransit.trainStations(location, distance); });
};
PerthTransit.nearbyBusStops = function(distance) {
return locationDeferred(function(location) { return PerthTransit.busStops(location, distance); });
};
PerthTransit.trainStations = function(location, distance) {
return this.get('train_stations', dataWithLocation(location, distance));
};
PerthTransit.trainStation = function(station) {
return this.get('train_stations/' + extractIdentifier(station));
};
PerthTransit.busStops = function(location, distance) {
return this.get('bus_stops', dataWithLocation(location, distance));
};
PerthTransit.busStop = function(stop) {
return this.get('bus_stops/' + extractIdentifier(stop));
};
PerthTransit.smartRider = function(smartRiderNumber) {
return this.get('smart_riders/' + smartRiderNumber);
};
// Run free my child.
window['PerthTransit'] = PerthTransit;
})(jQuery);