cm1.js | |
---|---|
Carbon, energy, and other environmental impact calculations for your JavaScript objects. Built for the browser and Node.js. | var ImpactEstimate = require('./impact-estimate'),
ImpactEstimator = require('./impact-estimator');
var HttpAdapter = require('./adapters/http-adapter'),
QueueAdapter = require('./adapters/queue-adapter'),
WebsocketAdapter = require('./adapters/websocket-adapter');
var CM1 = module.exports = function() {
this.attributeMap = {};
if(!CM1.adapter)
CM1.useHttpAdapter();
}; |
UsageFor a quick, one-off calculation, you can use | CM1.impacts = function(type, properties, callback) {
var model = CM1.model(type, properties);
model.getImpacts(callback);
}; |
Alternatively, | CM1.model = function(type, properties) {
var attributes = Object.keys(properties);
var proto = function() {};
CM1.extend(proto, {
model: type,
provides: attributes
});
var object = new proto();
for(var i = 0; i < attributes.length; i++) {
var attribute = attributes[i];
object[attribute] = properties[attribute];
}
return object;
}; |
You can also extend any prototype (class) to become and impact calculating machine. For example, let's say we have a class representing a rental car:
If you want to figure out how much CO2 it emits, use
This says "my RentalCar prototype will use the Automobile emitter to calculate impacts. It uses the make property to provide make to the web service, model maps to model, and the fuelEconomy property maps to fuel_efficiency on CM1. Now you can calculate impacts:
There are a whole bunch of other models available, including computer usage, rail trips, and flights. | CM1.extend = function(klass, mapping) {
klass.cm1 = new CM1();
klass.cm1.define(mapping);
klass.prototype.impactEstimator = new ImpactEstimator(klass.cm1);
klass.prototype.getImpacts = function(callback) {
return this.impactEstimator.getImpacts(this, callback);
};
}; |
Specifying an API KeyCM1 is free for non-commercial use and available for commercial use. In either case, you need to sign up for a Brighter Planet API key if you haven't already. To do so, go to keys.brighterplanet.com. Once you have your key, you can specify it with:
Note: if using the stand-alone library, | CM1.prototype.key = function() {
if(process && process.env && process.env.CM1_KEY)
return process.env.CM1_KEY;
else
return CM1.key;
}; |
Connection Adapters: HTTP, Websockets, etc.CM1.js can use a standard RESTful HTTP adapter (default) or an HTML5 Websockets adapter. | |
The standard HTTP adapter sends a separate HTTP request for each calculation performed. This is ideal for when one or only a few calculations are made at a given time. | CM1.useHttpAdapter = function() {
CM1.adapter = new HttpAdapter();
}; |
The Websockets adapter is ideal for when many calculations need to be made at once.
You will need to | CM1.useWebsocketAdapter = function() {
CM1.adapter = new WebsocketAdapter();
}; |
The Queue adapter is for clients who have access to a CM1 queue.
You will need to | CM1.useQueueAdapter = function() {
CM1.adapter = new QueueAdapter();
}; |
Etc.Apply a mapping to a CM1-enabled object. | CM1.prototype.define = function(mapping) {
this.emitAs(mapping.model);
var provisions = mapping.provide || mapping.provides;
this.provide(provisions);
}; |
Set the model (e.g. flight) used for calculation. | CM1.prototype.emitAs = function(model) {
this.model = model;
}; |
Define the properties of the CM1-enabled object that are sent as
characteristics to CM1's models.
The format of attributes can be:
* | CM1.prototype.provide = function(attributes) {
for(var i in attributes) {
if(attributes.hasOwnProperty(i)) {
var value = attributes[i];
if(typeof value == 'object') {
this.provide(value);
} else if(/^\d+$/.test(i)) {
this.attributeMap[this.underscore(value)] = value;
} else {
this.attributeMap[this.underscore(i)] = value;
}
}
}
};
CM1.prototype.underscore = function(string) {
return string.replace(/([a-z])([A-Z])/g, function(str, first, second) {
return first + '_' + second.toLowerCase();
});
};
CM1.ImpactEstimate = ImpactEstimate;
CM1.ImpactEstimator = ImpactEstimator;
|
Deploy With BrowserifyCM1.js can be used with browserify.
Simply | |