Last active
September 29, 2017 21:51
-
-
Save joshualambert/4515fbb849006df4ab1ca15ca2ddef0c to your computer and use it in GitHub Desktop.
Titanium BitLy CommonJS Library
This file contains 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
/* | |
* Used to interact with the Bit.LY API. | |
* Written by: Josh Lambert | |
* Twitter: @zettageek | |
* Email: [email protected] | |
* 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