Permalink
Browse files

converting and splitting up examples and specs

  • Loading branch information...
1 parent 6769fa7 commit 8c8f73f0dcbbefd53bbf3431e41e857cda97cabb @dantebronto committed Jan 19, 2011
View
@@ -10,7 +10,7 @@ p. Basic documentation is now available in the wiki. I recommend checking the sp
h3. License
-Copyright (c) 2009-2010 Kellen Presley
+Copyright (c) 2009-2011 Kellen Presley
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
View
@@ -0,0 +1,39 @@
+var Picard = require('../../lib/picard')
+
+// Break common.js standards and copy
+// functions from Picard into global namespace.
+// Functions from routing.js include
+// REST verbs, helpers, and routeSet
+
+Picard.globalize()
+
+get('/', function(){
+ return { text: 'Hello Universe' }
+})
+
+get('/haml', function(){
+ return {
+ template: 'index',
+ print_date: function () {
+ return (new Date()).toDateString();
+ },
+ current_user: {
+ name: "Jean-Luc Picard",
+ bio: "Captain of the USS Enterprise"
+ }
+ }
+})
+
+var commands = [
+ { command: 'Make it so' },
+ { command: 'You have the bridge, Number One' }
+]
+
+get('/json', function(){
+ return {
+ type: 'application/json',
+ body: JSON.stringify(commands)
+ }
+})
+
+Picard.start()
File renamed without changes.
@@ -0,0 +1,62 @@
+var Picard = require('../../lib/picard').start()
+
+// Controllers are groups of routes defined via routeSet
+
+Picard.routeSet('operations', function(){
+
+ var ops = this;
+ ops.pathPrefix = '/ops'
+ ops.layout = 'application'
+
+ ops.helpers({
+ message: function(){ return "App is running" },
+ version: require('../../lib/picard/server').version
+ })
+
+ // GET /ops/heartbeat
+ ops.get('/heartbeat', function(){
+ return {
+ text: ops.helpers().message(),
+ layout: 'alternate_layout'
+ }
+ })
+
+ // GET /ops/version
+ ops.get('/version', function(params){
+ return { template: 'ops/index' }
+ })
+
+})
+
+Picard.globalize()
+
+routeSet(function(){ // no name given
+
+ this.helpers({
+ message: function(name){
+ return 'Hello ' + (name ? name : '')
+ },
+ version: routeSet('operations').helpers().version
+ })
+
+ this.get('/anonymous_route_set', function(){
+ return { template: 'ops/index' }
+ })
+
+ this.get('/anonymous_route_two', function(){
+ return {
+ template: 'ops/index',
+ version: '123'
+ }
+ })
+
+ this.get('/anonymous_fail_route', function(){
+ return {
+ template: 'ops/index',
+ message: function(){
+ foo.bar // this will fail
+ // foo is not defined
+ }
+ }
+ })
+})
View
@@ -0,0 +1,26 @@
+var Picard = require('../../lib/picard').start()
+
+Picard.get('/foo/:bar', function(params){
+ return { text: params.bar }
+})
+
+Picard.get('/multiple/:thing/:stuff', function(params){
+ return { text: params.thing + " " + params.stuff }
+})
+
+Picard.get(/\/regex\/(.*)\/(.*)/, function(params){
+ return { text: params.captures[0] + ' ' + params.captures[1] }
+})
+
+Picard.get(/\/selective\/(\d+)/, function(params){ // must be a number
+ return { text: params.captures[0] }
+})
+
+Picard.get('/get_with_params', function(env){ // called with ?foo=bar&baz=bat
+ var query = env.parsedUrl().query // access querystring
+ return { text: query.foo + " " + query.baz }
+})
+
+Picard.get('/foo/bar/*:baz', function(params) {
+ return { text: "Globbed params for 'baz': " + params.baz }
+})
Binary file not shown.
View
@@ -11,7 +11,7 @@ var Conf = {
this.template.root = val
break
case 'public' :
- this.template.public_dir = val
+ this.template.public = val
break
case 'views' :
this.template.views = val
@@ -23,8 +23,17 @@ var Conf = {
this.template.mode = this.env.mode = val
break
}
+ },
+ rootDir: function(){
+ try {
+ var fullPath = process.mainModule.filename
+ return require('path').dirname(fullPath)
+ } catch(e) {
+ throw "Must set root directory via Picard.set('root', __dirname) or similar"
+ }
}
}
-exports.config = Conf.config
-exports.set = Conf.set
+exports.config = Conf.config
+exports.set = Conf.set
+exports.rootDir = Conf.rootDir
View
@@ -1,22 +1,21 @@
var Picard = {
config: require('./config').config,
set: require('./config').set,
- routes: require('./routing').routes,
- mime: require('./mime').mime,
version: require('./server').version,
start: require('./server').start,
template: require('./template').template
}
-// default configuration values
-Picard.config({
- root: __dirname.replace(/\/config/, ''),
+require('./merge')(Picard, require('./routing'))
+
+Picard.config({ // default settings
+ root: require('./config').rootDir(),
port: 9900,
public: '/public',
views: '/views',
mode: 'production'
})
-require('./request_extensions')
+require('./request')
module.exports = Picard
@@ -1,13 +1,11 @@
var sys = require('sys'),
url = require('url'),
- fs = require('fs'),
- http = require('http'),
doc = require('./template').template,
server = require('./server'),
merge = require('./merge'),
- Routes = require('./routing').routes
+ Routes = require('./routing')
-var request_extensions = {
+var requestExtensions = {
cookie: function(name, val, options){
if ( val === undefined )
return this.cookies[name]
@@ -18,16 +16,16 @@ var request_extensions = {
this.cookies[name] = options
},
- handle_exception: function(ex) {
- this.on_screen({
+ handleException: function(ex) {
+ this.onScreen({
status: 500,
body: '<h1> 500 Error </h1>' +
'<h3>' + ex.message + '</h3>' +
'<pre>' + ex.stack + '</pre>'
})
sys.puts(ex.stack)
},
- on_screen: function(scope){
+ onScreen: function(scope){
if( this.response.finished ){ return }
if ( scope == null )
@@ -45,25 +43,23 @@ var request_extensions = {
scope.headers.push([ 'Server', req.picardServer || 'Picard ' + server.version ])
scope.headers.push([ 'Content-Type', scope.type || 'text/html' ])
- scope.headers = req._set_cookies.call(req, scope.headers)
+ scope.headers = req.setCookies(scope.headers)
if ( scope.status == 500 && !scope.template )
- req.send_data(scope)
+ req.sendData(scope)
else
doc.buildAndSend(req, scope)
req.log(scope)
},
-
log: function(scope){
- sys.puts((this._method || this.method).toUpperCase() + ' ' + this.parsed_url().pathname + ' ' + scope.status)
+ sys.puts((this._method || this.method).toUpperCase() + ' ' + this.parsedUrl().pathname + ' ' + scope.status)
},
-
- parsed_url: function() {
- if (!('url' in this))
+ parsedUrl: function() {
+ if ( !('url' in this) )
return;
var parsed = url.parse(this.url, true);
- this.parsed_url = function() {
+ this.parsedUrl = function() {
return parsed;
};
return parsed;
@@ -75,32 +71,30 @@ var request_extensions = {
body: '<a href="'+ location + '">' + location + '</a>'
}
},
- send_data: function(scope){
+ sendData: function(scope){
if( !scope.body ) return
scope.headers.push([ 'Content-Length', scope.body.length ])
scope.headers.push([ 'Content-Encoding', scope.encoding ])
this.response.writeHead(scope.status, scope.headers)
this.response.write(scope.body, scope.encoding)
this.response.end()
},
- //////// private stuff //////////
-
- _extract_form_params: function(chunk){
+ extractFormParams: function(chunk){
try {
if( chunk == undefined ) { return }
var chunks = chunk.toString().replace(/\+/g, '%20').split('&')
- this.post_body = ('post_body' in this) ? this.post_body + chunk : chunk;
+ this.body = ('body' in this) ? this.body + chunk : chunk;
for(var i in chunks){
var k_v = chunks[i].toString().split('=')
this[k_v[0]] = decodeURIComponent(k_v[1])
}
} catch(ex) {
- this.handle_exception(ex)
+ this.handleException(ex)
}
},
- _extract_route_params: function(route, match_data){
+ extractRouteParams: function(route, match_data){
var i, l
if( match_data == null ){ return } else { match_data.shift() }
@@ -112,21 +106,7 @@ var request_extensions = {
for(i=0, l = match_data.length; i < l; i++)
this.captures[i] = match_data[i]
},
- _log_params: function(){
- if( this.post_body )
- sys.puts(' post_body: ' + this.post_body.toString())
-
- for( var prop in this ){
- var skips = [ 'socket','connection','httpVersion','headers','url','cookie','method','statusCode','client','httpVersionMajor',
- 'httpVersionMinor','upgrade','handle_exception','on_screen','parsed_url','redirect','send_data','serve_static','response',
- '_events','captures','route','constructor','_parseQueryString','setBodyEncoding','setEncoding','pause','resume','_addHeaderLine',
- 'emit','addListener','removeListener','removeAllListeners','listeners', 'cookies', '_method', 'post_body' ]
-
- if ( skips.indexOf(prop) == -1 )
- sys.puts(' ' + prop + ': ' + sys.inspect(this[prop]))
- }
- },
- _parse_cookies: function(){
+ parseCookies: function(){
try {
this.cookies = {}
var self = this
@@ -139,10 +119,10 @@ var request_extensions = {
})
}
} catch(ex) {
- this.handle_exception(ex)
+ this.handleException(ex)
}
},
- _resolve: function(){
+ resolve: function(){
try {
var scope = Routes.executeCallback(this)
@@ -151,13 +131,13 @@ var request_extensions = {
if ( scope == null )
return
- this.on_screen(scope)
+ this.onScreen(scope)
} catch(ex) {
- this.handle_exception(ex)
+ this.handleException(ex)
}
},
- _set_cookies: function(headers){
+ setCookies: function(headers){
var ret, name, options
for(name in this.cookies) {
@@ -181,4 +161,4 @@ var request_extensions = {
}
// extend abstract parent of http.ServerRequest
-merge(http.IncomingMessage.prototype, request_extensions)
+merge(require('http').IncomingMessage.prototype, requestExtensions)
Oops, something went wrong.

0 comments on commit 8c8f73f

Please sign in to comment.