each example: weather_forecast.js
This example queries the Yahoo! Weather RSS feed for locations in a list of major cities, displaying the returned weather conditions for each.
The state machines only state, MajorCityWeather, uses the each plug-in to iterate over a list of WOEID (Where On Earth ID) codes representing the city locations. At each iteration the yahooapis.request function is called with arguments generated by the fnArgs function.
When a response from any RSS request is received the iterator function is called which simply dumps the returned data to the console. The par parameter is used to set the maximum number of requests made in parallel to no more than 6.
var yahooapis = require('../../weather_yahooapis');
function weatherForecast (fire, woeidArray) {
this.startState = 'MajorCityWeather';
this.states = {
MajorCityWeather: {
each: {
fn: 'request',
fnArgs: function (woied) {
return { 'w': woied, 'u': 'c' };
},
iterator: function (itr, err, data) {
if (err) {
console.error(err);
} else {
console.log('%s', data);
}
},
par: 6
},
actions: {
'.done': '@exit'
}
}
};
};
weatherForecast.defaults = {
imports: { request: yahooapis.request },
args: [ yahooapis.majorCities ]
};
module.exports = weatherForecast;
Example Output
The source code for the example can be found in the file examples/plugins/each/weather_forecast.js and run using the ignite command line helper application as follows:
$ ./bin/ignite examples/plugins/each/weather_forecast.js Running examples/plugins/each/weather_forecast.js London, United Kingdom: 18C Partly Cloudy Paris, France: 20C Mostly Cloudy Mosco, Mexico: 16C Mostly Cloudy Beijing, China: 26C Fair Tokyo, Japan: 29C Mostly Cloudy Mumbai, India: 31C Haze San Fransisco, Spain: 27C Partly Cloudy New York, United States: 23C Partly Cloudy Barcelona, Spain: 27C Partly Cloudy Texas, United States: 21C Fair Seoul, South Korea: 27C Cloudy Sao Paulo, Brazil: 11C Fair Jakarta, Indonesia: 29C Partly Cloudy Karachi, Pakistan: 33C Mostly Cloudy Istanbul, Turkey: 28C Partly Cloudy Mexico, Mexico: 16C Mostly Cloudy Shanghai, China: 30C Mostly Cloudy Bangkok, Thailand: 31C Light Rain New Delhi, India: 31C Fog Hong Kong, Hong Kong: 33C Partly Cloudy run: examples/plugins/each/weather_forecast.js Exited with no error. $ _
See
- source: each API