File: features.rb

package info (click to toggle)
ruby-flipper 0.26.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,288 kB
  • sloc: ruby: 16,377; sh: 61; javascript: 24; makefile: 14
file content (53 lines) | stat: -rw-r--r-- 1,477 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
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 'flipper/api/action'
require 'flipper/api/v1/decorators/feature'

module Flipper
  module Api
    module V1
      module Actions
        class Features < Api::Action
          route %r{\A/features/?\Z}

          def get
            keys = params['keys']
            exclude_gates = params['exclude_gates']&.downcase == "true"
            features = if keys
              names = keys.split(',')
              if names.empty?
                []
              else
                existing_feature_names = names.keep_if do |feature_name|
                  feature_exists?(feature_name)
                end

                flipper.preload(existing_feature_names)
              end
            else
              flipper.features
            end

            decorated_features = features.map do |feature|
              Decorators::Feature.new(feature).as_json(exclude_gates: exclude_gates)
            end

            json_response(features: decorated_features)
          end

          def post
            feature_name = params.fetch('name') { json_error_response(:name_invalid) }
            feature = flipper[feature_name]
            feature.add
            decorated_feature = Decorators::Feature.new(feature)
            json_response(decorated_feature.as_json, 200)
          end

          private

          def feature_exists?(feature_name)
            flipper.features.map(&:key).include?(feature_name)
          end
        end
      end
    end
  end
end