Sha256: 36391c286ce60978c0e1b81a71f99cc83dca5db151f8d59c9949c0aab47ff65e

Contents?: true

Size: 981 Bytes

Versions: 44

Compression:

Stored size: 981 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


      def self.render_template(params, template)
        params.inject(template) do | agg, (key, value) |
          agg.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

44 entries across 44 versions & 1 rubygems

Version Path
pact_broker-2.112.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.111.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.109.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.109.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.108.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.107.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.107.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.107.0.beta.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.106.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.105.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.104.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.103.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.102.2 lib/pact_broker/webhooks/render.rb
pact_broker-2.102.1 lib/pact_broker/webhooks/render.rb
pact_broker-2.102.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.101.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.100.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.99.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.98.0 lib/pact_broker/webhooks/render.rb
pact_broker-2.97.0 lib/pact_broker/webhooks/render.rb