Sha256: cefce8112df104044bc93097a36bf95e222807be4dee5b9c1f634eeebbe250a8

Contents?: true

Size: 1.29 KB

Versions: 15

Compression:

Stored size: 1.29 KB

Contents

require 'rack'
require 'flipper/api/action_collection'

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

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

        @action_collection = ActionCollection.new
        @action_collection.add Api::V1::Actions::PercentageOfTimeGate
        @action_collection.add Api::V1::Actions::PercentageOfActorsGate
        @action_collection.add Api::V1::Actions::ActorsGate
        @action_collection.add Api::V1::Actions::GroupsGate
        @action_collection.add Api::V1::Actions::BooleanGate
        @action_collection.add Api::V1::Actions::ClearFeature
        @action_collection.add Api::V1::Actions::Feature
        @action_collection.add Api::V1::Actions::Features
      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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
flipper-api-0.16.0 lib/flipper/api/middleware.rb
flipper-api-0.15.0 lib/flipper/api/middleware.rb
flipper-api-0.14.0 lib/flipper/api/middleware.rb
flipper-api-0.13.0 lib/flipper/api/middleware.rb
flipper-api-0.13.0.beta1 lib/flipper/api/middleware.rb
flipper-api-0.12.2 lib/flipper/api/middleware.rb
flipper-api-0.12.1 lib/flipper/api/middleware.rb
flipper-api-0.12.0 lib/flipper/api/middleware.rb
flipper-api-0.11.0 lib/flipper/api/middleware.rb
flipper-api-0.11.0.rc1 lib/flipper/api/middleware.rb
flipper-api-0.11.0.beta9 lib/flipper/api/middleware.rb
flipper-api-0.11.0.beta8 lib/flipper/api/middleware.rb
flipper-api-0.11.0.beta7 lib/flipper/api/middleware.rb
flipper-api-0.11.0.beta6 lib/flipper/api/middleware.rb
flipper-api-0.11.0.beta5 lib/flipper/api/middleware.rb