Sha256: 1ee90eceb76a47eaabaee6ffb0f1f09a3c65f316d39a0857a20264041284cb33

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 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
      # Public: Initializes an instance of the API middleware.
      #
      # app - The app this middleware is included in.
      # flipper_or_block - The Flipper::DSL instance or a block that yields a
      #                    Flipper::DSL instance to use for all operations.
      #
      # Examples
      #
      #   flipper = Flipper.new(...)
      #
      #   # using with a normal flipper instance
      #   use Flipper::Api::Middleware, flipper
      #
      #   # using with a block that yields a flipper instance
      #   use Flipper::Api::Middleware, lambda { Flipper.new(...) }
      #
      def initialize(app, flipper_or_block)
        @app = app

        if flipper_or_block.respond_to?(:call)
          @flipper_block = flipper_or_block
        else
          @flipper = flipper_or_block
        end

        @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::Feature
        @action_collection.add Api::V1::Actions::Features
      end

      def flipper
        @flipper ||= @flipper_block.call
      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.status = 404
          @app.call(env)
        else
          action_class.run(flipper, request)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flipper-api-0.10.2 lib/flipper/api/middleware.rb
flipper-api-0.10.1 lib/flipper/api/middleware.rb
flipper-api-0.10.0 lib/flipper/api/middleware.rb