File: errors_controller_resolver.rb

package info (click to toggle)
ruby-gaffe 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 204 kB
  • sloc: ruby: 221; makefile: 7
file content (41 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Gaffe
  class ErrorsControllerResolver
    # Accessors
    attr_reader :controller

    # Constants
    BUILTIN_CONTROLLER = lambda do
      require 'gaffe/errors_controller'
      Gaffe::ErrorsController
    end

    def initialize(env)
      @env = env
    end

    def resolved_controller
      # Use the configured controller first
      controller = Gaffe.configuration.errors_controller

      # Parse the request if multiple controllers are configured
      controller = request_controller(controller) if controller.is_a?(Hash)

      # Fall back on the builtin errors controller
      controller ||= BUILTIN_CONTROLLER.call

      # Make sure we return a Class
      controller.respond_to?(:constantize) ? controller.constantize : controller
    end

  private

    def request_controller(controllers)
      matched_controllers = controllers.find do |pattern, _|
        relative_url = @env['REQUEST_URI']
        absolute_url = @env['HTTP_HOST'] + relative_url
        [relative_url, absolute_url].any? { |url| (url =~ pattern).present? }
      end
      matched_controllers.try(:last)
    end
  end
end