Created
April 26, 2011 20:44
-
-
Save mheadd/943092 to your computer and use it in GitHub Desktop.
Simple Node.js script showing how to retrieve location JSON from PHLAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Include required modules. | |
| var sys = require('sys'); | |
| var http = require('http'); | |
| var EventEmitter = require('events').EventEmitter; | |
| // PHLAPI class. | |
| PhlApi = function(host, port, data_set, version) { | |
| this.host = host; | |
| this.port = port; | |
| this.data_set = data_set | |
| this.version = version || 'v1' | |
| this.responseBody = ''; | |
| }; | |
| PhlApi.prototype = new EventEmitter; | |
| PhlApi.prototype.getLocations = function(lat1, lon1, lat2, lon2) { | |
| var self = this; | |
| var points = new Array(lat1, lon1, lat2, lon2); | |
| var bbox = '?bbox=' + points.join(","); | |
| var path = '/' + this.version + '/' + this.data_set + bbox; | |
| var client = http.createClient(this.port, this.host); | |
| var request = client.request('GET', path, {'Host': this.host + ':' + this.port, 'Accept': 'application/json'}); | |
| request.end(); | |
| request.on('response', function (response) { | |
| response.setEncoding('utf8'); | |
| self.emit('responseCode', response.statusCode); | |
| response.addListener('data', function (chunk) { | |
| self.responseBody += chunk; | |
| }); | |
| response.addListener('end', function() { | |
| self.emit('responseBody', self.responseBody); | |
| }); | |
| }); | |
| }; | |
| // Create a new PhlApi object. | |
| var libraries = new PhlApi('phlapi.com', 5984, 'phl_libraries'); | |
| // Specify bounding box. | |
| libraries.getLocations('-75.1733961','39.9438813','-75.1433961','39.9738813'); | |
| // Add listeners for events emitted from getLocations() method. | |
| libraries.addListener('responseCode', function(code){ | |
| sys.puts('Response code: ' + code); | |
| }); | |
| libraries.addListener('responseBody', function(body) { | |
| sys.puts(body); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment