Skip to content

Commit

Permalink
getKloutIdentity is backwards compatible with 0.2.x #4
Browse files Browse the repository at this point in the history
  • Loading branch information
cojohn committed Dec 18, 2013
1 parent dd2ea15 commit f34d000
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
63 changes: 31 additions & 32 deletions lib/node_klout.js
Expand Up @@ -7,7 +7,7 @@ var http = require("http");
var Klout = module.exports = function(key) {
this._key = key = key || "";
this._version = "v2";
}
};

/// Setters ///
Klout.prototype.setKey = function(key) { this._key = key; return this; };
Expand Down Expand Up @@ -51,22 +51,19 @@ Klout.prototype._get = function(path, callback) {
callback(new Error("Klout is unavailable."), klout_response);
break;
default:
switch (true) {
case /application\/json/.test(klout_response.headers["content-type"]):
try {
callback(null, JSON.parse(data));
}
catch (ex) {
callback(ex, klout_response);
}
break;

case /text\/xml/.test(klout_response.headers["content-type"]):
callback(null, data);
break;

default:
callback(new Error("Unsupported format."), klout_response);
if (/application\/json/.test(klout_response.headers["content-type"])) {
try {
callback(null, JSON.parse(data));
}
catch (ex) {
callback(ex, klout_response);
}
}
else if (/text\/xml/.test(klout_response.headers["content-type"])) {
callback(null, data);
}
else {
callback(new Error("Unsupported format."), klout_response);
}
}
});
Expand All @@ -76,13 +73,15 @@ Klout.prototype._get = function(path, callback) {
};

Klout.prototype.getKloutIdentity = function(id, network, callback) {
if(network instanceof Function){
throw new Error('You must specify a network.');
}else{
if(network === 'fb' || /^\d+$/.test(id)){
this._get("/identity.json/"+network+"/"+id+"?", callback);
}else{
this._get("/identity.json/twitter?screenName="+id+"&", callback)
if (network instanceof Function) {
this._get("/identity.json/twitter?screenName=" + id + "&", network);
}
else {
if (network === 'fb' || /^\d+$/.test(id)) {
this._get("/identity.json/" + network + "/" + id + "?", callback);
}
else {
this._get("/identity.json/twitter?screenName=" + id + "&", callback);
}
}
};
Expand All @@ -100,7 +99,7 @@ Klout.prototype.getSingleKlout = function(users, callback) {
callback(null, 0);
}
});
}
};

/// Version 2 methods ///

Expand Down Expand Up @@ -132,7 +131,7 @@ Klout.prototype.getUserScore = function(klout_id, callback) {
*/
Klout.prototype.getUserTopics = function(klout_id, callback) {
this._get("/user.json/" + klout_id + "/topics?", callback);
}
};

/**
* Returns a Klout user's influence object.
Expand All @@ -145,13 +144,13 @@ Klout.prototype.getUserInfluence = function(klout_id, callback) {
};


Klout.prototype.getUserNetworkHandle = function(klout_id, network, callback) {
var callback = (network instanceof Function)
? network
: callback,
network = (network instanceof Function)
Klout.prototype.getUserNetworkHandle = function(klout_id, _network, _callback) {
var callback = (_network instanceof Function)
? _network
: _callback,
network = (_network instanceof Function)
? "tw"
: network;
: _network;

this._get("/identity.json/klout/" + klout_id + "/" + network + "?", callback);
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
],
"name": "node_klout",
"description": "Klout API wrapper.",
"version": "0.3.0",
"version": "0.3.1",
"repository": {
"type": "git",
"url": "git://github.com/cojohn/node_klout.git"
Expand Down
17 changes: 15 additions & 2 deletions test/test.js
@@ -1,4 +1,4 @@
if(!process.env.API_KEY){
if(!process.env.API_KEY) {
console.log("!!! Please specify your Klout API key using the API_KEY argument.");
process.exit();
}
Expand All @@ -12,7 +12,7 @@ var util = require("util");
var Test = function() {
this.failed = 0;
this.finished = 0;
this.expected = 7;
this.expected = 8;

events.EventEmitter.call(this);
};
Expand Down Expand Up @@ -98,6 +98,19 @@ klout_v2.getKloutIdentity("_cojohn","tw", function(error, klout_user) {
}
});

// Get Klout identity from Twitter screen name
klout_v2.getKloutIdentity("_cojohn", function(error, klout_user) {
try {
assert.equal(klout_user.id, "1212702", "Invalid klout user identity.");
assert.equal(klout_user.network, "ks", "Invalid network.");
test.emit("runVersionTwoTests", klout_user.id);
test.emit("finishedTest");
}
catch (ex) {
test.emit("finishedTest", ex);
}
});

// Get Klout identity from Twitter id
klout_v2.getKloutIdentity(151230368, 'tw', function(error, klout_user) {
try {
Expand Down

0 comments on commit f34d000

Please sign in to comment.