Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 66414ed7f1599e3db3c26886bf232110878178f5 @cojohn committed Oct 28, 2011
Showing with 146 additions and 0 deletions.
  1. +33 −0 README.md
  2. +1 −0 index.js
  3. +96 −0 lib/node_klout.js
  4. +16 −0 package.json
@@ -0,0 +1,33 @@
+node_klout
+==========
+
+Simple wrapper for the Klout API. Users is an array. The constructor supports passing XML, but I
+haven't implemented that yet. Like the API, users is always an array.
+
+I'll write a test or something, too.
+
+```javascript
+var Klout = require("../lib/klout");
+
+klout = new Klout("YOUR KEY");
+
+klout.getKlout("registerzero", function(error, users) {
+ console.log(users);
+});
+
+klout.getShow("registerzero", function(error, users) {
+ console.log(users);
+});
+
+klout.getTopics("registerzero", function(error, users) {
+ console.log(users);
+});
+
+klout.getInfluencedBy("registerzero", function(error, users) {
+ console.log(users);
+});
+
+klout.getInfluencerOf("registerzero", function(error, users) {
+ console.log(users);
+});
+```
@@ -0,0 +1 @@
+module.exports = require("./lib/node_klout");
@@ -0,0 +1,96 @@
+var http = require("http");
+
+/*
+ * @params
+ * key - Your Klout API key
+ * format - Desired format; json or xml (currently unsupported); defaults json.
+ * version - Desired API version; defaults 1.
+ */
+var Klout = module.exports = function(key, format, version) {
+ this._format = (format === "xml" || format === "json") ? format : "json";
+ this._key = (typeof key == "string") ? key : "";
+ this._version = (version) ? version : 1;
+}
+
+// Make a GET request of the Klout API.
+Klout.prototype._get = function(method, users, callback) {
+ var self = this,
+ options = {
+ host: "api.klout.com",
+ path: "/" + self._version + method + "." + self._format + "?key=" + self._key + "&users=" + users,
+ port: 80
+ },
+ data = "";
+
+ var klout_request = http.get(options, function(klout_response) {
+ klout_response.on("data", function(chunk) {
+ data += chunk;
+ });
+ klout_response.on("error", function(error) {
+ callback(error);
+ });
+ klout_response.on("close", function(error) {
+ callback(error);
+ });
+ klout_response.on("end", function() {
+ // Handle responses as documented at http://developer.klout.com/docs/read/Profile_Detail
+ switch (klout_response.statusCode) {
+ case 401:
+ callback(new Error("Invalid authentication credentials."));
+ break;
+ case 403:
+ callback(new Error("Inactive key, or call threshold reached."));
+ break;
+ case 404:
+ callback(new Error("Resource or user not found."));
+ break;
+ case 500:
+ callback(new Error("Klout internal server error."));
+ break;
+ case 502:
+ callback(new Error("Klout is down or being upgraded."));
+ break;
+ case 503:
+ callback(new Error("Klout is unavailable."));
+ break;
+ default:
+ self._parse(data, callback);
+ };
+ });
+ }).on("error", function(error) {
+ callback(error);
+ });
+}
+
+// Parse JSON response data
+Klout.prototype._parse = function(data, callback) {
+ var parsed, error;
+ if (this._format === "json")
+ try {
+ parsed = JSON.parse(data);
+ }
+ catch (ex) {
+ error = ex;
+ }
+ finally {
+ callback(error, (parsed && parsed.users) ? parsed.users : []);
+ }
+ else callback(new Error("Unsupported format."));
+}
+
+// API methods as documented at http://developer.klout.com/api_gallery
+Klout.prototype.getKlout = function(users, callback) {
+ this._get("/klout", users, callback);
+}
+Klout.prototype.getShow = function(users, callback) {
+ this._get("/users/show", users, callback);
+}
+Klout.prototype.getTopics = function(users, callback) {
+ this._get("/users/topics", users, callback);
+}
+Klout.prototype.getInfluencedBy = function(users, callback) {
+ this._get("/soi/influenced_by", users, callback);
+}
+Klout.prototype.getInfluencerOf = function(users, callback) {
+ this._get("/soi/influencer_of", users, callback);
+}
@@ -0,0 +1,16 @@
+{
+ "author": "Christopher John <cojohn@gmail.com>",
+ "name": "node_klout",
+ "description": "Klout API wrapper.",
+ "version": "0.1.0",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/cojohn/node_klout.git"
+ },
+ "keywords": ["klout", "node_klout", "api"],
+ "main": "./index",
+ "engines": {
+ "node": "~v0.4.12"
+ },
+ "dependencies": { }
+}

0 comments on commit 66414ed

Please sign in to comment.