Skip to content

Instantly share code, notes, and snippets.

@phillipharding
Created August 22, 2014 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipharding/14debb084dc37c480b13 to your computer and use it in GitHub Desktop.
Save phillipharding/14debb084dc37c480b13 to your computer and use it in GitHub Desktop.
Javascript module for shortening URLs using Bitly
(function($,window) {
"use strict";
window.pdogs = window.pdogs || {};
pdogs.urlshortener = function() {
var
_bitlyAccessToken = '<YOUR BITLY ACCESS_TOKEN HERE>',
_bitlyShortDomain = 'j.mp',
_module = { shorten: shorten };
return _module;
function shorten(longUrl, sync) {
var p = $.Deferred();
if (typeof longUrl === 'undefined' || !longUrl || !longUrl.length) {
p.reject({ error: 'Invalid/Empty URI' });
return p.promise();
}
var
encodedLongUrl = encodeURIComponent(decodeURIComponent(longUrl)),
params = 'access_token=' + _bitlyAccessToken
+ '&longUrl=' + encodedLongUrl
+ '&domain=' + _bitlyShortDomain
+ '&format=json',
r = {
type: 'GET',
url: 'https://api-ssl.bitly.com/v3/shorten?' + params,
headers: {
'Accept': 'application/json'
},
async: !sync
};
jQuery.support.cors = true;
$.ajax(r)
.done(function(response) {
if (response.status_code != 200) p.reject({ error: response.status_txt });
else p.resolve({ shortUrl: response.data.url, shortUrlImg: response.data.url + '.qrcode' });
})
.fail(function(xhr,textStatus, error) {
var m = error.message ? error.message : error;
p.reject({ error: 'bitly] ' + textStatus + ': ' + m });
});
return p.promise();
}
}();
})(jQuery,window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment