/**
 *
 * Copyright (C) <2014> <Maxime Bouchenoire>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

var http = require('request');

TWITCH_API = 'https://api.twitch.tv/kraken/';

var bufferizedEmoticons = [];

function Twitch() {
    // do nothing
}

/**
 * Returns a list of 'stream' resource based on this documentation:
 * https://github.com/justintv/Twitch-API/blob/master/v3_resources/streams.md
 *
 * @param args args.number (optional): number of streams to retrieve
 *             args.offset (optional): offset from which you want to start to get the streams list
 * @param callback called when the streams have been retrieved, with format (err, streams)
 * @returns {boolean} false if arguments are missing
 */
Twitch.prototype.streams = function (args, callback) {
    if (typeof args == 'function') callback = args;
    if (!callback || typeof callback != 'function') return false;

    var number = args.number || 25;
    var offset = args.offset || 0;

    if (offset > number) return callback(new Error('Streams offset is superior to the number to retrieve!'));

    //TODO handle more than 25 streams (twitch api returns maximum 25 streams), have to play with offset

    return retrieveResource(TWITCH_API + 'streams?number=' + number + '&offset=' + offset, function (err, body) {
        var streams = body.streams;
        if (!streams) err = new Error('Failed to parse the resource in order to get the streams list!');
        callback(err, streams);
    });
}

/**
 * Returns a list of 'emoticons' resource based on this documentation:
 * https://github.com/justintv/Twitch-API/blob/master/v3_resources/chat.md#get-chatemoticons
 *
 * Note that this function bufferize emoticons from a channel, as they rarely change.
 *
 * @param channel the twitch.tv channel
 * @param callback called when the emoticons have been retrieved, with format (err, streams)
 * @returns {boolean} false if arguments are missing
 */
Twitch.prototype.emoticons = function (channel, callback) {
    if (!channel || typeof channel != 'string') return false;
    if (!callback || typeof callback != 'function') return false;

    var emoticons = getBufferizedEmoticons(channel);

    if (emoticons != null) {
        return callback(null, emoticons);
    }

    return retrieveResource(TWITCH_API + 'chat/' + channel + '/emoticons', function (err, body) {
        emoticons = body.emoticons;
        if (!emoticons) err = new Error('Failed to parse the resource in order to get the emoticons list!');

        bufferizedEmoticons.push({channel: channel, emoticons: emoticons});
        callback(err, emoticons);
    });
}

function retrieveResource(url, callback) {
    if (!url || typeof url != 'string') return false;
    if (!callback || typeof callback != 'function') return false;

    http.get({
        url: url
    }, function (err, response, body) {
        if (err) {
            callback(err);
        } else {
            var ex = null;

            try {
                body = JSON.parse(body);
            } catch(err) {
                ex = err;
            }

            if (callback) callback(ex, body);
        }
    })
}

function getBufferizedEmoticons(channel) {
    var emoticons = null;

    bufferizedEmoticons.forEach(function (buffer) {
        if (buffer.channel == channel) {
            emoticons = buffer.emoticons;
        }
    });

    return emoticons;
}

module.exports = Twitch;
@@ -815,16 +815,46 @@
 quest');
+%0Avar async = require('async');
 %0A%0ATWITCH
@@ -1959,22 +1959,18 @@
 ber=' + 
-number
+25
  + '&off
@@ -1977,22 +1977,17 @@
 set=' + 
-offset
+0
 , functi
@@ -2213,24 +2213,1136 @@
 ist of '
+game' resource based on this documentation:%0A * https://github.com/justintv/Twitch-API/blob/master/v3_resources/games.md%0A *%0A * @param args args.number (optional): number of streams to retrieve%0A *             args.offset (optional): offset from which you want to start to get the streams list%0A * @param callback called when the streams have been retrieved, with format (err, streams)%0A * @returns %7Bboolean%7D false if arguments are missing%0A */%0ATwitch.prototype.games = function(args, callback) %7B%0A    if (typeof args == 'function') callback = args;%0A    if (!callback %7C%7C typeof callback != 'function') return false;%0A%0A    var number = args.number %7C%7C 100;%0A    var offset = args.offset %7C%7C 0;%0A%0A    if (offset %3E number) return callback(new Error('Games offset is superior to the number to retrieve!'));%0A%0A    return retrieveResource(TWITCH_API + 'games?number=' + number + '&offset=' + offset, function (err, body) %7B%0A        var streams = body.streams;%0A        if (!streams) err = new Error('Failed to parse the resource in order to get the games list!');%0A        callback(err, streams);%0A    %7D);%0A%7D%0A%0A/**%0A * Returns a list of '
 emoticon
 s' resou
@@ -3333,17 +3333,16 @@
 emoticon
-s
 ' resour
@@ -4681,32 +4681,62 @@
      if (err) %7B%0A
+            console.dir(err);%0A
             call
@@ -4846,24 +4846,24 @@
 arse(body);%0A
-
             
@@ -4869,32 +4869,66 @@
  %7D catch(err) %7B%0A
+                console.dir(err);%0A
                 

add the games retrieving feature