emt-bus
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -9,2 +9,3 @@ 'use strict'; | ||
exports.BIKE = 'BiciMad'; | ||
@@ -6,3 +6,4 @@ 'use strict'; | ||
const media_endpoints = require('./media_endpoints'); | ||
const bike_endpoints = require('./bike_endpoints'); | ||
module.exports = { bus_endpoints, geo_endpoints, media_endpoints }; | ||
module.exports = { bus_endpoints, geo_endpoints, media_endpoints, bike_endpoints }; |
147
index.js
@@ -12,3 +12,4 @@ 'use strict'; | ||
GEO, | ||
MULTIMEDIA | ||
MULTIMEDIA, | ||
BIKE | ||
} = require('./config/category'); | ||
@@ -18,3 +19,4 @@ const { | ||
geo_endpoints, | ||
media_endpoints | ||
media_endpoints, | ||
bike_endpoints | ||
} = require('./config/endpoints'); | ||
@@ -30,8 +32,8 @@ | ||
module.exports = function emtService(clientId, passKey) { | ||
return function typeService(category) { | ||
if (category === 'bus') return new Bus(clientId, passKey, BUS); | ||
if (category === 'geo') return new Geo(clientId, passKey, GEO); | ||
if (category === 'media') return new Geo(clientId, passKey, MULTIMEDIA); | ||
if (category === 'bike') return; | ||
if (category === 'parking') return; | ||
return function typeService(service) { | ||
if (service === 'bus') return new Bus(clientId, passKey, BUS, 'POST'); | ||
if (service === 'geo') return new Geo(clientId, passKey, GEO, 'POST'); | ||
if (service === 'media') return new Media(clientId, passKey, MULTIMEDIA, 'POST'); | ||
if (service === 'bike') return new Bike(clientId, passKey, BIKE, 'GET'); | ||
if (service === 'parking') return; | ||
}; | ||
@@ -44,8 +46,7 @@ }; | ||
* @param {string} passKey - Password to validate the client autentification | ||
* @param {string} category - It can either be bus or geo | ||
*/ | ||
const Service = function (clientId, passKey, category) { | ||
const Service = function (clientId, passKey, rest_method) { | ||
var client = clientId; // private attribute | ||
var pass = passKey; // private attribute | ||
this.category = category; | ||
this.rest_method = rest_method; | ||
@@ -73,11 +74,14 @@ /** | ||
*/ | ||
this.glueURL = function () { | ||
return BUS_DOMAIN + this.category; | ||
this.glueURL = function (endpoint, params) { | ||
if (this.category !== BIKE) return `${BUS_DOMAIN}${this.category}/${endpoint}.php`; | ||
return `${BIKE_DOMAIN}/${endpoint}/${this.getClient()}/${this.getPassword()}}`; | ||
}; | ||
/** | ||
* Forms the authentication credentials so it can be added to | ||
* the body of the request and so, the user can succesfully | ||
* the request body, therefore the user can succesfully | ||
* have permission to it. | ||
*/ | ||
this.glueAuth = function () { | ||
this.glueBody = function (params) { | ||
if (this.category === BIKE) return; | ||
const auth = {}; | ||
@@ -87,3 +91,4 @@ auth['idClient'] = this.getClient(); | ||
return auth; | ||
Object.assign(params, auth); | ||
return params; | ||
}; | ||
@@ -100,13 +105,10 @@ | ||
*/ | ||
Service.prototype.makeRequest = function (endpoint, body = {}) { | ||
/* | ||
Creates an object that will embed | ||
the cliendId and the password into | ||
itself | ||
*/ | ||
Object.assign(body, this.glueAuth()); | ||
Service.prototype.makeRequest = function (endpoint, params = {}) { | ||
const url = this.glueURL(endpoint, params); | ||
const body = this.glueBody(params); | ||
return request({ | ||
'method': 'POST', | ||
'uri': this.glueURL() + '/' + endpoint + '.php', | ||
'method': this.rest_method, | ||
'uri': url, | ||
'form': body, | ||
@@ -116,5 +118,5 @@ 'gzip': true, | ||
}) | ||
.then(function (response) { | ||
return JSON.parse(response); | ||
}); | ||
.then(response => { | ||
return JSON.parse(response); | ||
}); | ||
}; | ||
@@ -130,4 +132,5 @@ | ||
*/ | ||
const Bus = function (clientId, passKey, category) { | ||
Service.call(this, clientId, passKey, category); | ||
const Bus = function (clientId, passKey, category, rest_method) { | ||
Service.call(this, clientId, passKey, rest_method); | ||
this.category = category; | ||
}; | ||
@@ -143,4 +146,4 @@ | ||
*/ | ||
Bus.prototype.getCalendar = function (SelectDateBegin,SelectDateEnd) { | ||
const body = {SelectDateBegin, SelectDateEnd} | ||
Bus.prototype.getCalendar = function (SelectDateBegin, SelectDateEnd) { | ||
const body = { SelectDateBegin, SelectDateEnd } | ||
return this.makeRequest(bus_endpoints.GET_CALENDAR, body); | ||
@@ -162,3 +165,3 @@ }; | ||
Bus.prototype.getListLines = function (SelectDate, Lines) { | ||
const body = {SelectDate, Lines} | ||
const body = { SelectDate, Lines } | ||
return this.makeRequest(bus_endpoints.GET_LIST_LINES, body); | ||
@@ -173,3 +176,3 @@ }; | ||
Bus.prototype.getNodesLines = function (Nodes) { | ||
const body = {Nodes}; | ||
const body = { Nodes }; | ||
return this.makeRequest(bus_endpoints.GET_NODES_LINES, body); | ||
@@ -185,3 +188,3 @@ }; | ||
Bus.prototype.getRouteLines = function (SelectDate, Lines) { | ||
const body = {SelectDate, Lines}; | ||
const body = { SelectDate, Lines }; | ||
return this.makeRequest(bus_endpoints.GET_ROUTE_LINES, body); | ||
@@ -196,3 +199,3 @@ }; | ||
Bus.prototype.getRouteLinesRoute = function (SelectDate, Lines) { | ||
const body = {SelectDate, Lines} | ||
const body = { SelectDate, Lines } | ||
return this.makeRequest(bus_endpoints.GET_ROUTE_LINES_ROUTE, body); | ||
@@ -207,3 +210,3 @@ }; | ||
Bus.prototype.getTimeTableLines = function (SelectDate, Lines) { | ||
const body = {SelectDate, Lines} | ||
const body = { SelectDate, Lines } | ||
return this.makeRequest(bus_endpoints.GET_TIME_TABLE_LINES, body); | ||
@@ -218,3 +221,3 @@ }; | ||
Bus.prototype.getTimesLines = function (SelectDate, Lines) { | ||
const body = {SelectDate, Lines} | ||
const body = { SelectDate, Lines } | ||
return this.makeRequest(bus_endpoints.GET_TIMES_LINES, body); | ||
@@ -231,4 +234,5 @@ }; | ||
*/ | ||
const Geo = function (clientId, passKey, category) { | ||
Service.call(this, clientId, passKey, category); | ||
const Geo = function (clientId, passKey, category, rest_method) { | ||
Service.call(this, clientId, passKey, rest_method); | ||
this.category = category; | ||
}; | ||
@@ -313,4 +317,5 @@ | ||
*/ | ||
const Multimedia = function (clientId, passKey, category) { | ||
Service.call(this, clientId, passKey, category); | ||
const Multimedia = function (clientId, passKey, category, rest_method) { | ||
Service.call(this, clientId, passKey, rest_method); | ||
this.category = category; | ||
}; | ||
@@ -320,22 +325,38 @@ | ||
/** | ||
* Get estimate arrival time to stop and its related issues | ||
*/ | ||
Multimedia.prototype.getEstimatesIncident = function (params) { | ||
return this.makeRequest(media_endpoints.getEstimatesIncident, params); | ||
}; | ||
Multimedia.prototype.getEstimatesIncident = function (params) { | ||
return this.makeRequest(media_endpoints.GET_ESTIMATES_INCIDENT, params); | ||
}; | ||
/** | ||
* Request up to three optimal routes from one place to another using bus or walking, | ||
* source and destination must be in a format known for the system, which means that | ||
* should have been validated by a GetStreet call | ||
*/ | ||
Multimedia.prototype.GetStreetRoute = function (params) { | ||
return this.makeRequest(media_endpoints.GET_STREET_ROUTE, params); | ||
}; | ||
/** | ||
* | ||
*/ | ||
Multimedia.prototype.getRouteWithAlarm = function (params) { | ||
return this.makeRequest(media_endpoints.GET_ROUTE_WITH_ALARM, params); | ||
}; | ||
/** | ||
* | ||
*/ | ||
Multimedia.prototype.getRouteWithAlarmResponse = function (params) { | ||
return this.makeRequest(media_endpoints.GET_ROUTE_WITH_ALARM_RESPONSE, params); | ||
}; | ||
/** | ||
* | ||
*/ | ||
Multimedia.prototype.getRoute = function (params) { | ||
return this.makeRequest(media_endpoints.GET_ROUTE, params); | ||
}; | ||
/** | ||
* | ||
*/ | ||
Multimedia.prototype.getRouteResponse = function (params) { | ||
@@ -345,2 +366,30 @@ return this.makeRequest(media_endpoints.GET_ROUTE_RESPONSE, params); | ||
/** | ||
* Set of services that let's know the actual state and availability | ||
* for all the bikes located in Madrid | ||
* | ||
* @param {string} clientId - client username to identify with | ||
* @param {string} passKey - password to validate the client autentification | ||
*/ | ||
const Bike = function (clientId, passKey, category, rest_method) { | ||
Service.call(this, clientId, passKey, rest_method); | ||
this.category = category; | ||
}; | ||
Bike.prototype = Object.create(Service.prototype); // inherits from service class | ||
/** | ||
* Obtiene la relación de todas las bases de Bicimad y su estado | ||
* operacional. | ||
*/ | ||
Bike.prototype.getStations = function () { | ||
return this.makeRequest(bike_endpoints.GET_STATIONS); | ||
}; | ||
/** | ||
* Obtiene la información de una base. | ||
*/ | ||
Bike.prototype.getSingleStations = function (baseId) { | ||
return this.makeRequest(bike_endpoints.GET_SINGLE_STATION, baseId); | ||
}; | ||
// Posible ws code responses | ||
@@ -347,0 +396,0 @@ const responses = { |
{ | ||
"name": "emt-bus", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Library that works a layer over the emtmadrid bus service API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,3 +13,3 @@ 'use strict'; | ||
}); | ||
describe('Bus API method requests', function (done) { | ||
describe('Bus API service', function (done) { | ||
let bus; | ||
@@ -79,2 +79,17 @@ | ||
describe('Bike API service', function (done) { | ||
let bike; | ||
before(function () { | ||
bike = EMT('bike'); | ||
}); | ||
it('/getStations', function () { | ||
return bike.getStations('09/09/2016','09/09/2017') | ||
.then(function (res) { | ||
console.log(res); | ||
assert.equal(res["code"], "0"); | ||
}); | ||
}); | ||
}); | ||
}); |
21188
15
505