Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #4 from JulianStier/master
Browse files Browse the repository at this point in the history
Continued development
  • Loading branch information
Jak Spalding authored Oct 9, 2018
2 parents 90c9842 + b8a6c1c commit cf12597
Show file tree
Hide file tree
Showing 16 changed files with 897 additions and 202 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
97 changes: 62 additions & 35 deletions lib/onfleet.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,73 @@
'use strict';

const Client = require('./onfleet/client.js');
const Tasks = require('./onfleet/tasks.js');
const Workers = require('./onfleet/workers.js');
const Webhooks = require('./onfleet/webhooks.js');
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

class Onfleet {
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Client = require('./onfleet/client.js');
var Tasks = require('./onfleet/tasks.js');
var Teams = require('./onfleet/teams.js');
var Webhooks = require('./onfleet/webhooks.js');
var Workers = require('./onfleet/workers.js');
var Destinations = require('./onfleet/destinations.js');

var Onfleet = function () {
function Onfleet(apiKey, apiBase) {
_classCallCheck(this, Onfleet);

constructor(apiKey, apiBase) {
this.client = new Client(apiKey, apiBase);
this._destinations = new Destinations(this.client);
this._tasks = new Tasks(this.client);
this._workers = new Workers(this.client);
this._teams = new Teams(this.client);
this._webhooks = new Webhooks(this.client);
this._workers = new Workers(this.client);
}

get tasks() {
return this._tasks;
}

get workers() {
return this._workers;
}
_createClass(Onfleet, [{
key: 'destinations',
get: function get() {
return this._destinations;
}
}, {
key: 'tasks',
get: function get() {
return this._tasks;
}
}, {
key: 'teams',
get: function get() {
return this._teams;
}
}, {
key: 'webhooks',
get: function get() {
return this._webhooks;
}
}, {
key: 'workers',
get: function get() {
return this._workers;
}
}], [{
key: 'Triggers',
get: function get() {
return {
TASK_STARTED: 0,
TASK_ETA: 1,
TASK_ARRIVAL: 2,
TASK_COMPLETED: 3,
TASK_FAILED: 4,
WORKER_DUTY: 5,
TASK_CREATED: 6,
TASK_UPDATED: 7,
TASK_DELETED: 8,
TASK_ASSIGNED: 9,
TASK_UNASSIGNED: 10
};
}
}]);

get webhooks() {
return this._webhooks;
}

static get Triggers() {
return {
TASK_STARTED: 0,
TASK_ETA: 1,
TASK_ARRIVAL: 2,
TASK_COMPLETED: 3,
TASK_FAILED: 4,
WORKER_DUTY: 5,
TASK_CREATED: 6,
TASK_UPDATED: 7,
TASK_DELETED: 8,
TASK_ASSIGNED: 9,
TASK_UNASSIGNED: 10
};
}
}
return Onfleet;
}();

module.exports = Onfleet;
module.exports = Onfleet;
166 changes: 99 additions & 67 deletions lib/onfleet/client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'use strict';

const request = require('request-promise');
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var request = require('request-promise');

var Client = function () {
function Client(apiKey, apiBase) {
_classCallCheck(this, Client);

class Client {
constructor(apiKey, apiBase) {
this.apiKey = apiKey;
this.apiBase = apiBase;
}
Expand All @@ -15,73 +21,99 @@ class Client {
* @param {object} query
* @returns {Promise}
*/
get(path, query=undefined) {
let options = this.authorizeOptions();
options.qs = query;
return request.get(this.toUri(path), options);
}

/**
* Makes a put request.
*
* @param {string} path
* @param {object} body
* @returns {Promise}
*/
put(path, body) {
let options = this.authorizeOptions();
options.body = body;
return request.put(this.toUri(path), options);
}

/**
* Makes a post request.
*
* @param {string} path
* @param {object} body
* @returns {Promise}
*/
post(path, body) {
let options = this.authorizeOptions();
options.body = body;
return request.post(this.toUri(path), options);
}
_createClass(Client, [{
key: 'get',
value: function get(path) {
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;

/**
* Makes a delete request.
*
* @param {string} path
* @returns {Promise}
*/
delete(path) {
let options = this.authorizeOptions();
return request.delete(this.toUri(path), options);
}
var options = this.authorizeOptions();
options.qs = query;
return request.get(this.toUri(path), options);
}

/**
* Adds basic authentication to the options object.
*
* @param {object} options
* @returns {object}
*/
authorizeOptions(options = null) {
options = options || {};
options.auth = options.auth || {};
options.auth['user'] = this.apiKey;
options.auth['sendImmediately'] = true;
options.json = true;
return options;
}
/**
* Makes a put request.
*
* @param {string} path
* @param {object} body
* @returns {Promise}
*/

/**
* Appends the path to the api base in order to get a full endpoint URI.
*
* @param {string} path
* @returns {string}
*/
toUri(path) {
return this.apiBase + path;
}
}
}, {
key: 'put',
value: function put(path, body) {
var options = this.authorizeOptions();
options.body = body;
return request.put(this.toUri(path), options);
}

/**
* Makes a post request.
*
* @param {string} path
* @param {object} body
* @returns {Promise}
*/

}, {
key: 'post',
value: function post(path, body) {
var options = this.authorizeOptions();
options.body = body;
return request.post(this.toUri(path), options);
}

/**
* Makes a delete request.
*
* @param {string} path
* @returns {Promise}
*/

}, {
key: 'delete',
value: function _delete(path) {
var options = this.authorizeOptions();
return request.delete(this.toUri(path), options);
}

/**
* Adds basic authentication to the options object.
*
* @param {object} options
* @returns {object}
*/

}, {
key: 'authorizeOptions',
value: function authorizeOptions() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

options = options || {};
options.auth = options.auth || {};
options.auth['user'] = this.apiKey;
options.auth['sendImmediately'] = true;
options.json = true;
return options;
}

/**
* Appends the path to the api base in order to get a full endpoint URI.
*
* @param {string} path
* @returns {string}
*/

}, {
key: 'toUri',
value: function toUri(path) {
return this.apiBase + path;
}
}]);

return Client;
}();

module.exports = Client;
module.exports = Client;
56 changes: 56 additions & 0 deletions lib/onfleet/destinations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Destinations = function () {
function Destinations(client) {
_classCallCheck(this, Destinations);

this.client = client;
}

/**
* Create a new destination.
*
* http://docs.onfleet.com/docs/destinations#create-destination
*
* @param {object} address - The address for the destination.
* @param {array} location
* @returns {Promise<object[]>}
*/


_createClass(Destinations, [{
key: 'create',
value: function create(address, location) {
return this.client.post('/destinations', {
'address': address,
'location': location
});
}

/**
* Gets a single destination.
*
* http://docs.onfleet.com/docs/destinations#get-single-destination
*
* @param {string} id
* @returns {Promise<object>}
*/

}, {
key: 'get',
value: function get(id) {
if (!id) {
throw new Error('No id given');
}
return this.client.get('/destinations/' + id);
}
}]);

return Destinations;
}();

module.exports = Destinations;
Loading

0 comments on commit cf12597

Please sign in to comment.