lib/wunderbar/cgi-methods.rb in wunderbar-0.10.0 vs lib/wunderbar/cgi-methods.rb in wunderbar-0.10.1
- old
+ new
@@ -1,79 +1,61 @@
module Wunderbar
-
module CGI
- HIDE_FRAME = [ %r{/(wunderbar|webrick)/},
- %r{/gems/.*/(builder|rack|sinatra)/} ]
-
# produce json
- def self.json(&block)
+ def self.json(scope, &block)
headers = { 'type' => 'application/json', 'Cache-Control' => 'no-cache' }
- builder = JsonBuilder.new
- output = builder.encode($params, &block)
+ builder = JsonBuilder.new(scope)
+ output = builder.encode(&block)
headers['status'] = "404 Not Found" if output == {}
rescue Exception => exception
- Wunderbar.error exception.inspect
headers['status'] = "500 Internal Server Error"
- backtrace = []
- exception.backtrace.each do |frame|
- next if HIDE_FRAME.any? {|re| frame =~ re}
- Wunderbar.warn " #{frame}"
- backtrace << frame
- end
- builder = JsonBuilder.new
- builder._exception exception.inspect
- builder._backtrace backtrace
+ builder._! Hash.new
+ builder._exception exception
ensure
- out?(headers) { builder.target! }
+ out?(scope, headers) { builder.target! }
end
# produce text
- def self.text &block
+ def self.text(scope, &block)
headers = {'type' => 'text/plain', 'charset' => 'UTF-8'}
- builder = TextBuilder.new
- output = builder.encode($params, &block)
+ builder = TextBuilder.new(scope)
+ output = builder.encode(&block)
headers['status'] = "404 Not Found" if output == ''
rescue Exception => exception
- Wunderbar.error exception.inspect
headers['status'] = "500 Internal Server Error"
- builder.puts unless builder.size == 0
- builder.puts exception.inspect
- exception.backtrace.each do |frame|
- next if HIDE_FRAME.any? {|re| frame =~ re}
- Wunderbar.warn " #{frame}"
- builder.puts " #{frame}"
- end
+ builder._exception exception
ensure
- out?(headers) { builder.target! }
+ out?(scope, headers) { builder.target! }
end
# Conditionally provide output, based on ETAG
- def self.out?(headers, &block)
+ def self.out?(scope, headers, &block)
content = block.call
require 'digest/md5'
etag = Digest::MD5.hexdigest(content)
- if $env.HTTP_IF_NONE_MATCH == etag.inspect
+ if scope.env['HTTP_IF_NONE_MATCH'] == etag.inspect
headers['Date'] = ::CGI.rfc1123_date(Time.now)
- $cgi.out headers.merge('status' => '304 Not Modified') do
+ scope.out headers.merge('status' => '304 Not Modified') do
''
end
else
- $cgi.out headers.merge('Etag' => etag.inspect) do
+ scope.out headers.merge('Etag' => etag.inspect) do
content
end
end
- rescue
+ rescue Exception => exception
+ Wunderbar.fatal exception.inspect
end
# produce html/xhtml
- def self.html(*args, &block)
+ def self.html(scope, *args, &block)
headers = { 'type' => 'text/html', 'charset' => 'UTF-8' }
headers['type'] = 'application/xhtml+xml' if @xhtml
- x = HtmlMarkup.new
+ x = HtmlMarkup.new(scope)
begin
if @xhtml
output = x.xhtml *args, &block
else
@@ -86,42 +68,32 @@
_head do
_title 'Internal Server Error'
end
_body do
_h1 'Internal Server Error'
- text = exception.inspect
- Wunderbar.error text
- exception.backtrace.each do |frame|
- next if HIDE_FRAME.any? {|re| frame =~ re}
- Wunderbar.warn " #{frame}"
- text += "\n #{frame}"
- end
-
- _pre text
+ _exception exception
end
end
end
- out?(headers) { output }
+ out?(scope, headers) { output }
end
- def self.call(env)
- require 'etc'
- $USER = ENV['REMOTE_USER'] ||= ENV['USER'] || Etc.getlogin
+ def self.call(scope)
+ env = scope.env
+ accept = env['HTTP_ACCEPT'].to_s
+ request_uri = env['REQUEST_URI'].to_s
- accept = $env.HTTP_ACCEPT.to_s
- request_uri = $env.REQUEST_URI.to_s
-
# implied request types
xhr_json = Wunderbar::Options::XHR_JSON || (accept =~ /json/)
text = Wunderbar::Options::TEXT ||
(accept =~ /plain/ and accept !~ /html/)
@xhtml = (accept =~ /xhtml/ or accept == '')
# overrides via the uri query parameter
- xhr_json ||= (request_uri =~ /\?json$/)
- text ||= (request_uri =~ /\?text$/)
+ xhr_json ||= (request_uri =~ /\?json$/)
+ text ||= (request_uri =~ /\?text$/)
# overrides via the command line
xhtml_override = ARGV.include?('--xhtml')
html_override = ARGV.include?('--html')
@@ -140,20 +112,20 @@
@xhtml = false unless xhtml_override
else
@xhtml = false if html_override
end
- self.html(*args, &block)
+ self.html(scope, *args, &block)
return
end
when :json
if xhr_json
- self.json(*args, &block)
+ self.json(scope, *args, &block)
return
end
when :text
if text
- self.text(*args, &block)
+ self.text(scope, *args, &block)
return
end
end
end
end