Permalink
Fetching contributors…
Cannot retrieve contributors at this time
127 lines (103 sloc) 3.51 KB
var API_URLS = {
test_in: 'http://lvh.me:8081/i/api/v1', // With a logged in user
test_out: 'http://lvh.me:8081/o/api/v1', // With no logged in user
staging: 'https://staging.services.sparkart.net/api/v1',
production: 'https://services.sparkart.net/api/v1'
};
var SolidusClient = require('solidus-client');
var Resource = require('solidus-client/lib/resource');
var Universe = function(options) {
if (!(this instanceof Universe)) return new Universe(options);
SolidusClient.call(this, options);
options || (options = {});
this.environment = options.environment || 'production';
this.key = options.key;
};
Universe.prototype = Object.create(SolidusClient.prototype);
Universe.prototype.init = function(callback) {
var self = this;
var data = {};
getFanclub.call(self, function(err, fanclub) {
data.fanclub = fanclub;
if (err) {
if (callback) callback(err, data);
return setTimeout(function() {self.emit('error', err)}, 0);
}
getCustomer.call(self, function(err, customer) {
data.customer = customer;
if (callback) callback(err, data);
if (!err) setTimeout(function() {self.emit('ready', data)}, 0);
});
});
};
Universe.prototype.render = function() {
expandResourcesEndpoints.call(this, arguments[0]);
return SolidusClient.prototype.render.apply(this, arguments);
};
Universe.prototype.get = function(endpoint, callback) {
this.getResource(this.resource(endpoint), null, callback);
};
Universe.prototype.post = function(endpoint, data, callback) {
var resource = new Resource(this.resource(endpoint));
if (resource.requestType() == 'jsonp') {
resource.options.query || (resource.options.query = {});
resource.options.query._method = 'POST';
}
resource.post(data, function(err, response) {
callback(err, response ? response.data : null);
});
};
Universe.prototype.resource = function(endpoint) {
return {
url: resourceUrl.call(this, endpoint),
query: {
key: this.key
},
with_credentials: true
};
};
Universe.prototype.jsonpResource = function(endpoint) {
return {
url: resourceUrl.call(this, endpoint),
jsonp: true
};
};
// PRIVATE
var getFanclub = function(callback) {
if (this.context && this.context.resources && this.context.resources.fanclub) {
callback(null, this.context.resources.fanclub.fanclub);
} else {
this.getResource(this.resource('/fanclub'), null, function(err, data) {
callback(err, data ? data.fanclub : null);
});
}
};
var getCustomer = function(callback) {
var self = this;
self.getResource(self.jsonpResource('/account/status'), null, function(err, data) {
if (err) return callback(err);
if (data.logged_in) {
self.getResource(self.resource('/account'), null, function(err, data) {
callback(err, data ? data.customer : null);
});
} else {
callback();
}
});
};
var expandResourcesEndpoints = function(view) {
if (!view || typeof view !== 'object') return;
for (var name in view.resources) {
var resource = view.resources[name];
if (typeof resource === 'string' && resource[0] === '/') {
resource = this.resource(resource);
} else if (resource !== null && typeof resource === 'object' && typeof resource.url === 'string' && resource.url[0] === '/') {
resource.url = resourceUrl.call(this, resource.url);
}
view.resources[name] = resource;
}
};
var resourceUrl = function(endpoint) {
return (API_URLS[this.environment] || API_URLS['production']) + endpoint;
};
module.exports = Universe;