Permalink
Browse files

File structure best practice

  • Loading branch information...
1 parent ef78515 commit 970c19b49cc9ec03483aebe2ba7fff16d9cb202c @kaimallea committed Jul 21, 2011
Showing with 125 additions and 125 deletions.
  1. +33 −0 cli.js
  2. +91 −124 lib/googl.js
  3. +1 −1 package.json
View
33 cli.js
@@ -0,0 +1,33 @@
+#!/usr/bin/env node
+
+var googl = require('./lib/googl.js'),
+ url = require('url'),
+ args = process.argv.slice(2),
+ hostname = null;
+
+if (args.length) {
+ args.forEach(function (val, index, array) {
+ if (typeof url.parse(val).protocol === 'undefined') {
+ val = 'http://' + val;
+ }
+
+ hostname = (typeof url.parse(val).hostname === 'undefined' ? '' :
+ url.parse(val).hostname);
+
+ if (!hostname) {
+ console.log('Invalid url: %s', val);
+ return;
+ }
+
+ if (hostname === 'goo.gl') {
+ googl.expand(val, function (res) {
+ console.log('%s -> %s', val, (res.longUrl || JSON.stringify(res)));
+ });
+ return;
+ }
+
+ googl.shorten(val, function (res) {
+ console.log('%s -> %s', val, (res.id || JSON.stringify(res)));
+ });
+ });
+}
View
@@ -1,138 +1,105 @@
-#!/usr/bin/env node
+var googl = (function () {
+ var _url = require('url'),
+ _https = require('https'),
+ _querystring = require('querystring'),
+ _apikey = '';
-var _url = require('url'),
- _https = require('https'),
- _querystring = require('querystring'),
- args = process.argv.slice(2),
-
- googl = (function () {
- var _apikey = '';
-
-
- function _setKey(key) {
- _apikey = key || '';
- exports.key = _apikey;
+
+ function _setKey(key) {
+ _apikey = key || '';
+ exports.key = _apikey;
+ }
+
+
+ function _getKey () {
+ return (_apikey ? _apikey : '');
+ }
+
+
+ function _shorten (url, callback) {
+ if (!url) {
+ console.error('Please specify a valid url.');
+ return;
}
-
- function _getKey () {
- return (_apikey ? _apikey : '');
+ if (typeof _url.parse(url).protocol === 'undefined') {
+ url = 'http://' + url;
}
+ if (!callback) { callback = false; }
- function _shorten (url, callback) {
- if (!url) {
- console.error('Please specify a valid url.');
- return;
- }
-
- if (typeof _url.parse(url).protocol === 'undefined') {
- url = 'http://' + url;
- }
-
- if (!callback) { callback = false; }
-
- var key = _getKey(),
- options = {
- host: 'www.googleapis.com',
- port: 443,
- path: '/urlshortener/v1/url' + (key ? '?' + _querystring.stringify({'key': key}) : ''),
- method: 'POST',
- headers: {
- 'content-type': 'application/json'
- }
- };
-
- var req = _https.request(options, function(res) {
- res.setEncoding('utf8');
- res.on('data', function (d) {
- d = JSON.parse(d);
- if (callback) {
- callback(d);
- } else {
- console.log(d.id || d.error);
- }
- });
- });
-
- req.on('error', function(e) { console.error(e); });
-
- req.write(JSON.stringify({'longUrl': url}));
-
- req.end();
- }
+ var key = _getKey(),
+ options = {
+ host: 'www.googleapis.com',
+ port: 443,
+ path: '/urlshortener/v1/url' + (key ? '?' + _querystring.stringify({'key': key}) : ''),
+ method: 'POST',
+ headers: {
+ 'content-type': 'application/json'
+ }
+ };
+ var req = _https.request(options, function(res) {
+ res.setEncoding('utf8');
+ res.on('data', function (d) {
+ d = JSON.parse(d);
+ if (callback) {
+ callback(d);
+ } else {
+ console.log(d.id || d.error);
+ }
+ });
+ });
- function _expand (url, callback) {
- if (!url) {
- console.error('Please specify a valid url.');
- return;
- }
+ req.on('error', function(e) { console.error(e); });
- if (typeof _url.parse(url).protocol === 'undefined') {
- url = 'http://' + url;
- }
-
- if (!callback) { callback = false; }
-
- var key = _getKey(),
- options = {
- host: 'www.googleapis.com',
- path: '/urlshortener/v1/url?' +
- (key ? _querystring.stringify({'key': key, 'shortUrl': url}) :
- _querystring.stringify({'shortUrl': url}))
- };
-
- _https.get(options, function(res) {
- res.setEncoding('utf8');
- res.on('data', function (d) {
- d = JSON.parse(d);
- if (callback) {
- callback(d);
- } else {
- console.log(d.longUrl || d.error);
- }
- });
-
- }).on('error', function(e) {
- console.error(e);
- });
- }
-
- return {
- 'shorten': _shorten,
- 'expand': _expand,
- 'setKey': _setKey
- };
- }());
+ req.write(JSON.stringify({'longUrl': url}));
+
+ req.end();
+ }
-exports.shorten = googl.shorten;
-exports.expand = googl.expand;
-exports.setKey = googl.setKey;
-if (args.length) {
- args.forEach(function (val, index, array) {
- if (typeof _url.parse(val).protocol === 'undefined') {
- val = 'http://' + val;
+ function _expand (url, callback) {
+ if (!url) {
+ console.error('Please specify a valid url.');
+ return;
}
-
- var hostname = (typeof _url.parse(val).hostname === 'undefined' ? '' :
- _url.parse(val).hostname);
-
- if (!hostname) {
- console.log('Invalid url: %s', val);
- return;
+
+ if (typeof _url.parse(url).protocol === 'undefined') {
+ url = 'http://' + url;
}
-
- if (hostname === 'goo.gl') {
- googl.expand(val, function (res) {
- console.log('%s -> %s', val, (res.longUrl || JSON.stringify(res)));
+
+ if (!callback) { callback = false; }
+
+ var key = _getKey(),
+ options = {
+ host: 'www.googleapis.com',
+ path: '/urlshortener/v1/url?' +
+ (key ? _querystring.stringify({'key': key, 'shortUrl': url}) :
+ _querystring.stringify({'shortUrl': url}))
+ };
+
+ _https.get(options, function(res) {
+ res.setEncoding('utf8');
+ res.on('data', function (d) {
+ d = JSON.parse(d);
+ if (callback) {
+ callback(d);
+ } else {
+ console.log(d.longUrl || d.error);
+ }
});
- return;
- }
-
- googl.shorten(val, function (res) {
- console.log('%s -> %s', val, (res.id || JSON.stringify(res)));
- });
- });
-}
+
+ }).on('error', function(e) {
+ console.error(e);
+ });
+ }
+
+ return {
+ 'shorten': _shorten,
+ 'expand': _expand,
+ 'setKey': _setKey
+ };
+}());
+
+module.exports = googl;
View
@@ -15,5 +15,5 @@
"dependencies": {},
"devDependencies": {},
"main": "lib/googl.js",
- "bin": { "goo.gl": "./lib/googl.js" }
+ "bin": { "goo.gl": "cli.js" }
}

0 comments on commit 970c19b

Please sign in to comment.