lib/jellyfish.rb in jellyfish-0.4.0 vs lib/jellyfish.rb in jellyfish-0.5.0
- old
+ new
@@ -1,44 +1,39 @@
module Jellyfish
autoload :VERSION, 'jellyfish/version'
autoload :Sinatra, 'jellyfish/sinatra'
- REQUEST_METHOD = 'REQUEST_METHOD'
- PATH_INFO = 'PATH_INFO'
- LOCATION = 'Location'
- RACK_ERRORS = 'rack.errors'
-
# -----------------------------------------------------------------
- class Respond < RuntimeError
+ class Response < RuntimeError
def headers
@headers ||= {'Content-Type' => 'text/html'}
end
def body
@body ||= [File.read("#{Jellyfish.public_root}/#{status}.html")]
end
end
- class InternalError < Respond; def status; 500; end; end
- class NotFound < Respond; def status; 404; end; end
- class Found < Respond
+ class InternalError < Response; def status; 500; end; end
+ class NotFound < Response; def status; 404; end; end
+ class Found < Response # this would be raised in redirect
attr_reader :url
def initialize url; @url = url ; end
def status ; 302 ; end
- def headers ; super.merge(LOCATION => url) ; end
+ def headers ; super.merge('Location' => url) ; end
def body ; super.map{ |b| b.gsub('VAR_URL', url) }; end
end
# -----------------------------------------------------------------
class Controller
- include Jellyfish
- attr_reader :routes, :env
- def initialize routes
- @routes = routes
+ attr_reader :routes, :jellyfish, :env
+ def initialize routes, jellyfish
+ @routes, @jellyfish = routes, jellyfish
+ @status, @headers, @body = nil
end
def call env
@env = env
block_call(*dispatch)
@@ -48,16 +43,16 @@
ret = instance_exec(argument, &block)
body ret if body.nil? # prefer explicitly set values
[status || 200, headers || {}, body]
end
- def forward ; raise(NotFound.new) ; end
- def found url; raise(Found.new(url)); end
+ def forward ; raise(Jellyfish::NotFound.new) ; end
+ def found url; raise(Jellyfish:: Found.new(url)); end
alias_method :redirect, :found
- def path_info ; env[PATH_INFO] || '/' ; end
- def request_method; env[REQUEST_METHOD] || 'GET'; end
+ def path_info ; env['PATH_INFO'] || '/' ; end
+ def request_method; env['REQUEST_METHOD'] || 'GET'; end
%w[status headers].each do |field|
module_eval <<-RUBY
def #{field} value=nil
if value.nil?
@@ -87,11 +82,11 @@
end
end
private
def actions
- routes[request_method.downcase] || raise(NotFound.new)
+ routes[request_method.downcase] || raise(Jellyfish::NotFound.new)
end
def dispatch
actions.find{ |(route, block)|
case route
@@ -99,11 +94,11 @@
break route, block if route == path_info
else#Regexp, using else allows you to use custom matcher
match = route.match(path_info)
break match, block if match
end
- } || raise(NotFound.new)
+ } || raise(Jellyfish::NotFound.new)
end
end
# -----------------------------------------------------------------
@@ -134,36 +129,37 @@
def initialize app=nil; @app = app; end
def controller ; Controller; end
def call env
- ctrl = controller.new(self.class.routes)
+ ctrl = controller.new(self.class.routes, self)
ctrl.call(env)
rescue NotFound => e # forward
if app
protect(ctrl, env){ app.call(env) }
else
handle(ctrl, e)
end
rescue Exception => e
- handle(ctrl, e, env[RACK_ERRORS])
+ handle(ctrl, e, env['rack.errors'])
end
def protect ctrl, env
yield
rescue Exception => e
- handle(ctrl, e, env[RACK_ERRORS])
+ handle(ctrl, e, env['rack.errors'])
end
private
def handle ctrl, e, stderr=nil
- raise e unless self.class.handle_exceptions
handler = self.class.handlers.find{ |klass, block|
break block if e.kind_of?(klass)
}
if handler
ctrl.block_call(e, handler)
- elsif e.kind_of?(Respond) # InternalError ends up here if no handlers
+ elsif !self.class.handle_exceptions
+ raise e
+ elsif e.kind_of?(Response) # InternalError ends up here if no handlers
[e.status, e.headers, e.body]
else # fallback and see if there's any InternalError handler
log_error(e, stderr)
handle(ctrl, InternalError.new)
end