Class: Consul::Async::ConsulTemplateKV

Inherits:
ConsulTemplateAbstractArray show all
Defined in:
lib/consul/async/consul_template.rb

Overview

Key/Values representations This is an array as it might contain several values Several helpers exist to handle nicely transformations

Instance Attribute Summary collapse

Attributes inherited from ConsulTemplateAbstract

#endpoint, #result, #seen_at

Instance Method Summary collapse

Methods inherited from ConsulTemplateAbstract

#_seen_at, #method_missing, #ready?, #respond_to_missing?

Constructor Details

#initialize(consul_endpoint, root) ⇒ ConsulTemplateKV

Returns a new instance of ConsulTemplateKV.



711
712
713
714
# File 'lib/consul/async/consul_template.rb', line 711

def initialize(consul_endpoint, root)
  @root = root
  super(consul_endpoint)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Consul::Async::ConsulTemplateAbstract

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



710
711
712
# File 'lib/consul/async/consul_template.rb', line 710

def root
  @root
end

Instance Method Details

#find(name = root) ⇒ Object



716
717
718
719
# File 'lib/consul/async/consul_template.rb', line 716

def find(name = root)
  res = result_delegate.find { |k| name == k['Key'] }
  res || {}
end

#get_value(name = root) ⇒ Object

Get the raw value (might be base64 encoded)



722
723
724
# File 'lib/consul/async/consul_template.rb', line 722

def get_value(name = root)
  find(name)['Value']
end

#get_value_decoded(name = root) ⇒ Object

Get the Base64 Decoded value



727
728
729
730
731
732
# File 'lib/consul/async/consul_template.rb', line 727

def get_value_decoded(name = root)
  val = get_value(name)
  return nil unless val

  Base64.decode64(val)
end

#get_value_json(name = root, catch_errors: true) ⇒ Object

Helper to get the value decoded as JSON



735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/consul/async/consul_template.rb', line 735

def get_value_json(name = root, catch_errors: true)
  x = get_value_decoded(name)
  return nil unless x

  begin
    JSON.parse(x)
  rescue JSON::ParserError => e
    return nil if catch_errors

    raise StandardError.new(e), "get_value_json() cannot deserialize kv(#{name}) as JSON: #{e.message}", e.backtrace
  end
end

#get_value_yaml(name = root, catch_errors: true) ⇒ Object

Helper to get the value decoded as YAML



749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/consul/async/consul_template.rb', line 749

def get_value_yaml(name = root, catch_errors: true)
  x = get_value_decoded(name)
  return nil unless x

  begin
    YAML.safe_load(x)
  rescue YAML::ParserError => e
    return nil if catch_errors

    raise StandardError.new(e), "get_value_yaml() cannot deserialize kv(#{name}) as YAML: #{e.message}", e.backtrace
  end
end