File: middleware.rb

package info (click to toggle)
ruby-gitlab-experiment 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,202; makefile: 7
file content (27 lines) | stat: -rw-r--r-- 734 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

module Gitlab
  class Experiment
    class Middleware
      def self.redirect(id, url)
        raise Error, 'no url to redirect to' if url.blank?

        experiment = Gitlab::Experiment.from_param(id)
        [303, { 'Location' => experiment.process_redirect_url(url) || raise(Error, 'not redirecting') }, []]
      end

      def initialize(app, base_path)
        @app = app
        @matcher = %r{^#{base_path}/(?<id>.+)}
      end

      def call(env)
        return @app.call(env) if env['REQUEST_METHOD'] != 'GET' || (match = @matcher.match(env['PATH_INFO'])).nil?

        Middleware.redirect(match[:id], env['QUERY_STRING'])
      rescue Error
        @app.call(env)
      end
    end
  end
end