File: middleware.rb

package info (click to toggle)
ruby-flipper 0.13.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,824 kB
  • sloc: ruby: 13,183; sh: 54; makefile: 14
file content (53 lines) | stat: -rw-r--r-- 1,501 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
42
43
44
45
46
47
48
49
50
51
52
53
require 'rack'
require 'flipper/ui/action_collection'

# Require all actions automatically.
Pathname(__FILE__).dirname.join('actions').each_child(false) do |name|
  require "flipper/ui/actions/#{name}"
end

module Flipper
  module UI
    class Middleware
      def initialize(app, options = {})
        @app = app
        @env_key = options.fetch(:env_key, 'flipper')

        @action_collection = ActionCollection.new

        # UI
        @action_collection.add UI::Actions::Features
        @action_collection.add UI::Actions::AddFeature
        @action_collection.add UI::Actions::Feature
        @action_collection.add UI::Actions::ActorsGate
        @action_collection.add UI::Actions::GroupsGate
        @action_collection.add UI::Actions::BooleanGate
        @action_collection.add UI::Actions::PercentageOfTimeGate
        @action_collection.add UI::Actions::PercentageOfActorsGate
        @action_collection.add UI::Actions::Gate

        # Static Assets/Files
        @action_collection.add UI::Actions::File

        # Catch all redirect to features
        @action_collection.add UI::Actions::Home
      end

      def call(env)
        dup.call!(env)
      end

      def call!(env)
        request = Rack::Request.new(env)
        action_class = @action_collection.action_for_request(request)

        if action_class.nil?
          @app.call(env)
        else
          flipper = env.fetch(@env_key)
          action_class.run(flipper, request)
        end
      end
    end
  end
end