Created
December 29, 2010 15:26
-
-
Save KOBA789/758626 to your computer and use it in GitHub Desktop.
ux.nuの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 ux_nu = require('./ux_nu'); | |
| //短縮(Shorten) | |
| ux_nu.shorten('http://www.gehirn.co.jp/', function (err, url, obj) { | |
| console.log(url); | |
| }); | |
| //元に戻す(Expand) | |
| ux_nu.expand('http://ux.nu/Gehirn', function (err, url, obj) { | |
| console.log(url); | |
| }); |
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 request (path, callback) { | |
| var client = http.createClient(80, 'ux.nu'); | |
| var request = client.request('GET', path, {host: 'ux.nu'}); | |
| request.end(); | |
| request.on('response', function (response) { | |
| var buffer = String(); | |
| response.setEncoding('utf8'); | |
| response.on('data', function (chunk) { | |
| buffer += chunk; | |
| }); | |
| response.on('end', function () { | |
| try { | |
| var parsed = JSON.parse(buffer); | |
| callback(null, parsed); | |
| } catch (e) { | |
| callback(e, buffer); | |
| } | |
| }); | |
| }); | |
| } | |
| module.exports.shorten = function (url, callback) { | |
| var apiUrl = '/api/short?format=json&url=' + url; | |
| request(apiUrl, function (err, result) { | |
| var shortenedUrl = null; | |
| if (!err) { | |
| shortenedUrl = result.data.url; | |
| } | |
| callback(err, shortenedUrl, result); | |
| }); | |
| } | |
| module.exports.expand = function (url, callback) { | |
| var apiUrl = '/hugeurl?format=json&url=' + url; | |
| request(apiUrl, function (err, result) { | |
| var expandedUrl = null; | |
| if (!err) { | |
| expandedUrl = result.exp; | |
| } | |
| callback(err, expandedUrl, result); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment