flipper 0.20.0 → 0.21.0.rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +57 -0
- data/Changelog.md +37 -1
- data/Gemfile +1 -0
- data/README.md +103 -47
- data/docs/Adapters.md +1 -0
- data/docs/Gates.md +74 -74
- data/docs/Optimization.md +7 -7
- data/docs/images/banner.jpg +0 -0
- data/examples/basic.rb +1 -12
- data/examples/configuring_default.rb +1 -2
- data/examples/dsl.rb +13 -24
- data/examples/enabled_for_actor.rb +8 -15
- data/examples/group.rb +3 -6
- data/examples/group_dynamic_lookup.rb +5 -19
- data/examples/group_with_members.rb +4 -14
- data/examples/importing.rb +1 -1
- data/examples/individual_actor.rb +2 -5
- data/examples/instrumentation.rb +1 -2
- data/examples/memoizing.rb +1 -2
- data/examples/percentage_of_actors.rb +6 -16
- data/examples/percentage_of_actors_enabled_check.rb +7 -10
- data/examples/percentage_of_actors_group.rb +5 -18
- data/examples/percentage_of_time.rb +3 -6
- data/lib/flipper.rb +1 -0
- data/lib/flipper/adapters/http.rb +32 -28
- data/lib/flipper/adapters/memory.rb +20 -94
- data/lib/flipper/adapters/sync/interval_synchronizer.rb +1 -1
- data/lib/flipper/configuration.rb +6 -6
- data/lib/flipper/errors.rb +2 -3
- data/lib/flipper/identifier.rb +17 -0
- data/lib/flipper/version.rb +1 -1
- data/spec/flipper/adapters/http_spec.rb +74 -8
- data/spec/flipper/adapters/memory_spec.rb +21 -1
- data/spec/flipper/configuration_spec.rb +5 -2
- data/spec/flipper/identifier_spec.rb +14 -0
- data/spec/flipper/middleware/memoizer_spec.rb +1 -1
- data/spec/flipper/middleware/setup_env_spec.rb +0 -16
- data/spec/flipper_spec.rb +0 -1
- data/spec/support/spec_helpers.rb +3 -0
- data/test/test_helper.rb +1 -0
- metadata +9 -5
- data/examples/example_setup.rb +0 -8
@@ -1,13 +1,10 @@
|
|
1
|
-
require
|
2
|
-
|
1
|
+
require 'bundler/setup'
|
3
2
|
require 'flipper'
|
4
3
|
|
5
|
-
|
6
|
-
flipper = Flipper.new(adapter)
|
7
|
-
logging = flipper[:logging]
|
4
|
+
logging = Flipper[:logging]
|
8
5
|
|
9
6
|
perform_test = lambda do |number|
|
10
|
-
logging.
|
7
|
+
logging.enable_percentage_of_time number
|
11
8
|
|
12
9
|
total = 100_000
|
13
10
|
enabled = []
|
data/lib/flipper.rb
CHANGED
@@ -152,6 +152,7 @@ require 'flipper/feature'
|
|
152
152
|
require 'flipper/gate'
|
153
153
|
require 'flipper/instrumenters/memory'
|
154
154
|
require 'flipper/instrumenters/noop'
|
155
|
+
require 'flipper/identifier'
|
155
156
|
require 'flipper/middleware/memoizer'
|
156
157
|
require 'flipper/middleware/setup_env'
|
157
158
|
require 'flipper/registry'
|
@@ -35,12 +35,6 @@ module Flipper
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
def add(feature)
|
39
|
-
body = JSON.generate(name: feature.key)
|
40
|
-
response = @client.post('/features', body)
|
41
|
-
response.is_a?(Net::HTTPOK)
|
42
|
-
end
|
43
|
-
|
44
38
|
def get_multi(features)
|
45
39
|
csv_keys = features.map(&:key).join(',')
|
46
40
|
response = @client.get("/features?keys=#{csv_keys}")
|
@@ -87,51 +81,61 @@ module Flipper
|
|
87
81
|
parsed_response['features'].map { |feature| feature['key'] }.to_set
|
88
82
|
end
|
89
83
|
|
84
|
+
def add(feature)
|
85
|
+
body = JSON.generate(name: feature.key)
|
86
|
+
response = @client.post('/features', body)
|
87
|
+
raise Error, response unless response.is_a?(Net::HTTPOK)
|
88
|
+
true
|
89
|
+
end
|
90
|
+
|
90
91
|
def remove(feature)
|
91
92
|
response = @client.delete("/features/#{feature.key}")
|
92
|
-
response.is_a?(Net::HTTPNoContent)
|
93
|
+
raise Error, response unless response.is_a?(Net::HTTPNoContent)
|
94
|
+
true
|
93
95
|
end
|
94
96
|
|
95
97
|
def enable(feature, gate, thing)
|
96
98
|
body = request_body_for_gate(gate, thing.value.to_s)
|
97
99
|
query_string = gate.key == :groups ? "?allow_unregistered_groups=true" : ""
|
98
100
|
response = @client.post("/features/#{feature.key}/#{gate.key}#{query_string}", body)
|
99
|
-
response.is_a?(Net::HTTPOK)
|
101
|
+
raise Error, response unless response.is_a?(Net::HTTPOK)
|
102
|
+
true
|
100
103
|
end
|
101
104
|
|
102
105
|
def disable(feature, gate, thing)
|
103
106
|
body = request_body_for_gate(gate, thing.value.to_s)
|
104
107
|
query_string = gate.key == :groups ? "?allow_unregistered_groups=true" : ""
|
105
|
-
response =
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
108
|
+
response = case gate.key
|
109
|
+
when :percentage_of_actors, :percentage_of_time
|
110
|
+
@client.post("/features/#{feature.key}/#{gate.key}#{query_string}", body)
|
111
|
+
else
|
112
|
+
@client.delete("/features/#{feature.key}/#{gate.key}#{query_string}", body)
|
113
|
+
end
|
114
|
+
raise Error, response unless response.is_a?(Net::HTTPOK)
|
115
|
+
true
|
113
116
|
end
|
114
117
|
|
115
118
|
def clear(feature)
|
116
119
|
response = @client.delete("/features/#{feature.key}/clear")
|
117
|
-
response.is_a?(Net::HTTPNoContent)
|
120
|
+
raise Error, response unless response.is_a?(Net::HTTPNoContent)
|
121
|
+
true
|
118
122
|
end
|
119
123
|
|
120
124
|
private
|
121
125
|
|
122
126
|
def request_body_for_gate(gate, value)
|
123
127
|
data = case gate.key
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
128
|
+
when :boolean
|
129
|
+
{}
|
130
|
+
when :groups
|
131
|
+
{ name: value }
|
132
|
+
when :actors
|
133
|
+
{ flipper_id: value }
|
134
|
+
when :percentage_of_actors, :percentage_of_time
|
135
|
+
{ percentage: value }
|
136
|
+
else
|
137
|
+
raise "#{gate.key} is not a valid flipper gate key"
|
138
|
+
end
|
135
139
|
JSON.generate(data)
|
136
140
|
end
|
137
141
|
|
@@ -20,58 +20,57 @@ module Flipper
|
|
20
20
|
|
21
21
|
# Public: The set of known features.
|
22
22
|
def features
|
23
|
-
|
23
|
+
@source.keys.to_set
|
24
24
|
end
|
25
25
|
|
26
26
|
# Public: Adds a feature to the set of known features.
|
27
27
|
def add(feature)
|
28
|
-
|
28
|
+
@source[feature.key] ||= default_config
|
29
29
|
true
|
30
30
|
end
|
31
31
|
|
32
32
|
# Public: Removes a feature from the set of known features and clears
|
33
33
|
# all the values for the feature.
|
34
34
|
def remove(feature)
|
35
|
-
|
36
|
-
clear(feature)
|
35
|
+
@source.delete(feature.key)
|
37
36
|
true
|
38
37
|
end
|
39
38
|
|
40
39
|
# Public: Clears all the gate values for a feature.
|
41
40
|
def clear(feature)
|
42
|
-
feature.
|
43
|
-
delete key(feature, gate)
|
44
|
-
end
|
41
|
+
@source[feature.key] = default_config
|
45
42
|
true
|
46
43
|
end
|
47
44
|
|
48
45
|
# Public
|
49
46
|
def get(feature)
|
50
|
-
|
47
|
+
@source[feature.key] || default_config
|
51
48
|
end
|
52
49
|
|
53
50
|
def get_multi(features)
|
54
|
-
|
51
|
+
result = {}
|
52
|
+
features.each do |feature|
|
53
|
+
result[feature.key] = @source[feature.key] || default_config
|
54
|
+
end
|
55
|
+
result
|
55
56
|
end
|
56
57
|
|
57
58
|
def get_all
|
58
|
-
|
59
|
-
Flipper::Feature.new(key, self)
|
60
|
-
end
|
61
|
-
|
62
|
-
read_many_features(features)
|
59
|
+
@source
|
63
60
|
end
|
64
61
|
|
65
62
|
# Public
|
66
63
|
def enable(feature, gate, thing)
|
64
|
+
@source[feature.key] ||= default_config
|
65
|
+
|
67
66
|
case gate.data_type
|
68
67
|
when :boolean
|
69
68
|
clear(feature)
|
70
|
-
|
69
|
+
@source[feature.key][gate.key] = thing.value.to_s
|
71
70
|
when :integer
|
72
|
-
|
71
|
+
@source[feature.key][gate.key] = thing.value.to_s
|
73
72
|
when :set
|
74
|
-
|
73
|
+
@source[feature.key][gate.key] << thing.value.to_s
|
75
74
|
else
|
76
75
|
raise "#{gate} is not supported by this adapter yet"
|
77
76
|
end
|
@@ -81,13 +80,15 @@ module Flipper
|
|
81
80
|
|
82
81
|
# Public
|
83
82
|
def disable(feature, gate, thing)
|
83
|
+
@source[feature.key] ||= default_config
|
84
|
+
|
84
85
|
case gate.data_type
|
85
86
|
when :boolean
|
86
87
|
clear(feature)
|
87
88
|
when :integer
|
88
|
-
|
89
|
+
@source[feature.key][gate.key] = thing.value.to_s
|
89
90
|
when :set
|
90
|
-
|
91
|
+
@source[feature.key][gate.key].delete thing.value.to_s
|
91
92
|
else
|
92
93
|
raise "#{gate} is not supported by this adapter yet"
|
93
94
|
end
|
@@ -103,81 +104,6 @@ module Flipper
|
|
103
104
|
]
|
104
105
|
"#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
|
105
106
|
end
|
106
|
-
|
107
|
-
private
|
108
|
-
|
109
|
-
def read_feature_keys
|
110
|
-
set_members(FeaturesKey)
|
111
|
-
end
|
112
|
-
|
113
|
-
# Private
|
114
|
-
def key(feature, gate)
|
115
|
-
"feature/#{feature.key}/#{gate.key}"
|
116
|
-
end
|
117
|
-
|
118
|
-
def read_many_features(features)
|
119
|
-
result = {}
|
120
|
-
features.each do |feature|
|
121
|
-
result[feature.key] = read_feature(feature)
|
122
|
-
end
|
123
|
-
result
|
124
|
-
end
|
125
|
-
|
126
|
-
def read_feature(feature)
|
127
|
-
result = {}
|
128
|
-
|
129
|
-
feature.gates.each do |gate|
|
130
|
-
result[gate.key] =
|
131
|
-
case gate.data_type
|
132
|
-
when :boolean, :integer
|
133
|
-
read key(feature, gate)
|
134
|
-
when :set
|
135
|
-
set_members key(feature, gate)
|
136
|
-
else
|
137
|
-
raise "#{gate} is not supported by this adapter yet"
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
result
|
142
|
-
end
|
143
|
-
|
144
|
-
# Private
|
145
|
-
def read(key)
|
146
|
-
@source[key.to_s]
|
147
|
-
end
|
148
|
-
|
149
|
-
# Private
|
150
|
-
def write(key, value)
|
151
|
-
@source[key.to_s] = value.to_s
|
152
|
-
end
|
153
|
-
|
154
|
-
# Private
|
155
|
-
def delete(key)
|
156
|
-
@source.delete(key.to_s)
|
157
|
-
end
|
158
|
-
|
159
|
-
# Private
|
160
|
-
def set_add(key, value)
|
161
|
-
ensure_set_initialized(key)
|
162
|
-
@source[key.to_s].add(value.to_s)
|
163
|
-
end
|
164
|
-
|
165
|
-
# Private
|
166
|
-
def set_delete(key, value)
|
167
|
-
ensure_set_initialized(key)
|
168
|
-
@source[key.to_s].delete(value.to_s)
|
169
|
-
end
|
170
|
-
|
171
|
-
# Private
|
172
|
-
def set_members(key)
|
173
|
-
ensure_set_initialized(key)
|
174
|
-
@source[key.to_s]
|
175
|
-
end
|
176
|
-
|
177
|
-
# Private
|
178
|
-
def ensure_set_initialized(key)
|
179
|
-
@source[key.to_s] ||= Set.new
|
180
|
-
end
|
181
107
|
end
|
182
108
|
end
|
183
109
|
end
|
@@ -19,7 +19,7 @@ module Flipper
|
|
19
19
|
# Public: Initializes a new interval synchronizer.
|
20
20
|
#
|
21
21
|
# synchronizer - The Synchronizer to call when the interval has passed.
|
22
|
-
# interval - The Integer number of
|
22
|
+
# interval - The Integer number of seconds between invocations of
|
23
23
|
# the wrapped synchronizer.
|
24
24
|
def initialize(synchronizer, interval: nil)
|
25
25
|
@synchronizer = synchronizer
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Flipper
|
2
2
|
class Configuration
|
3
3
|
def initialize
|
4
|
-
@default = -> {
|
4
|
+
@default = -> { Flipper.new(Flipper::Adapters::Memory.new) }
|
5
5
|
end
|
6
6
|
|
7
7
|
# Controls the default instance for flipper. When used with a block it
|
@@ -9,15 +9,15 @@ module Flipper
|
|
9
9
|
# without a block, it performs a block invocation and returns the result.
|
10
10
|
#
|
11
11
|
# configuration = Flipper::Configuration.new
|
12
|
-
# configuration.default # =>
|
12
|
+
# configuration.default # => Flipper::DSL instance using Memory adapter
|
13
13
|
#
|
14
|
-
# # sets the default block to generate a new instance using
|
14
|
+
# # sets the default block to generate a new instance using ActiveRecord adapter
|
15
15
|
# configuration.default do
|
16
|
-
# require "flipper
|
17
|
-
# Flipper.new(Flipper::Adapters::
|
16
|
+
# require "flipper-active_record"
|
17
|
+
# Flipper.new(Flipper::Adapters::ActiveRecord.new)
|
18
18
|
# end
|
19
19
|
#
|
20
|
-
# configuration.default # => Flipper::DSL instance using
|
20
|
+
# configuration.default # => Flipper::DSL instance using ActiveRecord adapter
|
21
21
|
#
|
22
22
|
# Returns result of default block invocation if called without block. If
|
23
23
|
# called with block, assigns the default block.
|
data/lib/flipper/errors.rb
CHANGED
@@ -16,9 +16,8 @@ module Flipper
|
|
16
16
|
# use it.
|
17
17
|
class DefaultNotSet < Flipper::Error
|
18
18
|
def initialize(message = nil)
|
19
|
-
|
20
|
-
|
21
|
-
super(message || default)
|
19
|
+
warn "Flipper::DefaultNotSet is deprecated and will be removed in 1.0"
|
20
|
+
super
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Flipper
|
2
|
+
# A default implementation of `#flipper_id` for actors.
|
3
|
+
#
|
4
|
+
# class User < Struct.new(:id)
|
5
|
+
# include Flipper::Identifier
|
6
|
+
# end
|
7
|
+
#
|
8
|
+
# user = User.new(99)
|
9
|
+
# Flipper.enable :thing, user
|
10
|
+
# Flipper.enabled? :thing, user #=> true
|
11
|
+
#
|
12
|
+
module Identifier
|
13
|
+
def flipper_id
|
14
|
+
"#{self.class.name};#{id}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/flipper/version.rb
CHANGED
@@ -76,9 +76,9 @@ RSpec.describe Flipper::Adapters::Http do
|
|
76
76
|
.to_return(status: 503, body: "", headers: {})
|
77
77
|
|
78
78
|
adapter = described_class.new(url: 'http://app.com/flipper')
|
79
|
-
expect
|
79
|
+
expect {
|
80
80
|
adapter.get(flipper[:feature_panel])
|
81
|
-
|
81
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
@@ -88,9 +88,9 @@ RSpec.describe Flipper::Adapters::Http do
|
|
88
88
|
.to_return(status: 503, body: "", headers: {})
|
89
89
|
|
90
90
|
adapter = described_class.new(url: 'http://app.com/flipper')
|
91
|
-
expect
|
91
|
+
expect {
|
92
92
|
adapter.get_multi([flipper[:feature_panel]])
|
93
|
-
|
93
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
@@ -100,9 +100,9 @@ RSpec.describe Flipper::Adapters::Http do
|
|
100
100
|
.to_return(status: 503, body: "", headers: {})
|
101
101
|
|
102
102
|
adapter = described_class.new(url: 'http://app.com/flipper')
|
103
|
-
expect
|
103
|
+
expect {
|
104
104
|
adapter.get_all
|
105
|
-
|
105
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -112,9 +112,75 @@ RSpec.describe Flipper::Adapters::Http do
|
|
112
112
|
.to_return(status: 503, body: "", headers: {})
|
113
113
|
|
114
114
|
adapter = described_class.new(url: 'http://app.com/flipper')
|
115
|
-
expect
|
115
|
+
expect {
|
116
116
|
adapter.features
|
117
|
-
|
117
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "#add" do
|
122
|
+
it "raises error when not successful" do
|
123
|
+
stub_request(:post, /app.com/)
|
124
|
+
.to_return(status: 503, body: "{}", headers: {})
|
125
|
+
|
126
|
+
adapter = described_class.new(url: 'http://app.com/flipper')
|
127
|
+
expect {
|
128
|
+
adapter.add(Flipper::Feature.new(:search, adapter))
|
129
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "#remove" do
|
134
|
+
it "raises error when not successful" do
|
135
|
+
stub_request(:delete, /app.com/)
|
136
|
+
.to_return(status: 503, body: "{}", headers: {})
|
137
|
+
|
138
|
+
adapter = described_class.new(url: 'http://app.com/flipper')
|
139
|
+
expect {
|
140
|
+
adapter.remove(Flipper::Feature.new(:search, adapter))
|
141
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "#clear" do
|
146
|
+
it "raises error when not successful" do
|
147
|
+
stub_request(:delete, /app.com/)
|
148
|
+
.to_return(status: 503, body: "{}", headers: {})
|
149
|
+
|
150
|
+
adapter = described_class.new(url: 'http://app.com/flipper')
|
151
|
+
expect {
|
152
|
+
adapter.clear(Flipper::Feature.new(:search, adapter))
|
153
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "#enable" do
|
158
|
+
it "raises error when not successful" do
|
159
|
+
stub_request(:post, /app.com/)
|
160
|
+
.to_return(status: 503, body: "{}", headers: {})
|
161
|
+
|
162
|
+
adapter = described_class.new(url: 'http://app.com/flipper')
|
163
|
+
feature = Flipper::Feature.new(:search, adapter)
|
164
|
+
gate = feature.gate(:boolean)
|
165
|
+
thing = gate.wrap(true)
|
166
|
+
expect {
|
167
|
+
adapter.enable(feature, gate, thing)
|
168
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
describe "#disable" do
|
173
|
+
it "raises error when not successful" do
|
174
|
+
stub_request(:delete, /app.com/)
|
175
|
+
.to_return(status: 503, body: "{}", headers: {})
|
176
|
+
|
177
|
+
adapter = described_class.new(url: 'http://app.com/flipper')
|
178
|
+
feature = Flipper::Feature.new(:search, adapter)
|
179
|
+
gate = feature.gate(:boolean)
|
180
|
+
thing = gate.wrap(false)
|
181
|
+
expect {
|
182
|
+
adapter.disable(feature, gate, thing)
|
183
|
+
}.to raise_error(Flipper::Adapters::Http::Error)
|
118
184
|
end
|
119
185
|
end
|
120
186
|
|