Skip to content

Instantly share code, notes, and snippets.

@joshualambert
Last active September 29, 2017 21:51
Show Gist options
  • Save joshualambert/4515fbb849006df4ab1ca15ca2ddef0c to your computer and use it in GitHub Desktop.
Save joshualambert/4515fbb849006df4ab1ca15ca2ddef0c to your computer and use it in GitHub Desktop.
Titanium BitLy CommonJS Library
/*
* Used to interact with the Bit.LY API.
* Written by: Josh Lambert
* Twitter: @zettageek
* Email: hi@centrevilletech.com
* Version: 1.0
*/
var apiKey,
apiVersion = 'v3',
sslApiUrl = 'https://api-ssl.bitly.com';
exports.init = function (providedApiKey) {
apiKey = providedApiKey;
};
exports.shortenUrl = function (longUrl, callback) {
var self = this;
var webClient = Titanium.Network.createHTTPClient({
timeout: 15000
});
webClient.open("GET", sslApiUrl + '/' + apiVersion + '/shorten?access_token=' + apiKey + '&format=txt&longUrl=' + encodeURIComponent(longUrl));
webClient.send();
// Handles a success being returned.
webClient.onload = function () {
console.log('(bitly) onload, ' + this.status + ', ' + this.responseText);
if (this.status === 200) {
callback(true, this.responseText);
} else {
callback(false, this.responseText);
}
};
// Handles an error being returned.
webClient.onerror = function () {
console.log('(bitly) Status: ' + this.status);
console.log('(bitly) Response: ' + this.responseText);
// Second, handle an unknown error.
callback(false, this.responseText);
};
};
/*
* EOF
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment