Skip to content

Instantly share code, notes, and snippets.

@VandeurenGlenn
Created March 18, 2015 20:07
Show Gist options
  • Save VandeurenGlenn/d31ca4ce67ea04a10945 to your computer and use it in GitHub Desktop.
Save VandeurenGlenn/d31ca4ce67ea04a10945 to your computer and use it in GitHub Desktop.
twitch-api
<link rel="import" href="..\..\bower_components/polymer/polymer.html">
<polymer-element name="twitch-api">
<template>
<link rel="stylesheet" href="twitch-api.css" attributes="clientId">
</template>
<script>
(function () {
Polymer({
twitchUrl: 'https://api.twitch.tv/kraken/',
clientId: 'f199msiwm0ltzyn892vlh3me1gx6bjq',
twitch: {},
SESSION_KEY: 'twitch_oauth_session',
ready: function() {
this.twitch = {
twitchUrl: this.twitchUrl,
_config: {},
_storage: {},
extend: function(src) {
$.extend(this.twitch, src);
}
}
this.init();
},
init: function(callback) {
if(!this.clientId) {
console.log('client id: not found');
}
this.twitch._config.clientId = this.clientId;
this.initSession();
if (typeof callback === 'function') {
twitch.getStatus(callback);
console.log('callback');
}
this.twitch.extend({
init : this.init
});
console.log('initialized');
},
initSession: function() {
var storedSession;
this.twitch._config.session = {};
// For browsers that do not have the JSON native object,
// [JSON.js](http://bestiejs.github.com/json3) will work
// as a drop-in implementation.
if(window.JSON) {
storedSession = this.twitch._storage.getItem(this.SESSION_KEY);
if(storedSession) {
try {
this.twitch._config.session = JSON.parse(storedSession);
} catch(e) {
}
}
}
// If we're on a page with an access token, it's probably a
// return uri for an authorization attempt. Overwrite with
// the new params if page has them.
if(document.location.hash.match(/acces_toke=(\w+)/)) {
this.twitch._config.session = this.parseFragment();
// Persist to session storage on browsers that support it
// otherwise use cookies.
if(window.JSON) {
this.twitch._storage.setItem(SESSION_KEY, JSON.stringify(this.twitch._config.session));
}
}
getStatus(function(err, status) {
if (status.authenticated) {
this.twitch.events.emit('auth.login');
}
});
this.twitch.extend({
_initSession: this.initSession,
_parseFragment: this.parseFragment,
getToken: this.getToken,
getStatus: this.getStatus,
login: this.login,
logout: this.logout
});
}
});
})();
</script>
</polymer-element>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment