module.exports = (function(){
    /**
     * Import npm modules
     */
    const http = require('http')
    const fs = require('fs')
    /**
     * Constants
     */
    const FOOTBALL_HOST = 'api.football-data.org'
    const FOOTBALL_PATH = '/v1/soccerseasons/'
    const FOOTBALL_CREDENTIALS = loadCredentials(__dirname +  '/footballCredentials.js')
    /**
     * footballDb module API
     */
    return {
        'leagues': leagues,
        'leagueTable': leagueTable
    }

    function leagues(cb) {
    }

    function leagueTable(id, cb) {
        const path = FOOTBALL_PATH + id + '/leagueTable'
        const headers = FOOTBALL_CREDENTIALS
        httpGetAsJson(FOOTBALL_HOST, path, headers, (err, obj) => {
            if(err) return cb(err)
            if(obj.error) return cb(new Error("There is no League with id = " + id))
            if(!obj.standing) return cb(new Error("There is no Table for id = " + id))
            cb(null, new LeagueTable(id, obj))
        })
    }

    function LeagueTable(id, obj) {
        this.id = id
        this.caption = obj.leagueCaption
        this.teams  = obj.standing.map(std => new Team(std))
    }

    function Team(obj) {
        const path = obj._links.team.href.split('/')
        this.id = path[path.length - 1]
        this.position = obj.position
        this.name = obj.teamName
        this.points = obj.points
        this.goals = obj.goals
    }

    function httpGetAsJson(host, path, headers, cb) {
        options = {
            'host': host,
            'path': path
        }
        if(headers) options.headers = headers
        http.get(options, (resp) => {
            let res = ''
            resp.on('error', cb)
            resp.on('data', chunck => res += chunck.toString())
            resp.on('end', () => {
                cb(null, JSON.parse(res))
            })
        })    
    }

    function loadCredentials(file) {
        /**
         * Change it if you find a better way of synchronously 
         * check whether a file exists, or not !!!!!!
         */
        try {
            const stats = fs.statSync(file);
            // it existsSync
            return require(file)
        }
        catch(err) {
            // it does not existsSync
            return undefined
        } 
    }
})()
@@ -522,23 +522,503 @@
 (cb) %7B%0D%0A
+        const path = FOOTBALL_PATH%0D%0A        const headers = FOOTBALL_CREDENTIALS%0D%0A        httpGetAsJson(FOOTBALL_HOST, path, headers, (err, arr) =%3E %7B%0D%0A            if(err) return cb(err)%0D%0A            if(arr.error) return cb(new Error(%22No leagues !!!! You probably reached your request limit. Get your free API token from http://api.football-data.org/!!! --------- Message from football-data.org:%22 + arr.error))%0D%0A            cb(null, arr.map(item =%3E new League(item)))%0D%0A        %7D)%0D%0A
     %7D%0D%0A
-
 %0D%0A    fu
@@ -1491,32 +1491,268 @@
    %7D)%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Domain Entity -- League %0D%0A     */%0D%0A    function League(obj) %7B%0D%0A        this.id = obj.id%0D%0A        this.caption = obj.caption%0D%0A        this.year = obj.year%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Domain Entity -- LeagueTable %0D%0A     */%0D%0A
     function Lea
@@ -1899,32 +1899,81 @@
 std))%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Domain Entity -- Team %0D%0A     */%0D%0A
     function Tea
@@ -2216,32 +2216,102 @@
 goals%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Utility auxiliary function -- httpGetAsJson%0D%0A     */%0D%0A
     function htt
@@ -2757,32 +2757,104 @@
 )    %0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Utility auxiliary function -- loadCredentials%0D%0A     */%0D%0A
     function loa

Implement footballDb -- Add leagues() action method