Sha256: 83fbbdadbc787f5483bd8306772609081a4015c3ba2c4c3394bfc1ef7ab8f56a

Contents?: true

Size: 907 Bytes

Versions: 3

Compression:

Stored size: 907 Bytes

Contents

# frozen_string_literal: true

require_dependency 'wcc/contentful/application_controller'

module WCC::Contentful
  class WebhookController < ApplicationController
    before_action :authorize_contentful

    def receive
      WCC::Contentful.sync!(up_to_id: params.dig('sys', 'id'))
    end

    def authorize_contentful
      config = WCC::Contentful.configuration

      if config.webhook_username.present? && config.webhook_password.present?
        unless authenticate_with_http_basic do |u, p|
                 u == config.webhook_username &&
                     p == config.webhook_password
               end
          request_http_basic_authentication
          return
        end
      end

      return if request.content_type == 'application/vnd.contentful.management.v1+json'
      render json: { msg: 'This endpoint only responds to webhooks from Contentful' }, status: 406
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wcc-contentful-0.2.2 app/controllers/wcc/contentful/webhook_controller.rb
wcc-contentful-0.2.1 app/controllers/wcc/contentful/webhook_controller.rb
wcc-contentful-0.2.0 app/controllers/wcc/contentful/webhook_controller.rb