mojio-js-sdk
Version:
Mojio javascript REST client.
321 lines (280 loc) • 8.86 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var HttpWrapper, MojioSDKState, MojioValidator,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
HttpWrapper = require('../wrappers/HttpWrapper');
MojioValidator = require('./MojioValidator');
module.exports = MojioSDKState = (function() {
var accountsURL, apiURL, defaultEndpoint, pushURL, state;
state = {};
accountsURL = "accounts.moj.io";
pushURL = "push.moj.io";
apiURL = "api2.moj.io";
defaultEndpoint = "api";
function MojioSDKState(options) {
var p, v;
if (options == null) {
options = {
environment: '',
version: 'v2'
};
}
this.redirect = bind(this.redirect, this);
this.initiate = bind(this.initiate, this);
this.options = {};
for (p in options) {
v = options[p];
this.options[p] = v;
}
if (this.options.environment == null) {
this.options.environment = '';
}
if (this.options.version == null) {
this.options.version = 'v2';
}
if (this.options.environment !== '') {
this.options.environment += '-';
}
if (!this.options.accountsURL) {
this.options.accountsURL = accountsURL;
}
if (!this.options.apiURL) {
this.options.apiURL = apiURL;
}
if (!this.options.pushURL) {
this.options.pushURL = pushURL;
}
this.endpoints = {
accounts: {
uri: "https://" + this.options.environment + this.options.accountsURL,
encoding: true
},
api: {
uri: "https://" + this.options.environment + this.options.apiURL + '/' + this.options.version,
encoding: false
},
push: {
uri: "https://" + this.options.environment + this.options.pushURL + '/' + this.options.version,
encoding: false
}
};
this.validator = new MojioValidator();
this.reset();
}
MojioSDKState.prototype.initiate = function(callback) {
var callbacks, httpWrapper;
callbacks = function(error, result) {
if ((state.callback != null)) {
state.callback(error, result);
}
if (callback) {
return callback(error, result);
}
};
if (state.answer != null) {
callbacks(null, state.answer);
} else {
httpWrapper = new HttpWrapper(state.token, this.endpoints[state.endpoint].uri, this.endpoints[state.endpoint].encoding);
httpWrapper.request(this.parts(false), callbacks);
}
return this.reset();
};
MojioSDKState.prototype.redirect = function(redirectClass) {
var httpWrapper;
if (redirectClass == null) {
redirectClass = null;
}
httpWrapper = new HttpWrapper(state.token, this.endpoints[state.endpoint].uri, this.endpoints[state.endpoint].encoding);
return httpWrapper.redirect(this.parts(true), redirectClass);
};
MojioSDKState.prototype.url = function(bodyAsParameters) {
var httpWrapper, url;
if (bodyAsParameters == null) {
bodyAsParameters = true;
}
httpWrapper = new HttpWrapper(state.token, this.endpoints[state.endpoint].uri, this.endpoints[state.endpoint].encoding);
url = httpWrapper.url(this.parts(bodyAsParameters));
return url;
};
MojioSDKState.prototype.parts = function(bodyAsParameters) {
if (bodyAsParameters == null) {
bodyAsParameters = true;
}
return {
method: state.method,
resource: state.resource,
pid: state.pid,
action: state.action,
sid: state.sid,
object: state.object,
tid: state.tid,
body: bodyAsParameters ? "" : state.body,
params: bodyAsParameters ? state.body : state.params
};
};
MojioSDKState.prototype.show = function() {
return state;
};
MojioSDKState.prototype.setCallback = function(callback) {
state.callback = callback;
};
MojioSDKState.prototype.setToken = function(token) {
state.token = token;
};
MojioSDKState.prototype.getToken = function() {
return state.token;
};
MojioSDKState.prototype.setAnswer = function(token) {
return state.answer = token;
};
MojioSDKState.prototype.setBody_ObjectOrJson = function(object_or_json_string) {
switch (typeof object_or_json_string) {
case "string":
state.body = object_or_json_string;
break;
case "object":
state.body = object_or_json_string;
}
return state;
};
MojioSDKState.prototype.setEndpoint = function(endpoint) {
var validateEndpoint;
validateEndpoint = (function(_this) {
return function(endpoint, endpoints) {
var found, name, value;
found = false;
for (name in endpoints) {
value = endpoints[name];
if (endpoint === name) {
found = true;
break;
}
}
if (!found) {
throw "Endpoint must be accounts, api, or push";
}
return found;
};
})(this);
if (validateEndpoint(endpoint, this.endpoints)) {
return state.endpoint = endpoint;
}
};
MojioSDKState.prototype.setMethod = function(method) {
this.reset();
return state.method = method;
};
MojioSDKState.prototype.setResource = function(resource) {
if (state.resource === 'Groups') {
return this.setObject(resource);
} else {
return state.resource = resource;
}
};
MojioSDKState.prototype.setObject = function(object) {
return state.object = object;
};
MojioSDKState.prototype.setAction = function(action) {
return state.action = action;
};
MojioSDKState.prototype.setParams = function(parameters) {
var property, results, value;
results = [];
for (property in parameters) {
value = parameters[property];
results.push(state.parameters[property] = value);
}
return results;
};
MojioSDKState.prototype.setBody = function(parameters) {
var property, value;
for (property in parameters) {
value = parameters[property];
state.body[property] = value;
}
};
MojioSDKState.prototype.setId = function(id) {
return this.setPrimaryId(id);
};
MojioSDKState.prototype.setPrimaryId = function(id) {
return state.pid = id;
};
MojioSDKState.prototype.setSecondaryId = function(id) {
return state.sid = id;
};
MojioSDKState.prototype.setTertiaryId = function(id) {
return state.tid = id;
};
MojioSDKState.prototype.getBody = function() {
return state.body;
};
MojioSDKState.prototype.getParams = function() {
return state.parameters;
};
MojioSDKState.prototype.setWhere = function(id_example_or_query) {
state.type = "all";
state.where = null;
if (id_example_or_query !== null) {
switch (typeof id_example_or_query) {
case "number":
state.type = "id";
state.where = id_example_or_query;
break;
case "string":
state.type = "query";
state.where = id_example_or_query;
break;
case "object":
state.type = "example";
state.where = id_example_or_query;
break;
default:
state.type = "query";
state.where = id_example_or_query;
}
}
return state;
};
MojioSDKState.prototype.fixup = function() {
var lowP, lowV, p, results, v;
results = [];
for (p in state) {
v = state[p];
lowP = p.toLowerCase();
lowV = v.toLowerCase();
state[lowP] = lowV;
results.push(delete state[p]);
}
return results;
};
MojioSDKState.prototype.mock = function() {
return state.mock = true;
};
MojioSDKState.prototype.reset = function() {
state.mock = false;
state.callback = null;
state.answer = null;
state.endpoint = defaultEndpoint;
state.method = "GET";
state.resource = "Vehicles";
state.pid = null;
state.sid = null;
state.tid = null;
state.action = null;
state.object = null;
state.parameters = {};
state.operation = "";
state.limit = 10;
state.offset = 0;
state.desc = false;
state.query = null;
state.field = null;
state.fields = null;
state.type = "all";
state.where = null;
return state.body = {};
};
return MojioSDKState;
})();
}).call(this);
//# sourceMappingURL=MojioSDKState.js.map