Skip to content

Instantly share code, notes, and snippets.

@villelahdenvuo
Created February 2, 2012 07:57
Show Gist options
  • Save villelahdenvuo/1722260 to your computer and use it in GitHub Desktop.
Save villelahdenvuo/1722260 to your computer and use it in GitHub Desktop.
Urly API for Node.js
var http = require('http');
function urly(url, cb) {
// Options for API call
var options = {
host: 'urly.fi',
port: 80,
path: '/api/shorten/?url=' + url}
, code = '';
// Named functions
function data (d) { code += d; }
function done () { cb(null, 'http://urly.fi/' + code); }
function UrlyError(e) {
this.name = "UrlyError";
this.message = "Urly API call failed with error code " + e;
}
UrlyError.prototype = new Error();
UrlyError.prototype.constructor = UrlyError;
// HTTP GET
http.get(options, function(res) {
if (res.statusCode !== 200) { cb(new UrlyError(res.statusCode)); }
else { res.on('data', data).on('end', done); }
}).on('error', cb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment