Skip to content

Commit

Permalink
v1.0.0, migrate to Promise, all dependencies updated
Browse files Browse the repository at this point in the history
  • Loading branch information
pintux committed Jul 28, 2016
1 parent f81c4e8 commit 03646e7
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 334 deletions.
53 changes: 32 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ About
-----


Basic node.js libraries to use Cagliari Open Data API endpoints.
Basic Node.js libraries to use Cagliari Open Data API endpoints.

**WARNING**: current version has been updated to use [ECMAscript 6 Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and it's not backward compatible with previous versions.
If you prefer continuing to use callbacks, please use **[version 0.2.5](https://github.com/pintux/cagliari-opendata/releases/tag/v0.2.5)**

Supported Datasets and Endpoints
--------------------------------

Requirements
------------
Node.js **> v6.3.x**


Supported Datasets and API Endpoints
------------------------------------

- Traffic

Expand All @@ -26,36 +34,34 @@ Basic Example
Getting all installed traffic monitoring stations:

```js
var opendata = require('cagliari-opendata');
var traffic = opendata.traffic;

traffic.getStations(function(err, stations){
const opendata = require('cagliari-opendata');
const traffic = opendata.traffic;

traffic.getStations()
.then(stations => {
if(!err){
console.log(stations);
console.log(stations);
}

});
```

API
---

All functions are asynchronous, thus a `callback(err, data)` is *mandatory* as the last parameter in a call.
All functions are asynchronous and they **return a Promise**.


* <a href="#getStations"><code>traffic.<b>getStations()</b></code></a>
* <a href="#getStation"><code>traffic.<b>getStation()</b></code></a>
* <a href="#getStationData"><code>traffic.<b>getStationData</b></code></a>
* <a href="#getStationData"><code>traffic.<b>getStationData()</b></code></a>
* <a href="#getSensorData"><code>traffic.<b>getSensorData()</b></code></a>

All functions are asynchronous, thus a `callback(err, data)` is *mandatory* as the last parameter in a call.



Data Description
----------------
JSON representations returned by API calls contain the following data items. See descriptions for details.
JSON representations returned by API calls contain the following data items. JSON is the only supported format. See descriptions for details.

JSON field | Descrizione (Italian) | Description (English) |
------------ | ------------- | ------------- |
Expand All @@ -77,42 +83,47 @@ tasso | Percentuale (%) | Percentage (%) |

----------------------------------------------------------
<a name="getStations"></a>
### getStations(cb)
### getStations()

Gets all available traffic stations installed in the city.

A JSON is returned.



-----------------------------------------------------------
<a name="getStation"></a>
### getStation(id, cb)
### getStation(id)
Gets info about a particular station given its `id`.

A JSON is returned.

- `id` is the numeric or String id of the station


-----------------------------------------------------------
<a name="getStationData"></a>
### getStationData(id, startDate, endDate, cb)
### getStationData(id, startDate, endDate)
Gets measurement data from all the sensors in a station, given its `id`.

A JSON is returned.

- `id` is the numeric or string id of the station
- `startDate` a Date representing the start date/time for required measurements (mandatory)
- `endDate` a Date representing the end date/time for required measurements (optional)
- `startDate` a Date object representing the start date/time for required measurements (mandatory)
- `endDate` a Date object representing the end date/time for required measurements (optional, use `null` to skip)


------------------------------------------------------------
<a name="getSensorData"></a>
### getSensorData(id, startDate, endDate, cb)
### getSensorData(id, startDate, endDate)
Gets measurement data from for a specific sensor, given its `id`.

A JSON is returned.

- `id` is the numeric or string id of the sensor
- `startDate` a Date representing the start date/time for required measurements (mandatory)
- `endDate` a Date representing the end date/time for required measurements (optional)
- `startDate` a Date object representing the start date/time for required measurements (mandatory)
- `endDate` a Date object representing the end date/time for required measurements (optional, use `null` to skip)



Links
Expand Down
6 changes: 5 additions & 1 deletion lib/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ module.exports = {
stationData: 'http://www.comune.cagliari.it/portale/api/rs/en/traffico/misurepostazione.json',
sensorData: 'http://www.comune.cagliari.it/portale/api/rs/en/traffico/misuresensore.json'
}
},
errors:{
generic: 'An Error happened calling the Stations API endpoint, maybe due to a temporary server issue. Please, retry later',
dates: 'Error processing dates, please check them',
missingId: 'id field is mandatory, please check it'
}

};


193 changes: 101 additions & 92 deletions lib/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,127 +26,136 @@
"use strict";


var conf = require('./conf');
var debug = require('debug')('lib:traffic');
var util = require('util');
var request = require('request');
var _ = require('lodash');
var dateFormat = require('dateformat');
const conf = require('./conf');
const debug = require('debug')('lib:traffic');
const util = require('util');
const request = require('request');
const _ = require('lodash');
const dateFormat = require('dateformat');


debug("Configuration is: "+ util.inspect(conf));

debug("Configuration is: " + util.inspect(conf));


