Class: PactBroker::Webhooks::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/webhooks/render.rb

Constant Summary collapse

TEMPLATE_PARAMETER_REGEXP =
/\$\{pactbroker\.[^\}]+\}/
DEFAULT_ESCAPER =
lambda { |it| it }

Class Method Summary collapse

Class Method Details

.call(template, params, &escaper) ⇒ Object



17
18
19
# File 'lib/pact_broker/webhooks/render.rb', line 17

def self.call(template, params, &escaper)
  render_template(escape_params(params, escaper || DEFAULT_ESCAPER), template)
end

.escape_params(params, escaper) ⇒ Object



28
29
30
31
32
# File 'lib/pact_broker/webhooks/render.rb', line 28

def self.escape_params(params, escaper)
  params.keys.each_with_object({}) do | key, new_params |
    new_params[key] = escaper.call(params[key])
  end
end

.includes_parameter?(value) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/pact_broker/webhooks/render.rb', line 9

def self.includes_parameter?(value)
  value =~ TEMPLATE_PARAMETER_REGEXP
end

.render_template(params, template) ⇒ Object



22
23
24
25
26
# File 'lib/pact_broker/webhooks/render.rb', line 22

def self.render_template(params, template)
  params.inject(template) do | agg, (key, value) |
    agg.gsub("${#{key}}", value)
  end
end

.render_with_placeholder(value, placeholder = "placeholder") ⇒ Object



13
14
15
# File 'lib/pact_broker/webhooks/render.rb', line 13

def self.render_with_placeholder(value, placeholder = "placeholder")
  value.gsub(TEMPLATE_PARAMETER_REGEXP, placeholder)
end