Sha256: a6bafe4d58deb4d42b5a26cf0ab4a1b0de83bd4fd5304f41f4852d55d0cae019

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth

  # Responsible for coordinating incoming messages
  #  1. Receives incoming request params
  #  2. Initializes respective service request handler
  #  3. Processes params through service request handler (might be async)
  #  4. Inits base StealthController with state params returned from the service
  #     request handler
  #  5. Returns an HTTP response to be returned to the requestor
  class Dispatcher

    attr_reader :service, :params, :headers, :message_handler

    def initialize(service:, params:, headers:)
      @service = service
      @params = params
      @headers = headers
      @message_handler = message_handler_klass.new(
        params: params,
        headers: headers
      )
    end

    def coordinate
      message_handler.coordinate
    end

    def process
      service_message = message_handler.process
      bot_controller = BotController.new(service_message: service_message)
      bot_controller.route
    end

    private

      def message_handler_klass
        begin
          Kernel.const_get("Stealth::Services::#{service.capitalize}::MessageHandler")
        rescue NameError
          raise(Stealth::Errors::ServiceNotRecognized, "The service '#{service}' was not recognized")
        end
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stealth-0.9.7 lib/stealth/dispatcher.rb
stealth-0.9.6 lib/stealth/dispatcher.rb