Class: Goat::ReqHandler
Instance Attribute Summary collapse
-
#app_class ⇒ Object
we can’t know this at initialize time because we start using the req handler in context of Goat::App, which would lead to app = Goat::App, which is wrong.
Instance Method Summary collapse
- #add_mapping(opts) ⇒ Object
- #around_handler_binding(handler, type) ⇒ Object
- #find_hook(meth, path) ⇒ Object
- #handle_request(env) ⇒ Object
-
#initialize ⇒ ReqHandler
constructor
A new instance of ReqHandler.
- #mappings ⇒ Object
- #resp_for_error(e, app) ⇒ Object
- #run_after_handlers(app) ⇒ Object
- #run_around_handlers(app, type) ⇒ Object
- #run_before_handlers(app) ⇒ Object
Constructor Details
#initialize ⇒ ReqHandler
Returns a new instance of ReqHandler.
249 250 251 |
# File 'lib/goat.rb', line 249 def initialize @around_handler_bindings = {} end |
Instance Attribute Details
#app_class ⇒ Object
we can’t know this at initialize time because we start using the req handler in context of Goat::App, which would lead to app = Goat::App, which is wrong
247 248 249 |
# File 'lib/goat.rb', line 247 def app_class @app_class end |
Instance Method Details
#add_mapping(opts) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/goat.rb', line 263 def add_mapping(opts) opts = { :metal => false, :method => :get }.merge(opts) %w{method path hook}.each do |key| raise "#{key} must be set" unless opts[key.to_sym] end method = opts[:method] path = opts[:path] hook = opts[:hook] if hook.kind_of?(Proc) && !opts[:metal] hook = App.bind(opts[:hook]) end mappings[method][path] = hook end |
#around_handler_binding(handler, type) ⇒ Object
284 285 286 287 288 289 290 291 292 |
# File 'lib/goat.rb', line 284 def around_handler_binding(handler, type) if type == :before @around_handler_bindings[handler] ||= App.bind(handler) elsif type == :after @around_handler_bindings[handler] ||= App.bind(handler, nil, false) else raise 'bad handler type' end end |
#find_hook(meth, path) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/goat.rb', line 333 def find_hook(meth, path) hooks = mappings[meth.downcase.to_sym] if h = hooks[path] return h else hooks.each do |pathspec, hook| if pathspec.kind_of?(Regexp) && pathspec =~ path return hook end end end nil end |
#handle_request(env) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/goat.rb', line 348 def handle_request(env) path = env['PATH_INFO'] meth = env['REQUEST_METHOD'] hook = find_hook(meth, path) hdrs = {} resp = nil req = Rack::Request.new(env) Dynamic.let(:current_request => req) do begin app = @app_class.new(req) begin run_before_handlers(app) rescue Halt => halt return halt.response.to_a end if hook resp = hook.handle_request(app) else raise NotFoundError.new(path) end rescue Halt => halt resp = halt.response rescue Exception => e resp = resp_for_error(e, app) end run_after_handlers(app) end resp.to_a end |
#mappings ⇒ Object
259 260 261 |
# File 'lib/goat.rb', line 259 def mappings @map ||= Hash.new {|h, k| h[k] = Hash.new} end |
#resp_for_error(e, app) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/goat.rb', line 303 def resp_for_error(e, app) resp = nil if e.kind_of?(NotFoundError) # 404 if app.class.not_found_handler @not_found_binding ||= App.bind(app.class.not_found_handler) resp = @not_found_binding.call(app, e) else resp = [404, {}, 'not found'] end else # not a 404 -- an actual problem if app.class.error_handler @error_handler_binding ||= App.bind(app.class.error_handler) resp = @error_handler_binding.call(app, e) else Goat.loge e.inspect Goat.loge e.backtrace.join("\n") resp = Rack::Response.new resp.status = 500 resp['Content-Type'] = 'text/plain' resp.body = e.inspect + "\n" + e.backtrace.join("\n") end end resp end |
#run_after_handlers(app) ⇒ Object
301 |
# File 'lib/goat.rb', line 301 def run_after_handlers(app); run_around_handlers(app, :after); end |
#run_around_handlers(app, type) ⇒ Object
294 295 296 297 298 |
# File 'lib/goat.rb', line 294 def run_around_handlers(app, type) app.class.around_handlers.select{|h| h.first == type}.each do |handler| around_handler_binding(handler[1], type).call(app) end end |
#run_before_handlers(app) ⇒ Object
300 |
# File 'lib/goat.rb', line 300 def run_before_handlers(app); run_around_handlers(app, :before); end |