Skip to content

Instantly share code, notes, and snippets.

@boydlee
Created September 21, 2012 16:52
Show Gist options
  • Save boydlee/3762608 to your computer and use it in GitHub Desktop.
Save boydlee/3762608 to your computer and use it in GitHub Desktop.
Imgur.com image upload commonjs module for Titanium
/*
* IMGUR.COM Anonymous image upload API
*/
Imgur = function(){
this.key = 'XXXXXXXXXX'; //get one free at imgur.com
this.httpEndpoint = 'http://api.imgur.com/2/';
};
/*
* Upload method, sample usage:
var IMGUR = require('/imgur');
var Imgur = new IMGUR.Imgur();
Imgur.upload(media, function(response){
alert('YOUR IMGUR.COM IMAGE LINK IS: ' + response);
});
*/
Imgur.prototype.upload = function(media, response){
var uri = this.httpEndpoint + 'upload.json';
var xhr = Ti.Network.createHTTPClient();
xhr.open("POST", uri); // Boooom!
xhr.onerror = function() {
response(false);
};
xhr.onload = function() {
// Big win!
// The URL of the image is:
var _link = JSON.parse(this.responseText).upload.links.imgur_page;
Ti.API.info(_link);
response(_link);
};
//send the media and key
xhr.send({
image: media,
key: this.key
});
};
//finally
exports.Imgur = Imgur;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment