Created
February 2, 2012 07:57
-
-
Save villelahdenvuo/1722260 to your computer and use it in GitHub Desktop.
Urly API for Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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