Skip to content
Browse files

[imp] plugin implementation (experimental)

  • Loading branch information...
1 parent f98087b commit 770ec216e4f22416460197d31b33d2779ff3401f @yssk22 committed Nov 10, 2010
Showing with 49 additions and 4 deletions.
  1. +1 −1 README.md
  2. +1 −1 examples/echo.js
  3. +1 −1 examples/match.js
  4. +17 −0 examples/plugin.js
  5. +6 −0 lib/plugins/debug.js
  6. +23 −1 lib/twbot.js
View
2 README.md
@@ -56,7 +56,7 @@ The first argument accepts String or RegExp, which are compared with the tweet t
### Starting event loop
-A bot instance currently uses only UserStream API. To start
+A bot instance currently uses only UserStream API. To start streaming event loop, execute as follows:
bot.startUserStream();
View
2 examples/echo.js
@@ -4,7 +4,7 @@
* example: echo bot
*
*/
-var TwBot = require('../lib/bot').TwBot;
+var TwBot = require('../lib/twbot').TwBot;
var bot = new TwBot({
consumerKey: process.argv[2],
consumerSecret: process.argv[3],
View
2 examples/match.js
@@ -5,7 +5,7 @@
*
*/
var cat = require('http').cat;
-var TwBot = require('../lib/bot').TwBot;
+var TwBot = require('../lib/twbot').TwBot;
var bot = new TwBot({
consumerKey: process.argv[2],
consumerSecret: process.argv[3],
View
17 examples/plugin.js
@@ -0,0 +1,17 @@
+#!/usr/bin/env node
+/**
+ *
+ * example: plugin usage
+ *
+ */
+var TwBot = require('../lib/twbot').TwBot;
+var bot = new TwBot({
+ consumerKey: process.argv[2],
+ consumerSecret: process.argv[3],
+ accessKey: process.argv[4],
+ accessSecret: process.argv[5]
+});
+
+// debug pluin dumps all data event to stderr.
+bot.loadPlugin('../lib/plugins/debug');
+bot.startUserStream();
View
6 lib/plugins/debug.js
@@ -0,0 +1,6 @@
+exports.events = [];
+exports.events.push(
+ ['data', function(data){
+ console.error(data);
+ }]
+);
View
24 lib/twbot.js
@@ -36,6 +36,7 @@ function TwBot(auth, options){
}
this._options = options;
this._account = null;
+ this._plugins = {};
}
util.inherits(TwBot, EventEmitter);
@@ -68,7 +69,28 @@ TwBot.prototype.match = function(condition, fun){
}
}
});
-}
+};
+
+TwBot.prototype.loadPlugin = function(/* arguments */){
+ var self = this;
+ [].slice.apply(arguments).forEach(function(path){
+ var modpath = require.resolve(path);
+ if( self._plugins[modpath] === undefined ){
+ var mod = require(path);
+ self._plugins[modpath] = mod;
+ if( mod.events ){
+ mod.events.forEach(function(elm){
+ self.on(elm[0], elm[1]);
+ });
+ }
+ if( mod.maches ){
+ mod.maches.forEach(function(elm){
+ self.matches(elm[0], elm[1]);
+ });
+ }
+ }
+ });
+};
TwBot.prototype.startUserStream = function(params){
var self = this;

0 comments on commit 770ec21

Please sign in to comment.
Something went wrong with that request. Please try again.