bmw-js
Version:
BMW javascript REST client for the BMW i3 hackathon.
316 lines (271 loc) • 9.54 kB
JavaScript
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Event=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
// Generated by CoffeeScript 1.8.0
(function() {
var Event, MojioModel,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
MojioModel = _dereq_('./MojioModel');
module.exports = Event = (function(_super) {
__extends(Event, _super);
Event.prototype._schema = {
"Type": "Integer",
"MojioId": "String",
"VehicleId": "String",
"OwnerId": "String",
"EventType": "Integer",
"Time": "String",
"Location": "Object",
"TimeIsApprox": "Boolean",
"BatteryVoltage": "Float",
"ConnectionLost": "Boolean",
"_id": "String",
"_deleted": "Boolean",
"Accelerometer": "Object",
"TripId": "String",
"Altitude": "Float",
"Heading": "Float",
"Distance": "Float",
"FuelLevel": "Float",
"FuelEfficiency": "Float",
"Speed": "Float",
"Acceleration": "Float",
"Deceleration": "Float",
"Odometer": "Float",
"RPM": "Integer",
"DTCs": "Array",
"MilStatus": "Boolean",
"Force": "Float",
"MaxSpeed": "Float",
"AverageSpeed": "Float",
"MovingTime": "Float",
"IdleTime": "Float",
"StopTime": "Float",
"MaxRPM": "Float",
"EventTypes": "Array",
"Timing": "Integer",
"Name": "String",
"ObserverType": "Integer",
"AppId": "String",
"Parent": "String",
"ParentId": "String",
"Subject": "String",
"SubjectId": "String",
"Transports": "Integer",
"Status": "Integer",
"Tokens": "Array"
};
Event.prototype._resource = 'Events';
Event.prototype._model = 'Event';
function Event(json) {
Event.__super__.constructor.call(this, json);
}
Event._resource = 'Events';
Event._model = 'Event';
Event.resource = function() {
return Event._resource;
};
Event.model = function() {
return Event._model;
};
return Event;
})(MojioModel);
}).call(this);
},{"./MojioModel":2}],2:[function(_dereq_,module,exports){
// Generated by CoffeeScript 1.8.0
(function() {
var MojioModel;
module.exports = MojioModel = (function() {
MojioModel._resource = 'Schema';
MojioModel._model = 'Model';
function MojioModel(json) {
this._client = null;
this.validate(json);
}
MojioModel.prototype.setField = function(field, value) {
this[field] = value;
return this[field];
};
MojioModel.prototype.getField = function(field) {
return this[field];
};
MojioModel.prototype.validate = function(json) {
var field, value, _results;
_results = [];
for (field in json) {
value = json[field];
_results.push(this.setField(field, value));
}
return _results;
};
MojioModel.prototype.stringify = function() {
return JSON.stringify(this, this.replacer);
};
MojioModel.prototype.replacer = function(key, value) {
if (key === "_client" || key === "_schema" || key === "_resource" || key === "_model") {
return void 0;
} else {
return value;
}
};
MojioModel.prototype.query = function(criteria, callback) {
var property, query_criteria, value;
if (this._client === null) {
callback("No authorization set for model, use authorize(), passing in a mojio _client where login() has been called successfully.", null);
return;
}
if (criteria instanceof Object) {
if (criteria.criteria == null) {
query_criteria = "";
for (property in criteria) {
value = criteria[property];
query_criteria += "" + property + "=" + value + ";";
}
criteria = {
criteria: query_criteria
};
}
return this._client.request({
method: 'GET',
resource: this.resource(),
parameters: criteria
}, (function(_this) {
return function(error, result) {
return callback(error, _this._client.model(_this.model(), result));
};
})(this));
} else if (typeof criteria === "string") {
return this._client.request({
method: 'GET',
resource: this.resource(),
parameters: {
id: criteria
}
}, (function(_this) {
return function(error, result) {
return callback(error, _this._client.model(_this.model(), result));
};
})(this));
} else {
return callback("criteria given is not in understood format, string or object.", null);
}
};
MojioModel.prototype.get = function(criteria, callback) {
return this.query(criteria, callback);
};
MojioModel.prototype.create = function(callback) {
if (this._client === null) {
callback("No authorization set for model, use authorize(), passing in a mojio _client where login() has been called successfully.", null);
return;
}
return this._client.request({
method: 'POST',
resource: this.resource(),
body: this.stringify()
}, callback);
};
MojioModel.prototype.post = function(callback) {
return this.create(callback);
};
MojioModel.prototype.save = function(callback) {
if (this._client === null) {
callback("No authorization set for model, use authorize(), passing in a mojio _client where login() has been called successfully.", null);
return;
}
return this._client.request({
method: 'PUT',
resource: this.resource(),
body: this.stringify(),
parameters: {
id: this._id
}
}, callback);
};
MojioModel.prototype.put = function(callback) {
return this.save(callback);
};
MojioModel.prototype["delete"] = function(callback) {
return this._client.request({
method: 'DELETE',
resource: this.resource(),
parameters: {
id: this._id
}
}, callback);
};
MojioModel.prototype.observe = function(object, subject, observer_callback, callback) {
if (subject == null) {
subject = null;
}
return this._client.observe(object, subject, observer_callback, callback);
};
MojioModel.prototype.unobserve = function(object, subject, observer_callback, callback) {
if (subject == null) {
subject = null;
}
return this._client.observe(object, subject, observer_callback, callback);
};
MojioModel.prototype.store = function(model, key, value, callback) {
return this._client.store(model, key, value, callback);
};
MojioModel.prototype.storage = function(model, key, callback) {
return this._client.storage(model, key, callback);
};
MojioModel.prototype.unstore = function(model, key, callback) {
return this._client.unstore(model, key, callback);
};
MojioModel.prototype.statistic = function(expression, callback) {
return callback(null, true);
};
MojioModel.prototype.resource = function() {
return this._resource;
};
MojioModel.prototype.model = function() {
return this._model;
};
MojioModel.prototype.schema = function() {
return this._schema;
};
MojioModel.prototype.authorization = function(client) {
this._client = client;
return this;
};
MojioModel.prototype.id = function() {
return this._id;
};
MojioModel.prototype.mock = function(type, withid) {
var field, value, _ref;
if (withid == null) {
withid = false;
}
_ref = this.schema();
for (field in _ref) {
value = _ref[field];
if (field === "Type") {
this.setField(field, this.model());
} else if (field === "UserName") {
this.setField(field, "Tester");
} else if (field === "Email") {
this.setField(field, "test@moj.io");
} else if (field === "Password") {
this.setField(field, "Password007!");
} else if (field !== '_id' || withid) {
switch (value) {
case "Integer":
this.setField(field, "0");
break;
case "Boolean":
this.setField(field, false);
break;
case "String":
this.setField(field, "test" + Math.random());
}
}
}
return this;
};
return MojioModel;
})();
}).call(this);
},{}]},{},[1])
(1)
});