Skip to content

Instantly share code, notes, and snippets.

Created March 7, 2016 18:39
Show Gist options
  • Save anonymous/5e68e91d661f4991e1d1 to your computer and use it in GitHub Desktop.
Save anonymous/5e68e91d661f4991e1d1 to your computer and use it in GitHub Desktop.
(function() {
/**
* Main TweetEmbed object with settings
* and helper functions to build "Tweet this"
* links around desired text.
*
* @type {Object}
*/
var TweetEmbed = {
// Settings ---------- //
settings: {
tweets: $('.tweet-embed'),
tweet_url: 'http://twitter.com/home/?status=',
tweet_source: '@handle',
post_url: window.location.href,
bitly_login: 'login_here',
bitly_key: 'key_here'
},
// Initialization ---------- //
init: function() {
s = this.settings;
this.tweetSweep();
this.getShortUrl(s.post_url);
},
// URL Constructor ---------- //
tweetSweep: function(shorturl) {
if (!shorturl) {
shorturl = '';
}
for (var i = 0; i < s.tweets.length; i++) {
var tweet = $(s.tweets[i]);
var tweet_text = $(tweet).text();
// Build the URL
$(tweet).attr({
href: s.tweet_url + tweet_text + ' ' + s.tweet_source + ' ' + shorturl,
target: '_blank'
});
}
},
// URL Shortening via Bitly API ---------- //
getShortUrl: function(url) {
var accessToken = s.bitly_key;
var url = 'https://api-ssl.bitly.com/v3/shorten?access_token=' + accessToken + '&longUrl=' + encodeURIComponent(url);
var self = this;
$.getJSON(url, function(response) {
var short = response.data.url;
self.tweetSweep(short);
});
}
}; // end TweetEmbed
// Fire off TweetEmbed's initialization
TweetEmbed.init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment