Sha256: 319afe33d04b0a4049a98d8da071fec1033c46c59798cf4362990f81b4c294fe

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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
      # 422: Message delivery will be retried
      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

1 entries across 1 versions & 1 rubygems

Version Path
cloudenvoy-0.4.1 app/controllers/cloudenvoy/subscriber_controller.rb