Sha256: 5e1f4dedd4fbebbaab2964aa076d7f646432f74fddbdeb57397a8240811b5fc0

Contents?: true

Size: 1.27 KB

Versions: 18

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module FlowcommerceSpree
  class WebhooksController < ActionController::Base
    wrap_parameters false
    respond_to :json
    http_basic_authenticate_with name: FLOW_IO_WEBHOOK_USER, password: FLOW_IO_WEBHOOK_PASSWORD

    # forward incoming requests to respective Flow Webhooks Service objects
    # /flow/event-target endpoint
    def handle_flow_io_event
      %i[event_id organization discriminator].each_with_object(params) { |key, obj| obj.require(key) }
      return unless organization_valid?

      webhook_result = "FlowcommerceSpree::Webhooks::#{params['discriminator'].classify}".constantize.process(params)
      @result = {}
      @result[:error] = webhook_result.full_messages.join("\n") if webhook_result.errors.any?
    rescue StandardError => e
      @result = { error: e.class.to_s, message: e.message, backtrace: e.backtrace }
    ensure
      logger.info(@result) if (error = @result[:error])
      render json: @result.except(:backtrace), status: error ? :unprocessable_entity : :ok
    end

    private

    def organization_valid?
      org = params[:organization]
      return true if org == FlowcommerceSpree::ORGANIZATION

      @result = { error: 'InvalidParam', message: "Organization '#{org}' is invalid!" }
      false
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
flowcommerce_spree-0.0.22 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.21 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.20 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.19 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.18 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.17 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.16 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.15 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.14 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.13 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.12 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.11 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.10 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.9 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.8 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.7 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.6 app/controllers/flowcommerce_spree/webhooks_controller.rb
flowcommerce_spree-0.0.5 app/controllers/flowcommerce_spree/webhooks_controller.rb