Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 0b5c0aca696a2ebb83831324722f4587323b7d2d 0 parents
@fojas authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
36 app.coffee
@@ -0,0 +1,36 @@
+
+io = require 'socket.io'
+express = require 'express'
+handlebars = require 'hbs'
+
+routes = require './routes'
+sockets = require './lib/sockets'
+handlebar_helpers = require('./lib/handlebar_helpers')(handlebars)
+
+app = module.exports = express.createServer()
+
+app.configure () ->
+ app.set 'views', "#{__dirname}/views"
+ app.set 'view engine', 'hbs'
+ app.use express.bodyParser()
+ app.use express.methodOverride()
+ app.use app.router
+ app.use express.static("#{__dirname}/public")
+ app.use require('connect-assets') src : "#{__dirname}/public"
+
+
+app.configure 'development', () ->
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
+
+
+app.configure 'production', () ->
+ app.use(express.errorHandler())
+
+app.get('/', routes.index)
+
+app.listen 3000, () ->
+ console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env)
+
+sio = io.listen(app)
+for own attr, command of sockets
+ sio.of('/'+attr).on('connection', command.connection)
6 lib/handlebar_helpers.coffee
@@ -0,0 +1,6 @@
+module.exports = (handlebars) ->
+ handlebars.registerHelper 'js', (options) ->
+ return js options.hash.file
+
+ handlebars.registerHelper 'css', (options) ->
+ return css options.hash.file
2  lib/sockets.coffee
@@ -0,0 +1,2 @@
+require("fs").readdirSync("./sockets").forEach (file) ->
+ module.exports[file.split('.')[0]] = require("../sockets/" + file)
24 package.json
@@ -0,0 +1,24 @@
+{
+ "name": "base-app"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.5.8"
+ , "coffee-script": "1.3.3"
+ , "hbs": "1.0.2"
+ , "socket.io": "0.9.6"
+ , "supervisor": "0.3.1"
+ , "connect-assets": "2.2.0"
+ , "less": "1.3.0"
+ , "cleverbot-node": "0.1.1"
+ }
+ , "scripts" : {
+ "start": "./node_modules/.bin/supervisor -e \"node|js|coffee\" run.js"
+ , "test": "./node_modules/.bin/mocha $(find test -name \"*_test.coffee\")"
+ }
+ , "devDependencies": {
+ "should": "0.6.3"
+ , "mocha": "1.0.3"
+ , "sinon": "1.3.4"
+ }
+}
8 public/css/style.css
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
18 public/js/application.js
@@ -0,0 +1,18 @@
+//= require_tree .
+!function($,window,undefined){
+ var startConversation = function(initialMsg){
+ var d = $('#conversation');
+ var a = new CleverClient;
+ a.callback(function(data){
+ d.append("<div><span>"+data.user+"</span> : <span>"+data.response+"</span></div>");
+ });
+ a.write(initialMsg);
+ }
+ $(function(){
+ $('form').one('submit',function(){
+ var input = $('input');
+ startConversation(input.val())
+ input.remove()
+ });
+ });
+}(jQuery,window)
15 public/js/clever_client.coffee
@@ -0,0 +1,15 @@
+$ = jQuery
+
+window.CleverClient = ->
+ @socket = io.connect('http://localhost:3000/index')
+ @socket.on 'response', =>
+ if @callback
+ @callback.apply @, arguments
+ this
+
+_.extend window.CleverClient.prototype, {
+ write : (message) ->
+ @socket.emit 'message', message
+ callback : (cb) ->
+ @callback = cb
+}
2  routes/index.coffee
@@ -0,0 +1,2 @@
+exports.index = (req, res) ->
+ res.render('index', {title: 'Express' })
2  run.js
@@ -0,0 +1,2 @@
+require('coffee-script');
+require('./app');
23 sockets/index.coffee
@@ -0,0 +1,23 @@
+CleverBot = require('cleverbot-node')
+clever = new CleverBot
+
+module.exports = {
+ connection : (socket) ->
+ connected = true
+ user = ["Bob Loblaw", "Tobias Funke"]
+ i = 0
+
+ callback = (data,fn) ->
+ socket.emit 'response', {
+ user :user[(i = (i+1) % 2)],
+ response : data
+ }
+ clever.write data, (response) ->
+ if connected
+ callback response.message, callback
+
+ socket.on 'message', callback
+
+ socket.on 'disconnect', () ->
+ connected = false
+}
2  test/mocha.opts
@@ -0,0 +1,2 @@
+--compilers coffee:coffee-script
+--reporter dot
12 test/routes/index_test.coffee
@@ -0,0 +1,12 @@
+routes = require ('../../routes')
+should = require('should')
+
+describe 'Routes', ->
+ describe 'Index', ->
+ it 'should respond to default route', (done)->
+ routes.index {}, {
+ render: (viewName,options) ->
+ viewName.should.equal("index")
+ options.title.should.equal("Express")
+ done()
+ }
10 test/sockets/index_test.coffee
@@ -0,0 +1,10 @@
+sockets = require '../../lib/sockets'
+should = require('should')
+
+describe "Socket", ->
+ describe "Index", ->
+ it "should have an index channel", ->
+ should.exist sockets.index
+
+ it "should have a connection callback for index", ->
+ should.exist sockets.index.connection
106 test/support/http.js
@@ -0,0 +1,106 @@
+
+/**
+ * visionmedia <3
+ * Module dependencies.
+ */
+
+var EventEmitter = require('events').EventEmitter
+ , methods = require('express').router.methods
+ , http = require('http');
+
+module.exports = request;
+
+function request(app) {
+ return new Request(app);
+}
+
+function Request(app) {
+ var self = this;
+ this.data = [];
+ this.header = {};
+ this.app = app;
+ if (!this.server) {
+ this.server = http.Server(app);
+ this.server.listen(0, function(){
+ self.addr = self.server.address();
+ self.listening = true;
+ });
+ }
+}
+
+/**
+ * Inherit from ventEmitter.prototype
+ */
+
+Request.prototype.__proto__ = EventEmitter.prototype;
+
+methods.forEach(function(method){
+ Request.prototype[method] = function(path){
+ return this.request(method, path);
+ };
+});
+
+Request.prototype.set = function(field, val){
+ this.header[field] = val;
+ return this;
+};
+
+Request.prototype.write = function(data){
+ this.data.push(data);
+ return this;
+};
+
+Request.prototype.request = function(method, path){
+ this.method = method;
+ this.path = path;
+ return this;
+};
+
+Request.prototype.expect = function(body, fn){
+ this.end(function(res){
+ if ('number' == typeof body) {
+ res.statusCode.should.equal(body);
+ } else if (body instanceof RegExp) {
+ res.body.should.match(body);
+ } else {
+ res.body.should.equal(body);
+ }
+ fn();
+ });
+};
+
+Request.prototype.end = function(fn){
+ var self = this;
+
+ if (this.listening) {
+ var req = http.request({
+ method: this.method
+ , port: this.addr.port
+ , host: this.addr.address
+ , path: this.path
+ , headers: this.header
+ });
+
+ this.data.forEach(function(chunk){
+ req.write(chunk);
+ });
+
+ req.on('response', function(res){
+ var buf = '';
+ res.setEncoding('utf8');
+ res.on('data', function(chunk){ buf += chunk });
+ res.on('end', function(){
+ res.body = buf;
+ fn(res);
+ });
+ });
+
+ req.end();
+ } else {
+ this.server.on('listening', function(){
+ self.end(fn);
+ });
+ }
+
+ return this;
+};
5 views/index.hbs
@@ -0,0 +1,5 @@
+<form action="javascript:void 0">
+<input type="text" placeholder="Start the conversation" />
+</form>
+<div id="conversation" >
+</div>
14 views/layout.hbs
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>{{title}}</title>
+ <link rel="stylesheet" href="/css/style.css" />
+ <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" ></script>
+ <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
+ <script src="/socket.io/socket.io.js" ></script>
+ {{{ js file="application" }}}
+ </head>
+ <body>
+ {{{body}}}
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.