Module: Camping::Base

Includes:
Helpers
Defined in:
lib/camping.rb

Constant Summary collapse

Z =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#/, #R, #URL, #errors_for

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*a, &b) ⇒ Object



9
10
11
# File 'lib/camping.rb', line 9

def method_missing*a,&b
a.shift if a[0]==:render;m=Mab.new({},self);s=m.capture{send(*a,&b)};s=m.layout{s}if
/^_/!~a[0].to_s and m.respond_to?:layout;s end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



8
9
10
# File 'lib/camping.rb', line 8

def body
  @body
end

#cookiesObject

Returns the value of attribute cookies.



8
9
10
# File 'lib/camping.rb', line 8

def cookies
  @cookies
end

#envObject

Returns the value of attribute env.



8
9
10
# File 'lib/camping.rb', line 8

def env
  @env
end

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/camping.rb', line 8

def headers
  @headers
end

#inputObject

Returns the value of attribute input.



8
9
10
# File 'lib/camping.rb', line 8

def input
  @input
end

#rootObject

Returns the value of attribute root.



8
9
10
# File 'lib/camping.rb', line 8

def root
  @root
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/camping.rb', line 8

def status
  @status
end

Instance Method Details

#initialize(r, e, m) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/camping.rb', line 13

def initialize r,e,m;e=H[e.to_hash];@status,@method,@env,@headers,@root=200,m.
downcase,e,{'Content-Type'=>"text/html"},e.SCRIPT_NAME.sub(/\/$/,'')
@k=C.kp e.HTTP_COOKIE;q=C.qs_parse e.QUERY_STRING;@in=r
if%r|\Amultipart/form-.*boundary=\"?([^\";,]+)|n.match e.CONTENT_TYPE
b=/(?:\r?\n|\A)#{Regexp::quote("--#$1")}(?:--)?\r$/;until@in.eof?;fh=H[];for l in@in
case l;when Z;break;when/^Content-D.+?: form-data;/;fh.u H[*$'.
scan(/(?:\s(\w+)="([^"]+)")/).flatten];when/^Content-Type: (.+?)(\r$|\Z)/m;fh[
:type]=$1;end;end;fn=fh[:name];o=if fh[:filename];o=fh[:tempfile]=Tempfile.new(:C)
o.binmode;else;fh=""end;while l=@in.read(16384);if l=~b;o<<$`.chomp;@in.seek(-$'.
size,IO::SEEK_CUR);break;end;o<<l;end;q[fn]=fh if fn;fh[:tempfile].rewind if
fh.is_a?H;end;elsif@method=="post";q.u C.qs_parse(@in.read)end;@cookies,@input=
@k.dup,q.dup end

#r(s, b, h = {}) ⇒ Object



11
12
# File 'lib/camping.rb', line 11

def r s,b,h={};@status=s;@headers. 
merge!h;@body=b end

#redirect(*a) ⇒ Object



12
# File 'lib/camping.rb', line 12

def redirect*a;r 302,'','Location'=>URL(*a)end

#service(*a) ⇒ Object



24
25
26
# File 'lib/camping.rb', line 24

def service*a;@body=send(@method,*a)if respond_to?@method
@headers["Set-Cookie"]=@cookies.map{|k,v|"#{k}=#{C.escape(v)}; path=#{self/'/'
}"if v!=@k[k]}-[nil];self end

#to_sObject



26
27
# File 'lib/camping.rb', line 26

def to_s;"Status: #{@status}#{Z+@headers.map{|k,v|
[*v].map{|x|"#{k}: #{x}"}}*Z+Z*2+@body}"end