Keys example
This example shows how to use kv() results. It requires at least one key if the Consul's KV in order to display something.
<%
result = kv('', keys:true)
num_keys = result.count
# We refresh key every 10 seconds only
key_index = (Time.now.to_i / 10)
prng = Random.new
if num_keys > 0
key_to_display = result[key_index % num_keys]
end
%>
Using a single key
Found <%= num_keys %> keys in KV/Store
<%
if key_to_display
%>
Display a random single key using helpers
kv('<%= key_to_display %>').get_value :
<%= kv(key_to_display).get_value %>
kv('<%= key_to_display %>').get_value_decoded
<%= ERB::Util.html_escape(kv(key_to_display).get_value_decoded) %>
kv('<%= key_to_display %>').get_value_json(catch_errors: true)
<%
# catch_errors: true will return nil if data cannot be converted to JSON
json = kv(key_to_display).get_value_json(catch_errors: true)
%><%= (json ? jERB::Util.html_escape(json) : 'No Valid JSON Data') %>
Listing all keys (without their values)
-
<%
result.each do |key_name|
%>
- <%= key_name %> <% end %>
Listing 10 first keys (with their values)
-
<% kv_values = kv('', recurse: true)
index = 0
kv_values.each do |tuple|
index += 1
break if index > 10
key_name = tuple['Key']
%>
- <%= key_name %>:
<%= kv_values.get_value_decoded(key_name) %>
<%
end
%>