Method: Sensu::Utilities#substitute_tokens

Defined in:
lib/sensu/utilities.rb

#substitute_tokens(tokens, attributes) ⇒ Array

Substitute dot notation tokens (eg. :::db.name|production:::) with the associated definition attribute value. Tokens can provide a fallback default value, following a pipe.

Parameters:

  • tokens (String)
  • attributes (Hash)

Returns:

  • (Array)

    containing the string with tokens substituted and an array of unmatched tokens.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sensu/utilities.rb', line 186

def substitute_tokens(tokens, attributes)
  unmatched_tokens = []
  encoded_tokens = tokens.encode("UTF-8", "binary", **{
    :invalid => :replace,
    :undef => :replace,
    :replace => ""
  })
  substituted = encoded_tokens.gsub(/:::([^:].*?):::/) do
    token, default = $1.to_s.split("|", 2)
    path = token.split(".").map(&:to_sym)
    matched = find_attribute_value(attributes, path, default)
    if matched.nil?
      unmatched_tokens << token
    end
    matched
  end
  [substituted, unmatched_tokens]
end