camping 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/camping.rb CHANGED
@@ -1,105 +1,49 @@
1
- %w[rubygems active_record markaby metaid ostruct].each { |lib| require lib }
2
-
3
- module Camping
4
- module Models; end ; module Views; end ; Models::Base = ActiveRecord::Base
5
- Markaby::Builder.class_eval { include Views }
6
-
7
- module Controllers
8
- module RM
9
- attr_accessor :input, :cookies, :headers, :body, :status
10
- def method_missing(m, *args, &blk)
11
- str = eval("markaby.#{m}(*args, &blk)")
12
- str = markaview(:layout) { str } rescue nil
13
- r(200, str.to_s, 'Content-Type' => 'text/html')
14
- end
15
- def render(m)
16
- str = markaview(m)
17
- str = markaview(:layout) { str } rescue nil
18
- r(200, str, "Content-Type" => 'text/html')
19
- end
20
- def redirect(*args)
21
- c, *args = args
22
- if c.respond_to? :urls
23
- c = c.urls.first.gsub(/\(.+?\)/) do
24
- a = args.shift
25
- a.method(a.class.primary_key)[] rescue a
26
- end
27
- end
28
- r(302, '', 'Location' => c)
29
- end
30
- def r(s, b, h = {}); @status = s; @headers.merge!(h); @body = b; end
31
- def service(r, e, m, a)
32
- @status, @headers = 200, {'Content-Type' => 'text/html'}
33
- @cookies = Camping.cookie_parse(e['HTTP_COOKIE'] || e['COOKIE'])
34
- cook = @cookies.marshal_dump.dup
35
- if ("POST" == e['REQUEST_METHOD']) and
36
- %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n.match(e['CONTENT_TYPE'])
37
- return r(500, "Urgh, multipart/form-data not yet supported.")
38
- else
39
- @input = Camping.qs_parse(e['REQUEST_METHOD'] == "POST" ?
40
- r.read(e['CONTENT_LENGTH'].to_i) : e['QUERY_STRING'])
41
- end
42
-
43
- @body = method( m.downcase ).call(*a)
44
- @headers['Set-Cookie'] = @cookies.marshal_dump.map { |k,v| "#{k}=#{Camping.escape(v)}; path=/" if v != cook[k] }.compact
45
- self
46
- end
47
- def to_s
48
- "Status: #{@status}\n#{@headers.map{|k,v|v.to_a.map{|v2|"#{k}: #{v2}"}}.flatten.join("\n")}\n\n#{@body}"
49
- end
50
- private
51
- def markaby
52
- Markaby::Builder.new( instance_variables.map { |iv|
53
- [iv[1..-1].intern, instance_variable_get(iv)] }, {} )
54
- end
55
- def markaview(m, *args, &blk)
56
- markaby.instance_eval { Views.instance_method(m).bind(self).call(*args, &blk); self }.to_s
57
- end
58
- end
59
- class R; include RM end
60
- class NotFound < R; def get; r(404, h1('Camping Problem!') + h2('Not Found')); end end
61
-
62
- class << self
63
- def R(*urls); Class.new(R) { meta_def(:inherited) { |c| c.meta_def(:urls) { urls } } }; end
64
- def D(path)
65
- constants.each do |c|
66
- k = const_get(c)
67
- return k, $~[1..-1] if (k.urls rescue "/#{c.downcase}").find { |x| path =~ /^#{x}\/?$/ }
68
- end
69
- [NotFound, []]
70
- end
71
- end
72
- end
73
-
74
- class Response; include Controllers::RM
75
- def initialize(s = 200, &blk)
76
- @headers, @body, @status = {}, "", s
77
- instance_eval &blk
78
- end
79
- end
80
-
81
- class << self
82
- def escape(s); s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n){'%'+$1.unpack('H2'*$1.size).join('%').upcase}.tr(' ', '+') end
83
- def unescape(s); s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){[$1.delete('%')].pack('H*')} end
84
- def qs_parse(qs, d = '&;'); OpenStruct.new((qs||'').split(/[#{d}] */n).
85
- inject({}){|hsh, p|k, v = p.split('=',2).map {|v| unescape(v)}; hsh[k] = v unless v.empty?; hsh}) end
86
- def cookie_parse(s); c = qs_parse(s, ';,'); end
87
- def run(r=$stdin,w=$stdout)
88
- begin
89
- k, a, m = Controllers.D(ENV['PATH_INFO']) + [ENV['REQUEST_METHOD'] || "GET"]
90
- k.class_eval { include Controllers::RM }
91
- o = k.new
92
- o.class.class_eval do
93
- Models.constants.each do |c|
94
- g = Models.const_get(c)
95
- remove_const c if const_defined? c
96
- const_set c, g
97
- end
98
- end
99
- w << o.service(r, ENV, m, a)
100
- rescue => e
101
- w << Response.new(200) { @headers['Content-Type'] = 'text/html'; @body = Markaby::Builder.new({}, {}) { h1 'Camping Problem!'; h2 "#{k}.#{m}"; h3 "#{e.class} #{e.message}:"; ul { e.backtrace.each { |bt| li bt } } }.to_s }
102
- end
103
- end
104
- end
105
- end
1
+ %w[rubygems active_record markaby metaid ostruct tempfile].each{|l|require l}
2
+ module Camping;C=self;S=File.read(__FILE__).gsub(/_{2}FILE_{2}/,__FILE__.dump)
3
+ module Helpers;def R c,*args;p=/\(.+?\)/;args.inject(c.urls.detect{|x|x.scan(p
4
+ ).size==args.size}.dup){|str,a|str.sub(p,(a.method(a.class.primary_key)[]rescue
5
+ a).to_s)};end;def / p;p[/^\//]?@root+p:p end;def errors_for(o);ul.errors{o.
6
+ errors.each_full{|er|li er}}unless o.errors.empty?;end;end;module Controllers
7
+ module Base; include Helpers;attr_accessor :input,:cookies,:headers,:body,
8
+ :status,:root;def method_missing(m,*args,&blk);str=m==:render ? markaview(*args,
9
+ &blk):eval("markaby.#{m}(*args,&blk)");str=markaview(:layout){str}rescue nil;r(
10
+ 200,str.to_s);end;def r(s,b,h={});@status=s;@headers.merge!(h);@body=b;end;def
11
+ redirect(c,*args);c=R(c,*args)if c.respond_to?:urls;r(302,'','Location'=>self/c)
12
+ end;def service(r,e,m,a)@status,@headers,@root=200,{},e['SCRIPT_NAME'];cook=C.kp(
13
+ e['HTTP_COOKIE']);qs=C.qs_parse(e['QUERY_STRING']);if "POST"==m;inp=r.read(e[
14
+ 'CONTENT_LENGTH'].to_i);if %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)|n.
15
+ match(e['CONTENT_TYPE']);b="--#$1";inp.split(/(?:\r?\n|\A)#{Regexp::quote(
16
+ b)}(?:--)?\r\n/m).each{|pt|h,v=pt.split("\r\n\r\n",2);fh={};[:name,:filename].
17
+ each{|x|fh[x]=$1 if h=~/^Content-Disposition: form-data;.*(?:\s#{x}="([^"]+)")\
18
+ /m};fn=fh[:name];if fh[:filename];fh[:type]=$1 if h =~ /^Content-Type: (.+?)(\
19
+ \r\n|\Z)/m;fh[:tempfile]=Tempfile.new("#{C}").instance_eval{binmode;write v
20
+ rewind;self};else;fh=v;end;qs[fn]=fh if fn};else;qs.merge!(C.qs_parse(inp));end
21
+ end;@cookies,@input=[cook,qs].map{|_|OpenStruct.new(_)};@body=method(m.downcase
22
+ ).call(*a);@headers["Set-Cookie"]=@cookies.marshal_dump.map{|k,v|"#{k}=#{C.
23
+ escape(v)}; path=/" if v != cook[k]}.compact;self;end;def to_s;"Status: #{
24
+ @status}\n#{{'Content-Type'=>'text/html'}.merge(@headers).map{|k,v|v.to_a.map{
25
+ |v2|"#{k}: #{v2}"}}.flatten.join("\n")}\n\n#{@body}";end;def markaby;Mab.new(
26
+ instance_variables.map{|iv|[iv[1..-1],instance_variable_get(iv)]},{});end;def
27
+ markaview(m,*args,&blk);b=markaby;b.method(m).call(*args, &blk);b.to_s
28
+ end;end;class R;include Base end;class
29
+ NotFound;def get(p);r(404,div{h1("#{C} Problem!")+h2("#{p} not found")});end
30
+ end;class ServerError;include Base;def get(k,m,e);r(500,markaby.div{h1 "#{C} Problem!"
31
+ h2 "#{k}.#{m}";h3 "#{e.class} #{e.message}:";ul{e.backtrace.each{|bt|li(bt)}}}
32
+ )end end;class<<self;def R(*urls);Class.new(R){meta_def(:inherited){|c|c.
33
+ meta_def(:urls){urls}}};end;def D(path);constants.inject(nil){|d,c|k=
34
+ const_get(c);k.meta_def(:urls){["/#{c.downcase}"]}if !(k<R);d||([k, $~[1..-1]
35
+ ] if k.urls.find { |x| path =~ /^#{x}\/?$/ })}||[NotFound, [path]];end end end
36
+ class<<self;def goes m;eval(S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING)end;def
37
+ escape s;s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n){'%'+$1.unpack('H2'*$1.size).join(
38
+ '%').upcase}.tr(' ','+') end;def unescape(s);s.tr('+', ' ').gsub(/((?:%[0-9a-f\
39
+ A-F]{2})+)/n){[$1.delete('%')].pack('H*')} end;def qs_parse(qs,d ='&;');(qs||''
40
+ ).split(/[#{d}] */n).inject({}){|hsh, p|k,v=p.split('=',2).map{|v|unescape(v)}
41
+ hsh[k]=v unless v.blank?;hsh} end; def kp(s);c=qs_parse(s,';,') end
42
+ def run(r=$stdin,w=$stdout);w<<begin;k,a=Controllers.D "/#{ENV['PATH_INFO']}".
43
+ gsub(%r!/+!,'/');m=ENV['REQUEST_METHOD']||"GET";k.class_eval{include C
44
+ include Controllers::Base;include Models};o=k.new;o.service(r,ENV,m,a);rescue\
45
+ =>e;Controllers::ServerError.new.service(r,ENV,"GET",[k,m,e]);end;end;end
46
+ module Views; include Controllers; include Helpers end;module Models;end
47
+ Models::Base=ActiveRecord::Base;class Mab<Markaby::Builder;include Views
48
+ def tag!(*g,&b);h=g[-1];[:href,:action].each{|a|(h[a]=self/h[a])rescue 0}
49
+ super;end;end;end
metadata CHANGED
@@ -3,19 +3,19 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: camping
5
5
  version: !ruby/object:Gem::Version
6
- version: "1.1"
7
- date: 2006-01-19 00:00:00 -07:00
6
+ version: "1.2"
7
+ date: 2006-01-23 00:00:00 -07:00
8
8
  summary: miniature rails for stay-at-home moms
9
9
  require_paths:
10
10
  - lib
11
11
  email: why@ruby-lang.org
12
- homepage: http://whytheluckystiff.net/camping/
13
- rubyforge_project: hobix
12
+ homepage: http://code.whytheluckystiff.net/camping/
13
+ rubyforge_project: camping
14
14
  description:
15
15
  autorequire: camping
16
16
  default_executable:
17
17
  bindir: bin
18
- has_rdoc: false
18
+ has_rdoc: true
19
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
20
20
  requirements:
21
21
  - - ">"
@@ -28,19 +28,38 @@ cert_chain:
28
28
  authors:
29
29
  - why the lucky stiff
30
30
  files:
31
+ - README
32
+ - examples/tepee
31
33
  - examples/blog
32
- - examples/blog/blog.sqlite3
33
- - examples/blog/blog.rb
34
+ - examples/charts
35
+ - examples/serve
36
+ - examples/tepee/tepee.rb
37
+ - examples/tepee/start
34
38
  - examples/blog/styles.css
39
+ - examples/blog/blog.rb
40
+ - examples/blog/blog.sqlite3
35
41
  - examples/blog/start
36
- - lib/camping-mural.rb
42
+ - examples/charts/charts
43
+ - examples/charts/pie.rb
44
+ - examples/charts/charts.rb
45
+ - examples/charts/start
37
46
  - lib/camping.rb
47
+ - lib/camping-unabridged.rb
38
48
  test_files: []
39
49
 
40
- rdoc_options: []
41
-
42
- extra_rdoc_files: []
43
-
50
+ rdoc_options:
51
+ - --title
52
+ - Camping Documentation
53
+ - --line-numbers
54
+ - --inline-source
55
+ - --main
56
+ - README
57
+ - --exclude
58
+ - ^examples/
59
+ - --exclude
60
+ - lib/camping.rb
61
+ extra_rdoc_files:
62
+ - README
44
63
  executables: []
45
64
 
46
65
  extensions: []
data/lib/camping-mural.rb DELETED
@@ -1,43 +0,0 @@
1
- %w[rubygems active_record markaby metaid ostruct].each {|lib| require lib}
2
- module Camping;C=self;module Models;Base=ActiveRecord::Base;end;module Views
3
- end;Markaby::Builder.class_eval{include Views};module Controllers;module RM
4
- attr_accessor :input,:cookies,:headers,:body,:status;def method_missing(m,
5
- *args, &blk);str=eval("markaby.#{m}(*args, &blk)");str=markaview(:layout){
6
- str}rescue nil;r(200,str.to_s,'Content-Type'=>'text/html');end;def render(m)
7
- str=markaview(m);str=markaview(:layout){ str }rescue nil;r(200, str,
8
- "Content-Type" => 'text/html');end;def redirect(*args);c, *args = args;if \
9
- c.respond_to? :urls;c=c.urls.first.gsub(/\(.+?\)/){a=args.shift;a.
10
- method(a.class.primary_key)[] rescue a};end;r(302,'','Location' => c);end
11
- def r(s,b,h={});@status=s;@headers.merge!(h);@body = b;end;def service(
12
- r,e,m,a);@status,@headers=200,{'Content-Type' => 'text/html'};@cookies=
13
- C.cookie_parse(e['COOKIE']||e['HTTP_COOKIE']);cook=@cookies.marshal_dump.
14
- dup;if (e['REQUEST_METHOD']=='POST')and %r|\Amultipart/form-data.*boundar\
15
- y=\"?.+?\"?|n=~e['CONTENT_TYPE'];return r(500, "Urgh, multipart/form-data\
16
- not yet supported.");else;@input=C.qs_parse(e['REQUEST_METHOD']!="POST" ?
17
- e['QUERY_STRING']:r.read(e['CONTENT_LENGTH'].to_i));end;@body=method(m.
18
- downcase).call(*a);@headers['Set-Cookie']=@cookies.marshal_dump.map{|k,v|
19
- "#{k}=#{C.escape(v)};path=/"if v != cook[k]}.compact;self;end;def to_s;
20
- "Status: #{@status}\n#{@headers.map{|k,v|v.to_a.map{|v2|"#{k}: #{v2}"}}.
21
- flatten.join("\n")}\n\n#{@body}";end;private;def markaby;Markaby::Builder.
22
- new( instance_variables.map{|iv|[iv[1..-1].intern,instance_variable_get(
23
- iv)]},{});end;def markaview(m,*args,&blk);markaby.instance_eval{Views.
24
- instance_method(m).bind(self).call(*args, &blk);self}.to_s;end;end;class R
25
- include RM end;class NotFound < R;def get;r(404,h1('#{C} Problem!')+h2(
26
- 'Not Found'))end end;class<<self;def R(*urls);Class.new(R){meta_def(
27
- :inherited){|c|c.meta_def(:urls){urls}}}end;def D(path);constants.each{|c|
28
- k=const_get(c);return k,$~[1..-1]if (k.urls rescue "/#{c.downcase}").find{
29
- |x|path=~/^#{x}\/?$/ };};[NotFound, []]end end end;class Response; include
30
- Controllers::RM;def initialize(s=200,&blk);@headers,@body,@status={},"",s
31
- instance_eval &blk end end;class << self;def escape(s); s.to_s.gsub(/([^ \
32
- a-zA-Z0-9_.-]+)/n){'%'+$1.unpack('H2'*$1.size).join('%').upcase}.tr(' ','+')
33
- end;def unescape(s);s.tr('+',' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){[$1.delete(
34
- '%')].pack('H*')}end;def qs_parse(qs,d='&;');OpenStruct.new((qs||'').split(
35
- /[#{d}] */n).inject({}){|hsh, p|k,v=p.split('=',2).map{|v|unescape(v)};hsh[
36
- k]=v unless v.empty?;hsh}) end;def cookie_parse(s);c=qs_parse(s,';,') end
37
- def run(r=$stdin,w=$stdout);begin;k,a,m=Controllers.D(ENV['PATH_INFO'])+[
38
- ENV['REQUEST_METHOD']||"GET"];k.class_eval{include Controllers::RM};o=k.new
39
- o.class.class_eval{Models.constants.each{|c|g=Models.const_get(c)
40
- remove_const c if const_defined? c;const_set c,g}};w<<o.service(r,ENV,m,a)
41
- rescue=>e;w<<Response.new(200){@headers['Content-Type']='text/html';@body=
42
- Markaby::Builder.new({},{}){h1'#{C} Problem!';h2"#{k}.#{m}";h3"#{e.class} \
43
- #{e.message}:";ul{e.backtrace.each{|bt|li bt}}}.to_s};end end end end