Sha256: 8d173fe153460e7fadccea9b0100df804f5e7dc58b8f882eb72181ffeb55643f

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

require_relative 'base_decorator'
require 'json'
require 'pact_broker/messages'

module PactBroker
  module Api
    module Decorators
      class WebhookExecutionResultDecorator < BaseDecorator
        class ErrorDecorator < BaseDecorator
          property :message
          property :backtrace
        end

        class HTTPResponseDecorator < BaseDecorator
          property :status, :getter => lambda { |_| code.to_i }
          property :headers, exec_context: :decorator
          property :body, exec_context: :decorator

          def headers
            headers_hash = represented.to_hash
            headers_hash.keys.each_with_object({}) do | name, new_headers_hash|
              new_headers_hash[name] = headers_hash[name].join(", ")
            end
          end

          def body
            begin
              ::JSON.parse(represented.body)
            rescue StandardError => e
              represented.body
            end
          end
        end

        property :error, :extend => ErrorDecorator, if: lambda { |context| context[:options][:user_options][:show_response] }
        property :response, :extend => HTTPResponseDecorator, if: lambda { |context| context[:options][:user_options][:show_response] }
        property :response_hidden_message, as: :message, exec_context: :decorator, if: lambda { |context| !context[:options][:user_options][:show_response] }

        link :webhook do | options |
          {
            href: webhook_url(options.fetch(:webhook).uuid, options.fetch(:base_url))
          }
        end

        link :'try-again' do | options |
          {
            title: 'Execute the webhook again',
            href: webhook_execution_url(options.fetch(:webhook), options.fetch(:base_url))
          }
        end

        def response_hidden_message
          PactBroker::Messages.message('messages.response_body_hidden')
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pact_broker-2.22.0 lib/pact_broker/api/decorators/webhook_execution_result_decorator.rb
pact_broker-2.21.0 lib/pact_broker/api/decorators/webhook_execution_result_decorator.rb
pact_broker-2.20.0 lib/pact_broker/api/decorators/webhook_execution_result_decorator.rb