Skip to content

Commit

Permalink
[api] Started to implement a way to get logging inputs by name
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Dec 1, 2010
1 parent 63921b9 commit 2f071ed
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
6 changes: 6 additions & 0 deletions lib/loggly/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ Config.prototype = {

get logglyUrl () {
return 'https://' + [this._subdomain, 'loggly', 'com'].join('.') + '/api';
},

get inputUrl () {
return 'https://logs.loggly.com' + '/inputs';
}


};

//
Expand Down
42 changes: 34 additions & 8 deletions lib/loggly/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,51 @@ var Loggly = function (config) {
// function log (callback)
// logs args to input device
//
Loggly.prototype.log = function (callback) {
Loggly.prototype.log = function (msg, callback) {

//interns.loggly({ method: 'POST', uri: 'some-url', body: { some: 'body'} }, callback, success)

console.log(this.inputUrl());

var addOptions = {
uri: this.logglyUrl('inputs') + '/e8758e8a-940e-4dbc-ba9a-044e94884a6b',
uri: 'https://logs.loggly.com/inputs' + '/e8758e8a-940e-4dbc-ba9a-044e94884a6b',
auth: this.config.auth,
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: '127.0.0.1 - fudge sundaes'
body: msg
};

eyes.inspect(addOptions);
//eyes.inspect(addOptions);

interns.loggly(addOptions, callback, function (res, body) {
callback(null, res);
body = body.replace(/\'/g,'"'); // loggly api is a bit borked, supported a support issue on this
callback(null, JSON.parse(body));
});

};

//
// function getInputs (callback)
// function getInputs (name, callback)
// Returns a list of all inputs for the authenicated account
//
Loggly.prototype.getInputs = function (callback) {
Loggly.prototype.getInputs = function (name, callback) {

interns.loggly(this.logglyUrl('inputs'), this.config.auth, callback, function (res, body) {
var inputs = [], results = JSON.parse(body);
results.forEach(function (result) {
inputs.push(new (loggly.Input)(result));

//console.log(result);

if(name === result.name){
inputs.push(new (loggly.Input)(result));
}


});

eyes.inspect(inputs);
callback(null, inputs);
});
};
Expand Down Expand Up @@ -117,6 +129,20 @@ Loggly.prototype.logglyUrl = function () {
return [this.config.logglyUrl].concat(args).join('/');
};

Loggly.prototype.inputUrl = function (inputName) {

this.getInputs(inputName, function(err, inputs){

//eyes.inspect(inputName);

//eyes.inspect(inputs);

});

var args = Array.prototype.slice.call(arguments);
return [this.config.logglyUrl].concat(args).join('/');
};

//
// Export the Loggly object
//
Expand Down
7 changes: 4 additions & 3 deletions lib/loggly/interns.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ interns.loggly = function () {
uri: uri,
method: method,
headers: {
'Authorization': 'Basic ' + new Buffer(auth.username + ':' + auth.password).toString('base64')
'Authorization': 'Basic ' + new Buffer(auth.username + ':' + auth.password).toString('base64'),
'Content-Type': "text/plain"
}
};

if (typeof requestBody !== 'undefined') {
requestOptions.headers['Content-Type'] = headers['Content-Type'] || 'application/json';
requestOptions.body = requestOptions.headers['Content-Type'] === 'application/json' ? JSON.stringify(requestBody) : requestBody;
}

//eyes.inspect(requestOptions);
try {
request(requestOptions, function (err, res, body) {
if (err) {
Expand All @@ -121,7 +122,7 @@ interns.loggly = function () {
}
return;
}

//eyes.inspect(body);
var statusCode = res.statusCode.toString();
if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
if (callback) {
Expand Down
11 changes: 6 additions & 5 deletions test/input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ vows.describe('node-loggly/inputs').addBatch({
"When using the node-loggly client": {
"the getInputs() method": {
topic: function () {
loggly.getInputs(this.callback);
loggly.getInputs('test', this.callback);
},
"should return a list of valid inputs": function (err, inputs) {
assert.isNull(err);
Expand All @@ -34,13 +34,14 @@ vows.describe('node-loggly/inputs').addBatch({
}
}
}).addBatch({
"When using the node-loggly client after creating a device ": {
"When using the node-loggly client": {
"the log() method": {
topic: function () {
loggly.log(this.callback);
loggly.log('this is a test logging message from /test/input-test.js', this.callback);
},
"should log messages to loggly": function (err, devices) {
assert.isTrue(false);
"should log messages to loggly": function (err, result) {
assert.equal(result.response, 'success');
assert.isObject(result)
}
}
}
Expand Down

0 comments on commit 2f071ed

Please sign in to comment.