Sha256: 96551dc106035e8a4ddde054ada882bfeaa947160b6f8a7991fd11e3dd9c9661

Contents?: true

Size: 1.99 KB

Versions: 28

Compression:

Stored size: 1.99 KB

Contents

require "pact_broker/api/resources/base_resource"
require "pact_broker/api/decorators/webhook_decorator"
require "pact_broker/api/decorators/webhooks_decorator"
require "pact_broker/api/contracts/webhook_contract"
require "pact_broker/api/resources/webhook_resource_methods"

module PactBroker
  module Api
    module Resources
      class PacticipantWebhooks < BaseResource
        include WebhookResourceMethods

        def allowed_methods
          ["POST", "GET", "OPTIONS"]
        end

        def content_types_provided
          [["application/hal+json", :to_json]]
        end

        def content_types_accepted
          [["application/json", :from_json]]
        end

        def resource_exists?
          (!consumer_specified? || consumer) && (!provider_specified? || provider)
        end

        def malformed_request?
          if request.post?
            return invalid_json? || webhook_validation_errors?(webhook)
          end
          false
        end

        def create_path
          webhook_url next_uuid, base_url
        end

        def post_is_create?
          true
        end

        def from_json
          saved_webhook = webhook_service.create next_uuid, webhook, consumer, provider
          response.body = decorator_class(:webhook_decorator).new(saved_webhook).to_json(decorator_options)
        end

        def to_json
          decorator_class(:webhooks_decorator).new(webhooks).to_json(decorator_options(resource_title: "Webhooks"))
        end

        def policy_name
          :'webhooks::webhooks'
        end

        def policy_record
          request.post? ? webhook : nil
        end

        private

        def webhooks
          webhook_service.find_by_consumer_and_provider(consumer, provider)
        end

        def webhook
          @webhook ||= decorator_class(:webhook_decorator).new(PactBroker::Domain::Webhook.new).from_json(request_body)
        end

        def next_uuid
          @next_uuid ||= webhook_service.next_uuid
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
pact_broker-2.100.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.99.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.98.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.97.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.96.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.95.1 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.95.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.94.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.93.4 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.93.3 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.93.2 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.93.1 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.93.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.92.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.91.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.90.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.89.1 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.89.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.88.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb
pact_broker-2.87.0 lib/pact_broker/api/resources/pacticipant_webhooks.rb