lib/jellyfish.rb in jellyfish-0.9.2 vs lib/jellyfish.rb in jellyfish-1.0.0
- old
+ new
@@ -1,19 +1,20 @@
module Jellyfish
autoload :VERSION , 'jellyfish/version'
autoload :Sinatra , 'jellyfish/sinatra'
+ autoload :Swagger , 'jellyfish/swagger'
autoload :NewRelic, 'jellyfish/newrelic'
autoload :MultiActions , 'jellyfish/multi_actions'
autoload :NormalizedParams, 'jellyfish/normalized_params'
autoload :NormalizedPath , 'jellyfish/normalized_path'
autoload :ChunkedBody, 'jellyfish/chunked_body'
+ Cascade = Object.new
GetValue = Object.new
- Identity = lambda{|_|_}
class Response < RuntimeError
def headers
@headers ||= {'Content-Type' => 'text/html'}
end
@@ -47,20 +48,22 @@
block_call(*dispatch)
end
def block_call argument, block
val = instance_exec(argument, &block)
- [status || 200, headers || {}, body || with_each(val || '')]
+ [status || 200, headers || {}, body || rack_body(val || '')]
rescue LocalJumpError
- jellyfish.log("Use `next' if you're trying to `return' or" \
- " `break' from the block.", env['rack.errors'])
+ log("Use `next' if you're trying to `return' or `break' from a block.")
raise
end
+ def log message; jellyfish.log( message, env['rack.errors']); end
+ def log_error error; jellyfish.log_error(error, env['rack.errors']); end
def request ; @request ||= Rack::Request.new(env); end
def halt *args; throw(:halt, *args) ; end
- def forward ; halt(Jellyfish::NotFound.new) ; end
+ def cascade ; halt(Jellyfish::Cascade) ; end
+ def not_found ; halt(Jellyfish::NotFound.new) ; end
def found url; halt(Jellyfish:: Found.new(url)); end
alias_method :redirect, :found
def path_info ; env['PATH_INFO'] || '/' ; end
def request_method; env['REQUEST_METHOD'] || 'GET'; end
@@ -81,11 +84,11 @@
if value == GetValue
@body
elsif value.nil?
@body = value
else
- @body = with_each(value)
+ @body = rack_body(value)
end
end
def headers_merge value
if headers.nil?
@@ -95,11 +98,11 @@
end
end
private
def actions
- routes[request_method.downcase] || halt(Jellyfish::NotFound.new)
+ routes[request_method.downcase] || action_missing
end
def dispatch
actions.find{ |(route, block)|
case route
@@ -107,16 +110,20 @@
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
- } || halt(Jellyfish::NotFound.new)
+ } || action_missing
end
- def with_each value
- if value.respond_to?(:each) then value else [value] end
+ def action_missing
+ if jellyfish.app then cascade else not_found end
end
+
+ def rack_body v
+ if v.respond_to?(:each) || v.respond_to?(:to_path) then v else [v] end
+ end
end
# -----------------------------------------------------------------
module DSL
@@ -149,14 +156,14 @@
inject(value){ |ctrl, mod| ctrl.__send__(:include, mod) }
end
%w[options get head post put delete patch].each do |method|
module_eval <<-RUBY
- def #{method} route=//, &block
+ def #{method} route=//, meta={}, &block
raise TypeError.new("Route \#{route} should respond to :match") \
unless route.respond_to?(:match)
- (routes['#{method}'] ||= []) << [route, block]
+ (routes['#{method}'] ||= []) << [route, block || lambda{|_|}, meta]
end
RUBY
end
def inherited sub
@@ -174,16 +181,16 @@
def initialize app=nil; @app = app; end
def call env
ctrl = self.class.controller.new(self.class.routes, self)
case res = catch(:halt){ ctrl.call(env) }
- when NotFound
- app && forward(ctrl, env) || handle(ctrl, res)
+ when Cascade
+ cascade(ctrl, env)
when Response
handle(ctrl, res, env['rack.errors'])
- else
- res || ctrl.block_call(nil, Identity)
+ else # make sure we return rack triple
+ res || ctrl.block_call(nil, lambda{|_|_})
end
rescue => e
handle(ctrl, e, env['rack.errors'])
end
@@ -196,10 +203,10 @@
return unless stderr
stderr.puts("[#{self.class.name}] #{msg}")
end
private
- def forward ctrl, env
+ def cascade ctrl, env
app.call(env)
rescue => e
handle(ctrl, e, env['rack.errors'])
end