Module Camping
In: lib/camping-unabridged.rb
lib/camping/ar.rb
lib/camping/reloader.rb
lib/camping/server.rb
lib/camping/session.rb

The Camping Server (for development)

Camping includes a pretty nifty server which is built for development. It follows these rules:

  • Load all Camping apps in a directory or a file.
  • Load new apps that appear in that directory or that file.
  • Mount those apps according to their name. (e.g. Blog is mounted at /blog.)
  • Run each app‘s create method upon startup.
  • Reload the app if its modification time changes.
  • Reload the app if it requires any files under the same directory and one of their modification times changes.
  • Support the X-Sendfile header.

Run it like this:

  camping examples/        # Mounts all apps in that directory
  camping blog.rb          # Mounts Blog at /

And visit localhost:3301/ in your browser.

Methods

call   goes   method_missing   options   set   use  

Classes and Modules

Module Camping::Base
Module Camping::Controllers
Module Camping::Helpers
Module Camping::Models
Module Camping::Session
Module Camping::Views
Class Camping::Cookies
Class Camping::H
Class Camping::Reloader
Class Camping::Server

Constants

C = self
S = IO.read(__FILE__) rescue nil
P = "<h1>Cam\ping Problem!</h1><h2>%s</h2>"
U = Rack::Utils
O = {}
Apps = []
X = Controllers

Public Class methods

Ruby web servers use this method to enter the Camping realm. The e argument is the environment variables hash as per the Rack specification. And array with [status, headers, body] is expected at the output.

See: rack.rubyforge.org/doc/SPEC.html

[Source]

     # File lib/camping-unabridged.rb, line 634
634:     def call(e)
635:       X.M
636:       k,m,*a=X.D e['PATH_INFO'],e['REQUEST_METHOD'].downcase,e
637:       k.new(e,m).service(*a).to_a
638:     rescue
639:       r500(:I, k, m, $!, :env => e).to_a
640:     end

When you are running many applications, you may want to create independent modules for each Camping application. Camping::goes defines a toplevel constant with the whole MVC rack inside:

  require 'camping'
  Camping.goes :Nuts

  module Nuts::Controllers; ... end
  module Nuts::Models;      ... end
  module Nuts::Views;       ... end

All the applications will be available in Camping::Apps.

[Source]

     # File lib/camping-unabridged.rb, line 621
621:     def goes(m)
622:       Apps << a = eval(S.gsub(/Camping/,m.to_s), TOPLEVEL_BINDING)
623:       caller[0]=~/:/
624:       IO.read(a.set:__FILE__,$`)=~/^__END__/ &&
625:       (b=$'.split(/^@@\s*(.+?)\s*\r?\n/m)).shift rescue nil
626:       a.set :_t,H[*b||[]]
627:     end

The Camping scriptable dispatcher. Any unhandled method call to the app module will be sent to a controller class, specified as an argument.

  Blog.get(:Index)
  #=> #<Blog::Controllers::Index ... >

The controller object contains all the @cookies, @body, @headers, etc. formulated by the response.

You can also feed environment variables and query variables as a hash, the final argument.

  Blog.post(:Login, :input => {'username' => 'admin', 'password' => 'camping'})
  #=> #<Blog::Controllers::Login @user=... >

  Blog.get(:Info, :env => {'HTTP_HOST' => 'wagon'})
  #=> #<Blog::Controllers::Info @headers={'HTTP_HOST'=>'wagon'} ...>

[Source]

     # File lib/camping-unabridged.rb, line 660
660:     def method_missing(m, c, *a)
661:       X.M
662:       h = Hash === a[-1] ? a.pop : {}
663:       e = H[Rack::MockRequest.env_for('',h.delete(:env)||{})]
664:       k = X.const_get(c).new(e,m.to_s)
665:       h.each { |i, v| k.send("#{i}=", v) }
666:       k.service(*a)
667:     end

A hash where you can set different settings.

[Source]

     # File lib/camping-unabridged.rb, line 681
681:     def options
682:       O
683:     end

Shortcut for setting options:

  module Blog
    set :secret, "Hello!"
  end

[Source]

     # File lib/camping-unabridged.rb, line 690
690:     def set(k, v)
691:       O[k] = v
692:     end

Injects a middleware:

  module Blog
    use Rack::MethodOverride
    use Rack::Session::Memcache, :key => "session"
  end

[Source]

     # File lib/camping-unabridged.rb, line 675
675:     def use(*a, &b)
676:       m = a.shift.new(method(:call), *a, &b)
677:       meta_def(:call) { |e| m.call(e) }
678:     end

[Validate]