Sha256: 9d3ebe43a6f4190c3a37e3ed0d440cccc6ac371b87925395977a1739f222ec18

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require_relative 'handlers/base_action_handler'
require_relative 'handlers/file_action_handler'
require_relative 'handlers/sql_action_handler'
require_relative 'handlers/verify_action_handler'
require_relative 'handlers/not_found_action_handler'

module RailsSpotlight
  module Middlewares
    class RequestHandler
      def initialize(app)
        @app = app
      end

      def call(env)
        path_info = env['PATH_INFO']
        action, content_type = path_info.match(%r{/__rails_spotlight/(.+)\.(\w+)$}).try(:captures)
        return handle(Rack::Request.new(env), action, content_type.try(:to_sym)) if action

        app.call(env)
      end

      private

      attr_reader :app

      def handle(request, action, content_type = :json)
        args = [SecureRandom.uuid, request, content_type]
        case action
        when 'file' then Handlers::FileActionHandler.new(*args).call
        when 'sql' then Handlers::SqlActionHandler.new(*args).call
        when 'verify' then Handlers::VerifyActionHandler.new(*args).call
        else
          Handlers::NotFoundActionHandler.new(*args).call
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_spotlight-0.1.7 lib/rails_spotlight/middlewares/request_handler.rb
rails_spotlight-0.1.6 lib/rails_spotlight/middlewares/request_handler.rb
rails_spotlight-0.1.5 lib/rails_spotlight/middlewares/request_handler.rb
rails_spotlight-0.1.4 lib/rails_spotlight/middlewares/request_handler.rb