(function()
{
    "use strict";

    var math = require('mathjs');
    var Twit = require('twit');
    var Q = require('q');
    var R = require('ramda');
    var request = require('request');
    var parseString = require('xml2js').parseString;

    var words = require('./jzx-dict').words;

    var getMeaning = function(word)
    {
        var deferred = Q.defer();

        // / This function grabs the definition of a word in XML format.

        // function grab_xml_definition ($word, $ref, $key)
        //     {   $uri = "http://www.dictionaryapi.com/api/v1/references/" . urlencode($ref) . "/xml/" .
        //                     urlencode($word) . "?key=" . urlencode($key);
        //         return file_get_contents($uri);
        //     };

        // $xdef = grab_xml_definition("test", "collegiate", "9b40c328-3b86-46a7-9927-c27f8138276d");

        var url = 'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/' + word + '?key=9b40c328-3b86-46a7-9927-c27f8138276d';

        request(url, function(error, response, xml)
        {
            if (!error && response.statusCode === 200)
            {
                parseString(xml, function(err, result)
                {
                    deferred.resolve(result);
                });
            }
        });

        return deferred.promise;
    };

    var tweet = function(text)
    {
        var T = new Twit(
        {
            // jshint ignore:start
            consumer_key: process.env.twitterConsumerKey,
            consumer_secret: process.env.twitterConsumerSecret,
            access_token: process.env.twitterAccessToken,
            access_token_secret: process.env.twitterAccessTokenSecret
                // jshint ignore:end
        });

        T.post('statuses/update',
        {
            status: text
        }, function(err /*, data, response */ )
        {
            if (err)
            {
                console.log(err);
            }

            // console.log(data);
        });
    };

    // pick one at random
    var word = words[math.randomInt(0, words.length)];

    console.log(word);

    getMeaning(word).then(function(data)
    {
        // console.log('data', JSON.stringify(data, null, 4));

        var entries = data.entry_list; //jshint ignore:line

        var getDefinition = function(entry)
        {
            var def = [];

            if (entry.def)
            {
                var dt = entry.def[0].dt;

                def = R.map(function(d)
                {
                    // console.log(d, typeof d);

                    var result = '';

                    if (typeof d === "string")
                    {
                        result = d;
                    }
                    else if (d._)
                    {
                        if (d._ !== ':')
                        {
                            result += d._;
                        }

                        if (d.it)
                        {
                            result += ' ' + d.it[0];
                        }
                        else if (d.sx)
                        {
                            result += ' ' + d.sx[0];
                        }
                    }
                    else if (d.un)
                    {
                        result = d.un[0];
                    }

                    return result.replace(/^:/, '');

                }, dt);
            }

            return def;

        };

        var meaning = '';

        if (entries.suggestion)
        {
            // meaning = 'Did you mean: ' + R.join(', ', entries.suggestion);
        }
        else
        {
            var meanings = R.map(getDefinition, entries.entry);

            // remove blanks
            meanings = R.filter(function(w)
            {
                return !!w.replace(/\s/g, '');
            }, R.flatten(meanings));

            // pick min length
            var minlen = R.reduce(function(min, w)
            {
                return math.min(min, w.length);
            }, 1e6, meanings);

            meaning = R.find(function(w)
            {
                return w.length === minlen;
            }, meanings);

        }

        console.log(meaning);
        // tweet(word);
    });


}());

@@ -2092,25 +2092,23 @@
     
-console.log(word)
+// word = 'elk'
 ;%0A%0A 
@@ -2799,20 +2799,28 @@
      if 
+(R.trim
 (d._
+)
  !== ':'
@@ -3025,15 +3025,23 @@
 ' + 
+R.trim(
 d.it%5B0%5D
+)
 ;%0A  
@@ -3164,24 +3164,30 @@
  result 
-+
 = 
-' ' +
+d.sx%5B0%5D._ %7C%7C
  d.sx%5B0%5D
@@ -3380,21 +3380,31 @@
       re
-turn 
+sult =  R.trim(
 result.r
@@ -3419,19 +3419,100 @@
 %5E:/, '')
+)
 ;%0A%0A
+                    // console.log(result);%0A%0A                    return result;%0A
         
@@ -3852,16 +3852,36 @@
 e blanks
+ & too long to tweet
 %0A       
@@ -3960,28 +3960,36 @@
 n !!
-w.replace(/%5Cs/g, '')
+R.trim(w) && w.length %3C= 130
 ;%0A  
@@ -4040,138 +4040,58 @@
     
-// pick min length%0A            var minlen = R.reduce(function(min, w)%0A            %7B%0A                return math.min(min, w
+meaning = meanings%5Bmath.randomInt(meanings
 .length)
 ;%0A  
@@ -4086,16 +4086,17 @@
 .length)
+%5D
 ;%0A      
@@ -4101,29 +4101,33 @@
     
+%7D%0A%0A
     
-%7D, 1e6, meanings)
+    var msg = word
 ;%0A%0A 
@@ -4137,51 +4137,29 @@
     
-  
-  
+if (
 meaning
- = R.find(function(w)%0A  
+)%0A
         
   %7B%0A
@@ -4146,34 +4146,32 @@
 eaning)%0A        
-  
 %7B%0A              
@@ -4172,62 +4172,34 @@
     
-    return w.length === minlen;%0A            %7D,
+msg = msg + ': ' +
  meaning
 s);%0A
@@ -4198,12 +4198,9 @@
 ning
-s);%0A
+;
 %0A   
@@ -4232,38 +4232,31 @@
 og(m
-eanin
+s
 g);%0A
+%0A
        
- //
  tweet(
-word
+msg
 );%0A 
@@ -4262,16 +4262,14 @@
     %7D);%0A
-%0A%0A
 %7D());%0A

add meaning