Build to Learn – socialmedia.js – Case Study
6 minutes read
socialmedia.js (Socialmedia) is a very small wrapper around the SDKs of popular social media platforms. It makes it easier to load whichever SDKs you need and use their native methods. I built it for three reasons:
- Shorter, cleaner code for using several social media SDKs in a web app
- Reusable code for my daily workflow
- To learn by building a library
Think of all the code it takes just to load the Facebook SDK into a web app, plus the chunks you need everywhere you call its methods. Socialmedia makes that cleaner and easier, and it fits into any frontend workflow.
Building and maintaining it taught me CoffeeScript. I liked it so much that the library's source has been in CoffeeScript ever since. I also learned to set up a better workflow with Grunt, Bower and Node/npm, and, most of all, how to test JavaScript.
I also got to learn JavaScript itself properly. Objects, prototypes, closures, debugging and modules had my full attention for a while. I'm glad to see Socialmedia getting stronger with each release, and I'm still learning.
So how does Socialmedia help a project? Below I go through installing and setting it up, comparing the native way with the Socialmedia way.
At the time of writing, Socialmedia supports four platforms:
- Google+
The aim is to set up an SDK with as little code as possible and, where it can, offer an API with fewer options and more readable names.
Facebook SDK
To use the Facebook SDK in your web app, you'd normally need this chunk of code from the Facebook developer docs.
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 'your-app-id',
xfbml: true,
version: 'v2.3',
});
};
(function (d, s, id) {
var js,
fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = '//connect.facebook.net/en_US/sdk.js';
fjs.parentNode.insertBefore(js, fjs);
})(document, 'script', 'facebook-jssdk');
</script>
With Socialmedia:
<script src="path/to/socialmedia.min.js" type="text/javascript"></script>
<script>
var facebook = new Socialmedia.Facebook({
appid: 'your-app-id',
});
</script>
Once that's done, your app has all the SDK's native objects and methods, like the FB object, plus helper methods that wrap some of the most used native ones. Some of the helpers (full API reference):
facebook.AddToPage()
facebook.Invite(...)
facebook.Share(...)
facebook.ShareOpenGraph(...)
facebook.Send(...)
facebook.Pay(...)
facebook.Feed(...)
Examples
This site already uses Socialmedia, so here are two live examples.
Example 1
- This button lets you invite your Facebook friends to read this post. If you're not logged in to Facebook, it asks you to log in first.
The code:
<button class="btn btn-sm btn-primary" type="button" id="inviteFriends">Invite friends to read article</button>
<script type="text/javascript">
var inviteFriends = document.querySelector('#inviteFriends');
if (inviteFriends) {
inviteFriends.addEventListener(
'click',
function (e) {
facebook.Invite({
title: 'Invited friends to read this article',
message: document.title,
});
},
false
);
}
</script>
Example 2
- This button fetches your basic Facebook information and shows it below. If you're not logged in, or haven't given the app permission, it asks you to log in and allow it first.
Nothing is collected or saved. All requests and responses are live. Once you've allowed the permissions, Facebook won't ask again. To remove them, delete the app from your Facebook app settings.
The code:
<button class="btn btn-sm btn-primary" type="button" id="getInfo">Get basic info from Facebook</button>
<textarea
name="infoBox"
id="infoBox"
class="form-control"
rows="5"
style="resize: none;"
placeholder="Info will appear here"
disabled
></textarea>
<script>
var getInfo = document.querySelector('#getInfo');
if (getInfo) {
getInfo.addEventListener(
'click',
function (e) {
FB.getLoginStatus(function (response) {
if (response && response.status === 'connected') {
FB.api('/me', getUserInfo);
getInfo.style.display = 'none';
} else {
FB.login(function (response) {
if (response && response.status === 'connected') {
FB.api('/me', getUserInfo);
getInfo.style.display = 'none';
}
});
}
});
},
false
);
function getUserInfo(info) {
var infoBox = document.querySelector('#infoBox');
infoBox.innerHTML = JSON.stringify(info, null, 2);
}
}
</script>
The other platforms work the same way.
Twitter SDK
Setup and a few of the methods (full API reference):
<script src="path/to/socialmedia.min.js" type="text/javascript"></script>
<script>
var twitter = new Socialmedia.Twitter();
</script>
// Exposes following API to use with Twitter SDK: twitter.Tweet() twitter.Hashtag() twitter.Mention()
Google+ SDK
Setup and a few of the methods (full API reference):
<script src="path/to/socialmedia.min.js" type="text/javascript"></script>
<script>
var gplus = new Socialmedia.GooglePlus();
</script>
// Exposes following API to use with Google+ SDK: gplus.Share()
Pinterest SDK
Setup and a few of the methods (full API reference):
<script src="path/to/socialmedia.min.js" type="text/javascript"></script>
<script>
var pinterest = new Socialmedia.Pinterest();
</script>
// Exposes following API to use withPinterest SDK: pinterest.Pinit()
Socialmedia is stable for what it was built to do, thanks to test driven development (TDD), and heavy use in production projects, simple and complex, has made it more so.
Along the way it broke in places I'd never have thought of. Every failure taught me something and made it more stable. You may still find bugs, and I'd love to hear about them, along with any fixes. That's what open source is for.
The documentation is a wiki, so anyone can improve it. It covers everything from installation and setup to the API reference.
So, what are you going to build?