//get all available stations
exports.getStations = function(cb){
request(conf.endpoints.traffic.stations, function (error, response, body) {
try {
var data = JSON.parse(body).response;
if (!error && response.statusCode === 200) {
return cb(null, {stations: data.result.items.item});
} else {
return cb(data.errors, null);
exports.getStations = () => {
return new Promise((resolve, reject) => {
request(conf.endpoints.traffic.stations, (error, response, body) => {
try {
let data = JSON.parse(body).response;
if (!error && response.statusCode === 200) {
resolve({stations: data.result.items.item});
} else {
reject(data.errors);
}
} catch (exception) {
reject(new Error(conf.errors.generic));
}
}catch(exception){
return cb(new Error('Error calling Stations API endpoint'), null);
}
});
});

};

//get a single station
exports.getStation = function(id, cb){
request(conf.endpoints.traffic.station+'?id='+id, function (error, response, body) {
try {
var data = JSON.parse(body).response;
if (!error && response.statusCode === 200) {
return cb(null, data.result.postazione)
} else {
return cb(data.errors, null);
}
}catch(exception){
return cb(new Error('Error calling Station API endpoint'), null);
exports.getStation = (id) => {
return new Promise((resolve, reject) => {
if(!id || id ===''){
return reject(new Error(conf.errors.missingId));
} else {
let queryString = {id: id};
let options = {
url: conf.endpoints.traffic.station,
qs: queryString
};
request(options, (error, response, body) => {
try {
let data = JSON.parse(body).response;
if (!error && response.statusCode === 200) {
resolve(data.result.postazione);
} else {
reject(new Error(data.errors.error));
}
} catch (exception) {
reject(new Error(conf.errors.generic));
}
});
}
});

};



//get data from a station by id and data range

exports.getStationData = function(stationId, startDate, endDate, cb){
try{
var start = dateFormat(startDate,'yyyymmddHHMM');

var querystring = '?id='+stationId + '&start='+start;
if(!(arguments.length < 4 && cb === undefined)) {
var end = dateFormat(endDate,'yyyymmddHHMM');
querystring += '&end='+end;

}else{
cb = endDate;
}
}catch(exception){
if(arguments.length < 4 && cb === undefined){
cb = endDate;
}
return cb(new Error('Error processing dates, please check them'), null);
}



request(conf.endpoints.traffic.stationData + querystring, function (error, response, body) {
try{
var data = JSON.parse(body).response;
if (!error && response.statusCode === 200 && data.result !== 'FAILURE') {
return cb(null, data.result.misurePostazione);
}else{
return cb(data.errors, null);
exports.getStationData = function (stationId, startDate = new Date(), endDate = new Date()) {

return new Promise((resolve, reject) => {
if(!stationId || stationId === ''){
reject(new Error(conf.errors.missingId));
} else {
let queryString = {};
try {
let start = dateFormat(startDate, 'yyyymmddHHMM');
let end = dateFormat(endDate, 'yyyymmddHHMM');
queryString = {id: stationId, start: start, end: end};
} catch (exception) {
reject(new Error(conf.errors.dates));
}

}catch(exception){
return cb(new Error('Error calling Station Data API endpoint'), null);
let options = {
url: conf.endpoints.traffic.stationData,
qs: queryString
};
request(options, function (error, response, body) {
try {
let data = JSON.parse(body).response;
if (!error && response.statusCode === 200 && data.result !== 'FAILURE') {
resolve(data.result.misurePostazione);
} else {
reject(data.errors);
}
} catch (exception) {
reject(new Error(conf.errors.generic));
}
});
}

});

};


//get data from a sensor by id and data range
exports.getSensorData = function(sensorId, startDate, endDate, cb){

try{
var start = dateFormat(startDate,'yyyymmddHHMM');
var querystring = '?id='+sensorId + '&start='+start;
if(!(arguments.length < 4 && cb === undefined)) {
var end = dateFormat(endDate,'yyyymmddHHMM');
querystring += '&end='+end;
}else{
cb = endDate;
}
exports.getSensorData = function (sensorId, startDate = new Date(), endDate = new Date()) {

}catch(exception){
if(arguments.length < 4 && cb === undefined){
cb = endDate;
}
return cb(new Error('Error processing dates, please check them'), null);
}
return new Promise((resolve, reject) => {

if(!sensorId || sensorId === ''){
reject(new Error(conf.errors.missingId));
} else {
let queryString = {};
try {
let start = dateFormat(startDate, 'yyyymmddHHMM');
let end = dateFormat(endDate, 'yyyymmddHHMM');
queryString = {id: sensorId, start: start, end: end};

request(conf.endpoints.traffic.sensorData + querystring, function (error, response, body) {
try{
var data = JSON.parse(body).response;
if (!error && response.statusCode === 200 && data.result !== 'FAILURE') {
return cb(null, data.result.misureSensore);
}else{
return cb(data.errors, null);
} catch (exception) {
reject(new Error(conf.errors.dates));
}
}catch(exception){
return cb(new Error('Error calling Station Data API endpoint'), null);
}

let options = {
url: conf.endpoints.traffic.sensorData,
qs: queryString
};
request(options, function (error, response, body) {
try {
let data = JSON.parse(body).response;
if (!error && response.statusCode === 200 && data.result !== 'FAILURE') {
resolve(data.result.misureSensore);
} else {
reject(data.errors);
}
} catch (exception) {
reject(new Error(conf.errors.generic));
}

});
};
});
};
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "cagliari-opendata",
"version": "0.2.5",
"version": "1.0.0",
"description": "Cagliari Open Data SDK",
"homepage":"https://github.com/pintux/cagliari-opendata",
"main": "index.js",
"scripts": {
"test": "mocha -t 5000"
"test": "DEBUG=lib:traffic mocha -t 5000"
},
"author": " Antonio Pintus",
"license": "MIT",
"dependencies": {
"async": "1.5.2",
"async": "2.0.1",
"dateformat": "1.0.12",
"debug": "latest",
"lodash": "4.6.1",
"request": "2.69.0"
"lodash": "4.14.0",
"request": "2.74.0"
},
"devDependencies": {
"mocha": "latest",
Expand Down
Loading

0 comments on commit 03646e7

Please sign in to comment.