Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tanepiper committed Nov 15, 2011
1 parent bca5cb9 commit 07ff4c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*.log
*.tpm
node_module
14 changes: 9 additions & 5 deletions bitly.js
Expand Up @@ -49,20 +49,23 @@ Bitly.prototype._generateNiceUrl = function(query, method) {
* @return {void}
*/
Bitly.prototype._doRequest = function(request_query, cb) {
var scope = this;
// Pass the requested URL as an object to the get request
http.get(request_query, function(res) {
var data = [];
res
.on('data', function(chunk) { data.push(chunk); })
.on('end', function() {
var urldata = data.join('');
var result = JSON.parse(urldata);
return cb(null, result);
var urldata = data.join('').trim();
var result;
try {
result = JSON.parse(urldata);
} catch (exp) {
result = {'status_code': 500, 'status_text': 'JSON Parse Failed'}
}
cb(null, result);
});
})
.on('error', function(e) {
console.log("Got http get error: " + e.message);
callback(e);
});
};
Expand Down Expand Up @@ -93,6 +96,7 @@ Bitly.prototype.shorten = function(longUrl, cb) {
longUrl: longUrl,
domain: this.config.domain
};

var request_query = this._generateNiceUrl(query, 'shorten');
this._doRequest(request_query, cb);
};
Expand Down

0 comments on commit 07ff4c9

Please sign in to comment.