Twitter reader: lnugExample2.js
A Twitter reader example used as a more complex example during our LNUG presentation. For more details see the presentation slides.
var _ = require('underscore') ;
var colors = require('colors') ;
function lnugExample (fire, name) {
// Closure: local variables are available across states:
var languages = {} ;
return {
startState: "Welcome",
states: {
"Welcome": {
entry: function () {
console.log("Welcome. I'm searching for: '%s'", name) ;
return "GetTweets" ;
}
},
"GetTweets": {
entry: function () {
fire.request("http://search.twitter.com/search.json?"+
"q="+name+
"&result_type=mixed") ;
},
timeout: 2000,
actions: {
"request.err": "@error",
"request.done": "HandleResult",
"timeout": function () {
console.log("Sorry, but Twitter are taking a long time to reply.") ;
return "@exit" ;
}
}
},
"HandleResult": {
entry: function (response, body) {
body = JSON.parse(body) ;
console.log("Completed in: ", body.completed_in) ;
_.each(body.results, function (result) {
languages[result.iso_language_code] =
(languages[result.iso_language_code] || 0) + 1 ;
}) ;
if (body.results.length) {
return ["GetProfileImages", body.results] ;
} else {
return "@exit" ;
}
}
},
"GetProfileImages": {
map: {
fn: "request.get",
fnArgs: function (result) {
return { url: result.profile_image_url } ;
},
iterator: function (response, body) {
return body.toString() ;
}
},
actions: {
"map.done": function (images, results, errors) {
_.each(results, function (result, index) {
var image = errors[index] ? "" : images[index] ;
var fromTo = result.from_user.bold.red + " -> " ;
fromTo += (result.to_user ? result.to_user :
"<span style="color: blue;">[Everyone]</span>").blue ;
console.log("%d: %s Img len: %d\n '%s'",
index, fromTo, image.length, result.text.green) ;
}) ;
return "DumpLanguages" ;
}
}
},
"DumpLanguages": {
entry: function () {
console.log("\nLanguage counts:") ;
_.each(languages, function (count, lang) {
console.log(" %s: %d", lang, count) ;
}) ;
return "@exit" ;
}
}
}
} ;
}
lnugExample.defaults = {
imports: {
request: require('request')
},
args: [ '#lnug' ]
};
module.exports = lnugExample;
Example Output
The source code for the example can be found in the file examples/lnug/lnugExample2.js and run using the ignite command line helper application as follows:
$ ./bin/ignite examples/lnug/lnugExample2.js Running examples/lnug/lnugExample2.js Welcome. I'm searching for: '#lnug' Completed in: 0.192 0: lancestewart -> [Everyone] Img len: 900 'RT @ikershaw: Looking for Node.js developers to recruit at London node.js user meetup #lnug @jlsync is here with his award winning charity hack app!' 1: chrisspranklen -> [Everyone] Img len: 935 'The #node.js Developer would be working (12 wk contract!) for an app development company (London). anyone looking - 02032061924? #lnug #node' 2: teabass -> [Everyone] Img len: 3769 'I always tell the #lnug google group about tickets before anyone else, if you want to ensure you get one then join http://t.co/yYMfVszj' 3: teabass -> [Everyone] Img len: 3769 'RT @forwardtek: All the slides from #lnug on wednesday have been added to the @lanyrd page and the videos will be added next week: http://t.co/LVjtN8j2' 4: Erica_Bennett -> [Everyone] Img len: 1431 'RT @rtweed: Cash prizes available in #GlobalsDB competition. 1st one this Saturday: http://t.co/QRdhROcP http://t.co/UZ7raFlQ #lnug #nodejs' 5: rtweed -> [Everyone] Img len: 6779 'Cash prizes available in #GlobalsDB competition. 1st one this Saturday: http://t.co/QRdhROcP http://t.co/UZ7raFlQ #lnug #nodejs' 6: detillen -> [Everyone] Img len: 1332 'And, to complete the set, here are my slides from #lnug yesterday http://t.co/06RIz7oZ - remember state machines rock!' 7: Cloud9IDE -> [Everyone] Img len: 3479 'RT @mikedeboer: My slides from yesterday's presentation at #lnug: http://t.co/bTZha4DG Hope you'll find it useful during development!' 8: chrissywelsh -> [Everyone] Img len: 3236 'RT @mikedeboer: My slides from yesterday's presentation at #lnug: http://t.co/bTZha4DG Hope you'll find it useful during development!' 9: mikedeboer -> [Everyone] Img len: 1650 'My slides from yesterday's presentation at #lnug: http://t.co/bTZha4DG Hope you'll find it useful during development!' 10: podviaznikov -> [Everyone] Img len: 823 'RT @seddonandrew: Slides from my 'massive-git' #lnug talk are up at http://t.co/iFlIbE2k' 11: mikedeboer -> [Everyone] Img len: 1650 'Back in #AMS from a great #lnug in London. Thanks for having me guys! Met a lot of talented people. Slides of my talk will be online soon!' 12: seddonandrew -> bjnortier Img len: 4127 '@bjnortier haha thanks. I name dropped @shapesmith at #lnug last night so hopefully you'll get some people checking it out :-)' 13: patrickhamann -> [Everyone] Img len: 4726 'RT @seddonandrew: Slides from my 'massive-git' #lnug talk are up at http://t.co/iFlIbE2k' 14: teabass -> [Everyone] Img len: 3769 'RT @seddonandrew: Slides from my 'massive-git' #lnug talk are up at http://t.co/iFlIbE2k'
Language counts: en: 15 run: examples/lnug/lnugExample2.js Exited with no error.