Sha256: 30275d7f331f967c0eefbe9522c17dceacd58d82af40dd2cd48e474cca32b769

Contents?: true

Size: 1005 Bytes

Versions: 63

Compression:

Stored size: 1005 Bytes

Contents

require 'pact_broker/webhooks/pact_and_verification_parameters'

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)
        end
      end

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

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
pact_broker-2.79.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.79.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.78.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.78.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.77.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.76.2 lib/pact_broker/webhooks/render.rb
pact_broker-2.76.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.76.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.75.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.74.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.74.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.73.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.72.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.71.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.70.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.69.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.68.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.68.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.67.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.66.0 lib/pact_broker/webhooks/render.rb