Here's the complete code for the "Get Klout Score" machine from the node-machine.org documentation:
module.exports = {
friendlyName: 'Get Klout score',
description: 'Get the Klout score for a Klout user.',
extendedDescription: '',
inputs: {
kloutId: {
example: '32423423432342',
description: 'A Klout ID.',
required: true
},
apiKey: {
example: 'ODUfdisauPUdufsoUSF',
description: 'Your Klout API key.',
required: true
}
},
exits: {
error: {
description: 'Unexpected error occurred.'
},
wrongOrNoKey: {
description: 'Invalid or unprovided API key. All calls must have a key.'
},
success: {
description: 'Returns the Klout score for a Klout user.',
example: '54.342342332'
}
},
fn: function(inputs, exits) {
var URL = require('url');
var QS = require('querystring');
var _ = require('lodash');
var Http = require('machinepack-http');
Http.sendHttpRequest({
baseUrl: 'http://api.klout.com/v2/user.json/' + inputs.kloutId + '/score?key=' + inputs.apiKey,
url: '',
method: 'get',
}).exec({
// OK.
success: function(result) {
try {
var responseBody = JSON.parse(result.body);
} catch (e) {
return exits.error('An error occurred while parsing the body.');
}
return exits.success(responseBody.score);
},
// Non-2xx status code returned from server
notOk: function(result) {
try {
if (result.status === 403) {
return exits.wrongOrNoKey("Invalid or unprovided API key. All calls must have a key.");
}
} catch (e) {
return exits.error(e);
}
return exits.error(result);
},
// An unexpected error occurred.
error: function(err) {
exits.error(err);
},
});
},
};