class Flipper::Adapters::Consul

Constants

FeaturesKey

Private: The key that stores the set of known features.

VERSION

Attributes

name[R]

Public: The name of the adapter.

namespace[R]

Public Class Methods

new(client, namespace=nil) click to toggle source
# 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

add(feature) click to toggle source

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
clear(feature) click to toggle source

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
disable(feature, gate, thing) click to toggle source

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
enable(feature, gate, thing) click to toggle source

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
features() click to toggle source

Public: The set of known features.

# File lib/flipper/adapters/consul.rb, line 33
def features
  read_multiple build_path "#{FeaturesKey}/features"
end
get(feature) click to toggle source

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
remove(feature) click to toggle source

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

build_path(key) click to toggle source
# File lib/flipper/adapters/consul.rb, line 130
def build_path(key)
  if namespace.nil?
    key
  else
    "#{namespace}/#{key}"
  end
end
gate_values_as_set(values, gate) click to toggle source
# 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
get_feature_values(feature) click to toggle source
# 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
key(feature, gate) click to toggle source

Private

# File lib/flipper/adapters/consul.rb, line 122
def key(feature, gate)
  build_path "#{feature.key}/#{gate.key}"
end
read_multiple(key_path) click to toggle source
# 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
set_member_key(feature, gate, thing) click to toggle source
# 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