Sha256: 224eef2058f79a911af907ca657403743faad8d23f7589983dc33bc7cfa6746e

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Cloudenvoy
  # Handle execution of Cloudenvoy subscribers
  class SubscriberController < ApplicationController
    # Authenticate all requests.
    before_action :authenticate!

    # Return 401 when API Token is invalid
    rescue_from AuthenticationError do
      head :unauthorized
    end

    # POST /cloudenvoy/receive
    #
    # Process a Pub/Sub message using a Cloudenvoy subscriber.
    #
    def receive
      # Process msg_descriptor
      Subscriber.execute_from_descriptor(msg_descriptor)
      head :no_content
    rescue InvalidSubscriberError
      # 404: Message delivery will be retried
      head :not_found
    rescue StandardError => e
      # 422: Message delivery will be retried
      Cloudenvoy.logger.error(e)
      Cloudenvoy.logger.error(e.backtrace.join("\n"))
      head :unprocessable_entity
    end

    private

    #
    # Parse the request body and return the actual message
    # descriptor.
    #
    # @return [Hash] The descriptor payload
    #
    def msg_descriptor
      @msg_descriptor ||= begin
        # Get raw body
        content = request.body.read

        # Return content parsed as JSON and add job retries count
        JSON.parse(content).except('token')
      end
    end

    #
    # Authenticate incoming requests via a token parameter
    #
    # See Cloudenvoy::Authenticator#verification_token
    #
    def authenticate!
      Authenticator.verify!(params['token'])
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cloudenvoy-0.4.0 app/controllers/cloudenvoy/subscriber_controller.rb
cloudenvoy-0.3.1 app/controllers/cloudenvoy/subscriber_controller.rb
cloudenvoy-0.3.0 app/controllers/cloudenvoy/subscriber_controller.rb
cloudenvoy-0.2.0 app/controllers/cloudenvoy/subscriber_controller.rb
cloudenvoy-0.1.0 app/controllers/cloudenvoy/subscriber_controller.rb