/*
 * octofish
 * https://github.com/mattstyles/octofish
 *
 * Content Delivery Module for Viperfish
 * https://github.com/mattstyles/viperfish
 *
 * Copyright (c) 2013 Matt Styles
 * Licensed under the MIT license.
 */

// Add external dependencies
var _            = require( 'underscore'),
    GithubApi    = require( 'github' );

/* -----------------------------------------------------------------------
 *
 *     Export the public API
 *     These functions will be used to extend and overwrite core functions
 *
 * ----------------------------------------------------------------------- */
module.exports = {
    /**
     * getContent
     * gets the content from github repo set up in config
     * @param cb - the callback to fire on success - passes in the collected data
     */
    getContent: function( cb ) {
        octofish.github.repos.getContent( octofish.config.githubapi.contentOpts, function ( err, res ) {
            // Handle any error
            if (err) {
                console.log( 'An error occured whilst getting content from github - this should be dealt with properly' );
                console.log( 'Should be displaying a 404 here as the URL is probably invalid' );
                return;
            }

            var data = null;

            // Decode the data before sending on to the callback
            // Check for decoding an image
            if ( octofish.config.githubapi.contentOpts.path.match(/img/) ) {
                data = octofish.decodeGithubImageData( res.content )
            } else {
                data = octofish.decodeGithubData( res.content );
            }

            // If the operation to get content from github was successful then call the callback with only the content
            // This file should actually handle what happens next but for now delegate to a callback
            cb( data );
        });
    },

    /**
     * authGithub
     * Grabs the environment variables and uses them to authenticate with github
     */
    authorise: function() {
        var getAuth = function() {
            var auth = {};

            auth.type = 'basic';
            auth.username = process.env.GHusername || null;
            auth.password = process.env.GHpassword || null;

            if ( auth.password === null || auth.username === null ) {
                return null;
            } else {
                return auth;
            }
        };

        // Get the authentication data - use environment vars
        var auth = getAuth();

        // If authentication vars are valid then attempt to authenticate with github
        if ( auth ) {
            console.log( 'Got github credentials from process.env --- attempting to authenticate with github --- using basic authorisation' );
            octofish.github.authenticate( auth );
        } else {
            console.warn( 'No github credentials in environment vars --- Unauthorised access to github --- max rate limit of 60 reqs per hour' );
        }
    },
    /**
     * setPath
     * sets the path for retrieving a file from github
     * @param path
     */
    setContentPath: function( path ) {
        octofish.config.githubapi.contentOpts.path = path;
    }
};


/* -----------------------------------------------------------------------
 *
 *     Private octofish object
 *     Contains functions that are private to octofish so they are
 *     namespaced and don't interfere with core
 *
 * ----------------------------------------------------------------------- */
var octofish = octofish || {};

// Add the config to the octofish object
octofish.config = require( './octo-config' );

octofish = {

    config: octofish.config,

    // Set up githubapi
    github: new GithubApi( octofish.config.githubapi.apiOpts ),

    /**
     * decodes the data returned from a github api query
     * @param data - base64 encoded response from querying github for content
     * @returns buffer containing a string representation of the base64 encoded query data
     */
    decodeGithubData: function( data ) {
        return new Buffer(data, 'base64').toString('utf8');
    },

    /**
     * decodes github image data from base64 to binary
     * @param data - base64 encoded response from querying github for content
     * @return buffer containing a binary representation of the base64 encoded image
     */
    decodeGithubImageData: function( data ) {
        return new Buffer( data, 'base64').toString( 'binary' );
    }
};
@@ -812,16 +812,22 @@
 ion(
+ path,
  cb ) %7B%0A
     
@@ -818,24 +818,150 @@
 ath, cb ) %7B%0A
+        // Set the content path%0A        octofish.setContentPath( path );%0A%0A        // Use the content path to retrieve content%0A
         octo
@@ -1049,24 +1049,55 @@
 rr, res ) %7B%0A
+%0A            var data = null;%0A%0A
             
@@ -1135,24 +1135,95 @@
  if (err) %7B%0A
+                // Log the error - not a very useful log at the moment%0A
             
@@ -1367,71 +1367,155 @@
 g( '
-Should be displaying a 404 here as the URL is probably invalid'
+It is most likely a URL error with the path' );%0A%0A                // Call the callback with a null value to denote an error%0A                cb( data
  );%0A
@@ -1557,38 +1557,8 @@
  %7D%0A%0A
-            var data = null;%0A%0A
     
@@ -1934,48 +1934,29 @@
 the 
-operation to get content from github was
+content was collected
  suc
@@ -1962,16 +1962,18 @@
 ccessful
+ly
  then ca
@@ -1992,130 +1992,40 @@
 ack 
-with only the content%0A            // This file should actually handle what happens next but for now delegate to a callback
+and remember to pass in the data
 %0A   
@@ -3170,218 +3170,8 @@
   %7D%0A
-    %7D,%0A    /**%0A     * setPath%0A     * sets the path for retrieving a file from github%0A     * @param path%0A     */%0A    setContentPath: function( path ) %7B%0A        octofish.config.githubapi.contentOpts.path = path;%0A
     
@@ -3612,24 +3612,97 @@
 tofish = %7B%0A%0A
+    // Register the config object that was collected from octo-config.js%0A
     config: 
@@ -4431,32 +4431,32 @@
 ction( data ) %7B%0A
-
         return n
@@ -4500,20 +4500,231 @@
 'binary' );%0A
+    %7D,%0A%0A    /**%0A     * setPath%0A     * sets the path for retrieving a file from github%0A     * @param path%0A     */%0A    setContentPath: function( path ) %7B%0A        octofish.config.githubapi.contentOpts.path = path;%0A
     %7D%0A%7D;

Change to the API for collecting data