Class: RailsHttpLab::Execution::VariableResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_http_lab/execution/variable_resolver.rb

Overview

Replaces {var} with the value from a flat hash of variables. Unknown vars are left as-is so the user can see what’s missing.

Constant Summary collapse

VAR_RE =
/\{\{\s*([\w.-]+)\s*\}\}/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ VariableResolver

Returns a new instance of VariableResolver.



8
9
10
# File 'lib/rails_http_lab/execution/variable_resolver.rb', line 8

def initialize(vars = {})
  @vars = stringify_keys(vars)
end

Class Method Details

.from_environment_document(doc) ⇒ Object



17
18
19
20
21
# File 'lib/rails_http_lab/execution/variable_resolver.rb', line 17

def self.from_environment_document(doc)
  return new({}) unless doc
  pairs = doc.block("vars")&.pairs || []
  new(pairs.to_h)
end

Instance Method Details

#resolve(str) ⇒ Object



12
13
14
15
# File 'lib/rails_http_lab/execution/variable_resolver.rb', line 12

def resolve(str)
  return str unless str.is_a?(String)
  str.gsub(VAR_RE) { |m| @vars.fetch(Regexp.last_match(1), m) }
end