Skip to content
Browse files

Listo para publicacion

  • Loading branch information...
1 parent 052da7b commit 6beb24ff4e88a88d9904f91645c9da4393b6d276 @alejonext committed Feb 5, 2013
Showing with 126 additions and 85 deletions.
  1. +1 −5 bin/run
  2. +24 −31 bin/server.js
  3. +24 −0 examples/config.json
  4. +24 −1 lib/config.js
  5. +32 −28 lib/db.js
  6. +4 −2 lib/twitter.js
  7. +0 −12 package.json
  8. +17 −6 routes/index.js
View
6 bin/run
@@ -5,11 +5,7 @@
var path = require('path'),
dir = path.join(__dirname, '..'),
- sys = require('sys'),
- server = require('./app.js'),
program = require('commander'),
- _ = require('underscore'),
- os = require('os'),
lib = require( path.join(dir, 'lib' ) );
program.name = lib.config["name"] ;
@@ -18,14 +14,14 @@ program.version( lib.config["version"] );
program
.option('-w, --web-server [port]', 'Servidor web y el puerto')
- .option('-s, --stream-twitter', 'Correr el stream en tuiter' )
.option('-c, --configuration [path]', 'La configuracion del usuario en twiter en json' )
config = lib.config( path.resolve( program["configuration"] ) );
program.on('--help', function(){
console.log( config["help"].join('\n') );
});
server.set( parserFloat( program["web-server"] ) || 3000 )
+ server = require('./app.js')( config );
require('http').createServer( server ).listen( server.get('port'), function() {
console.log("Express server listening on port " + server.get('port'));
});
View
55 bin/server.js
@@ -7,35 +7,28 @@ var express = require('express'),
var app = express();
-app.configure(function() {
- app.set('port', process.env.PORT || 3000);
- app.set('views', __dirname + '/views');
- app.set('view engine', 'jade');
- app.use(express.favicon());
- app.use(express.logger('dev'));
- app.use(express.bodyParser());
- app.use(express.methodOverride());
- app.use(express.cookieParser('your secret here'));
- app.use(express.session());
- app.use(app.router);
- app.use(require('less-middleware')({
- src : __dirname + '/public'
- }));
- app.use(express.static(path.join(__dirname, 'public')));
-});
-
-app.configure('development', function() {
- app.use(express.errorHandler());
-});
-
-app.get('/', routes.index);
-app.post('/user', routes.user);
-app.post('/madr', routes.madriar);
-
-module.exports = app;
-
-if (!module.parent) {
- require('http').createServer(app).listen(app.get('port'), function() {
- console.log("Express server listening on port " + app.get('port'));
+module.exports = function(data){
+ app.configure(function() {
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.favicon());
+ app.use(express.logger(data.get("status").substring(3)));
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(express.cookieParser(data.get("pass")));
+ app.use(express.session());
+ app.use(app.router);
+ app.use(require('less-middleware')({
+ src : __dirname + '/public'
+ }));
+ app.use(express.static(path.join(__dirname, 'public')));
+ });
+ route = new routes(data);
+ app.configure(data.get("status"), function() {
+ app.use(express.errorHandler());
});
-};
+ app.get('/', route.index);
+ app.post('/user', route.user);
+ app.post('/madr', route.madriar);
+ return app;
+} ;
View
24 examples/config.json
@@ -0,0 +1,24 @@
+{
+ "twitter" : {
+ "consumer_key" : "YourKey",
+ "consumer_secret" : "YourKeyScret",
+ "access_token" : "TheToken",
+ "access_token_secret" : "TokenSecret"
+ },
+ "db" : {
+ "host" : "localhost",
+ "path" : "madrazos",
+ "type" : "mongodb",
+ "port" : 27017
+ },
+ "insulto" : "insulto",
+ "dejar" : "dejardemadriarme",
+ "madriar" : "madrear",
+ "user" : "madriador",
+ "me" : "alejonext",
+ "tag" : "madriador",
+ "coleccion" : {
+ "usuario" : "usuarios",
+ "madrazo" : "madrazos"
+ }
+}
View
25 lib/config.js
@@ -1,8 +1,31 @@
var path = require('path'),
dir = path.resolve(__dirname, '..'),
- config = require(path.join(dir, 'package.json')),
+ z = require(path.join(dir, 'package.json')),
_ = require('underscore');
module.exports = function( data ){
+ config =_.extend({
+ twitter : {
+ consumer_key : "YourKey",
+ consumer_secret : "YourKeyScret",
+ access_token : "TheToken",
+ access_token_secret : "TokenSecret"
+ },
+ db : {
+ host : "localhost",
+ path : "madrazos",
+ type : "mongodb",
+ port : 27017
+ },
+ insulto : "insulto",
+ dejar : "dejardemadriarme",
+ madriar : "madrear",
+ user : "madriador",
+ me : "alejonext",
+ tag : "madriador",
+ coleccion : {
+ usuario : "usuarios",
+ madrazo : "madrazos"
+ } }, z);
return require('configurable')( _.extend(data, config ) );
};
View
60 lib/db.js
@@ -8,7 +8,7 @@ var _ = require('underscore'),
lib = require('./all.js'),
dir = path.resolve(__dirname, '..'),
config = require(path.join(dir, 'package.json')),
- db = mongo.createConnection( lib.urlServer( config.db ) );
+
var Schema = mongo.Schema,
ObjectId= Schema.Types.ObjectId;
@@ -39,50 +39,48 @@ madrazo.path('idioma').validate(_.isString, 'format');
usuario.path('usuario').validate(_.isString, 'format');
usuario.path('idioma').validate(_.isString, 'format');
-usuarios = db.model('usuario', usuario);
-madriar = db.model('madrazo', madrazo);
-
-module.exports = {
-
- insertarMadrazo : function(data, callback) {
- obj = new madriar({
- insulto : data.madrazo,
- idioma : 'es'
- });
- return obj.save(callback);
- },
+db = function(config){
+ connet = mongo.createConnection( lib.urlServer( config.get("db") ) );
+ this.usuarios = connet.model(config.get("coleccion").usuario, usuario);
+ this.madriar = connet.model(config.get("coleccion").madrazo, madrazo);
+};
- insertarUsuario : function(data, callback) {
+insertarMadrazo = function(data, callback) {
+ obj = new this.madriar({
+ insulto : data.madrazo,
+ idioma : 'es'
+ });
+ return obj.save(callback);
+};
+insertarUsuario = function(data, callback) {
esto = new Array();
for(var i=0,j=data.usuarios.length; i<j; i++){
- doc = new usuarios({
+ doc = new this.usuarios({
usuario : data.usuarios[i]
});
doc.save(function(err) {
esto.push(err);
});
};
return callback( _.compact(esto) );
- },
-
- eliminarUsuario : function(data, callback) {
+ };
+eliminarUsuario = function(data, callback) {
uesto = new Array;
for(var i=0,j=data.users.length; i<j; i++){
- usuarios.findOneAndRemove({
+ this.usuarios.findOneAndRemove({
usuario : data.usuarios[i]
}, function(err) {
uesto.push(err);
});
};
return callback( _.compact( uesto ) );
- },
-
- Quienes : function(callback) {
- return usuarios.find( callback );
- },
+ };
+Quienes = function(callback) {
+ return this.usuarios.find( callback );
+ };
- madrazo : function( who, callback ) {
- return madriar.find(function(err, docs) {
+madrazo = function( who, callback ) {
+ return this.madriar.find(function(err, docs) {
if( docs.length > 0 || !err ){
temlp = "<%= usuario%> " + _.shuffle( docs )[0].insulto.join(' ');
return callback(err, who ? _.template( temlp, who) : false );
@@ -91,5 +89,11 @@ module.exports = {
}
});
- }
-};
+ };
+
+//Exports
+db.prototype.insertarMadrazo = insertarMadrazo;
+db.prototype.insertarUsuario = insertarUsuario;
+db.prototype.Quienes = Quienes;
+db.prototype.madrazo = madrazo;
+module.exports = exports = db;
View
6 lib/twitter.js
@@ -1,13 +1,14 @@
var Twit= require('twit'),
tuitter = require('ntwitter');
_ = require('underscore'),
- db = require('./db.js'),
+ dbs = require('./db.js'),
lib = require('./all.js');
module.exports = {
index : function( config ) {
T = new Twit( config.get("twuiter") ),
twit = new tuitter( config.get("twuiter") );
+ db = new dbs( config.set("db") );
db.Quienes(function(err, docs) {
db.madrazo( _.shuffle( docs )[0], function(err, text){
if(!err && _.isString( text ) ){
@@ -21,7 +22,8 @@ module.exports = {
});
},
stream : function( config ) {
- var T = new Twit( config.get("twuiter") ),
+ var db = new dbs( config.set("db") ),
+ T = new Twit( config.get("twuiter") ),
stream = T.stream('statuses/filter', { track: config.get("tag") });
stream.on('tweet', function (data) {
if( !data.retweeted && data.retweet_count <= 0 ){
View
12 package.json
@@ -9,17 +9,5 @@
"express": "3.0.0rc1",
"ejs": "*",
"less-middleware": "*"
- },
- "twitter": {
- "consumer_key": "YourKey",
- "consumer_secret": "YourKeyScret",
- "access_token": "TheToken",
- "access_token_secret": "TokenSecret"
- },
- "db": {
- "host": "localhost",
- "path": "madrazos",
- "type": "mongodb",
- "port": 27017
}
}
View
23 routes/index.js
@@ -4,12 +4,17 @@
*/
var lib = require('../lib');
-exports.index = function(req, res){
+routes = function(data){
+ this.db = new lib.db(data.get("db"));
+};
+
+
+index = function(req, res){
res.render('index', { title: 'Express' });
};
-exports.user = function(req, res, next){
- lib.db.insertarUsuario(req.body.user, function(err, result){
+user = function(req, res, next){
+ this.db.insertarUsuario(req.body.user, function(err, result){
if(_.isNull(result) ){
res.json({ error : err, user : 'delete' });
} else {
@@ -18,12 +23,18 @@ exports.user = function(req, res, next){
});
};
-exports.madriar = function(req, res, next){
- lib.db.insertarMadrazo(req.body.user, function(err, result){
+madriar = function(req, res, next){
+ this.db.insertarMadrazo(req.body.user, function(err, result){
if(_.isNull(result) ){
res.json({ error : err, user : 'delete' });
} else {
res.json({ error : err, madrazo : result });
};
});
-};
+};
+
+//Exports
+routes.prototype.user = user;
+routes.prototype.madriar = madriar;
+routes.prototype.index = index;
+module.exports = exports = routes;

0 comments on commit 6beb24f

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