Skip to content

Commit

Permalink
refactor(webhooks): move template substitution into Render class
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 25, 2019
1 parent 7fa518c commit 66c1dc4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/contracts/webhook_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def host_whitelist
end

def parse_uri(uri_string, placeholder = 'placeholder')
URI(uri_string.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, placeholder))
URI(PactBroker::Webhooks::Render.render_with_placeholder(uri_string, placeholder))
end
end

Expand Down
7 changes: 6 additions & 1 deletion lib/pact_broker/webhooks/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
module PactBroker
module Webhooks
class Render

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

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

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

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

private

def self.render_template(params, template)
params.inject(template) do | template, (key, value) |
template.gsub("${#{key}}", value)
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/webhook_request_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def build(template_params)
end

def description
"#{http_method.upcase} #{URI(url.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, 'placeholder')).host}"
"#{http_method.upcase} #{URI(PactBroker::Webhooks::Render.render_with_placeholder(url)).host}"
end

def display_password
Expand Down

0 comments on commit 66c1dc4

Please sign in to comment.