Sha256: b9718463c7711410e049b6c82c610a9154275ff76015842431d54cded5e92f48

Contents?: true

Size: 1.43 KB

Versions: 39

Compression:

Stored size: 1.43 KB

Contents

require 'flipper/api/action'
require 'flipper/api/v1/decorators/feature'

module Flipper
  module Api
    module V1
      module Actions
        class PercentageOfTimeGate < Api::Action
          include FeatureNameFromRoute

          route %r{\A/features/(?<feature_name>.*)/percentage_of_time/?\Z}

          def post
            if percentage < 0 || percentage > 100
              json_error_response(:percentage_invalid)
            end

            feature = flipper[feature_name]
            feature.enable_percentage_of_time(percentage)
            decorated_feature = Decorators::Feature.new(feature)
            json_response(decorated_feature.as_json, 200)
          end

          def delete
            feature = flipper[feature_name]
            feature.disable_percentage_of_time

            decorated_feature = Decorators::Feature.new(feature)
            json_response(decorated_feature.as_json, 200)
          end

          private

          def percentage_param
            @percentage_param ||= params['percentage'].to_s
          end

          def percentage
            @percentage ||= begin
              unless percentage_param.match(/\d/)
                raise ArgumentError, "invalid numeric value: #{percentage_param}"
              end

              Flipper::Types::Percentage.new(percentage_param).value
            rescue ArgumentError, TypeError
              -1
            end
          end
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
flipper-api-1.3.2 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.3.1 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.3.0 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.3.0.pre lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.2.2 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.2.1 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.2.0 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.1.2 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.1.1 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.1.0 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.0.0 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-1.0.0.pre lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.28.3 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.28.2 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.28.1 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.28.0 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.27.1 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.27.0 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.26.2 lib/flipper/api/v1/actions/percentage_of_time_gate.rb
flipper-api-0.26.1 lib/flipper/api/v1/actions/percentage_of_time_gate.rb