class Flipper::Adapters::Consul
Constants
- FeaturesKey
Private: The key that stores the set of known features.
- VERSION
Attributes
Public: The name of the adapter.
Public Class Methods
# File lib/flipper/adapters/consul.rb, line 18 def initialize(client, namespace=nil) @client = client @name = :consul if !namespace.nil? namespace = namespace.strip if namespace == '' namespace = nil elsif namespace.start_with? '/' namespace[0] = '' end end @namespace = namespace end
Public Instance Methods
Public: Adds a feature to the set of known features.
# File lib/flipper/adapters/consul.rb, line 38 def add(feature) @client.put build_path("#{FeaturesKey}/features/#{feature.key}"), '1' true end
Public: Clears the gate values for a feature.
# File lib/flipper/adapters/consul.rb, line 51 def clear(feature) @client.delete build_path(feature.key), recurse: true true end
Public: Disables a gate for a given thing.
feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.
Returns true.
# File lib/flipper/adapters/consul.rb, line 104 def disable(feature, gate, thing) case gate.data_type when :boolean @client.delete build_path(feature), recurse: true when :integer @client.put key(feature, gate), thing.value.to_s when :set @client.delete set_member_key(feature, gate, thing) else unsupported_data_type gate.data_type end true end
Public: Enables a gate for a given thing.
feature - The Flipper::Feature for the gate. gate - The Flipper::Gate to disable. thing - The Flipper::Type being disabled for the gate.
Returns true.
# File lib/flipper/adapters/consul.rb, line 84 def enable(feature, gate, thing) case gate.data_type when :boolean, :integer @client.put key(feature, gate), thing.value.to_s when :set @client.put set_member_key(feature, gate, thing), '1' else unsupported_data_type gate.data_type end true end
Public: The set of known features.
# File lib/flipper/adapters/consul.rb, line 33 def features read_multiple build_path "#{FeaturesKey}/features" end
Public: Gets the values for all gates for a given feature.
Returns a Hash of Flipper::Gate#key => value.
# File lib/flipper/adapters/consul.rb, line 59 def get(feature) result = {} values = get_feature_values(feature) feature.gates.each do |gate| result[gate.key] = case gate.data_type when :boolean, :integer values[gate.key.to_s] when :set gate_values_as_set(values, gate) else unsupported_data_type gate.data_type end end result end
Public: Removes a feature from the set of known features.
# File lib/flipper/adapters/consul.rb, line 44 def remove(feature) @client.delete build_path "#{FeaturesKey}/features/#{feature.key}" clear feature true end
Private Instance Methods
# File lib/flipper/adapters/consul.rb, line 130 def build_path(key) if namespace.nil? key else "#{namespace}/#{key}" end end
# File lib/flipper/adapters/consul.rb, line 167 def gate_values_as_set(values, gate) regex = /^#{Regexp.escape(gate.key.to_s)}\// keys_for_gate = values.keys.grep regex values = keys_for_gate.map { |key| key.split('/', 2).last } values.to_set end
# File lib/flipper/adapters/consul.rb, line 152 def get_feature_values(feature) begin key_path = build_path(feature.key) @client.get key_path, recurse: true values = @client.raw result = {} values.each do |item| result[item['Key'].sub!("#{key_path}/", '')] = item['Value'] end result rescue Diplomat::KeyNotFound {} end end
Private
# File lib/flipper/adapters/consul.rb, line 122 def key(feature, gate) build_path "#{feature.key}/#{gate.key}" end
# File lib/flipper/adapters/consul.rb, line 138 def read_multiple(key_path) begin @client.get key_path, recurse: true values = @client.raw values = values.map do |item| item['Key'].sub!("#{key_path}/", '') end value = values.to_set rescue Diplomat::KeyNotFound value = {}.to_set end value end
# File lib/flipper/adapters/consul.rb, line 126 def set_member_key(feature, gate, thing) build_path "#{feature.key}/#{gate.key}/#{thing.value.to_s}" end