Sha256: 3ddaed47aaa483118ce57e603156c00349da03c87e2aae355b7a5bfbe55f3c34

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

module Hermes
  class EventProcessor
    extend Forwardable

    def self.call(event_class, body, headers)
      new(
        distributed_trace_repository: Hermes::DependenciesContainer["distributed_trace_repository"],
        config: Hermes::DependenciesContainer["config"]
      ).call(event_class, body, headers)
    end

    attr_reader :distributed_trace_repository, :config
    private     :distributed_trace_repository, :config

    def initialize(distributed_trace_repository:, config:)
      @distributed_trace_repository = distributed_trace_repository
      @config = config
    end

    def call(event_class, body, headers)
      event = Object.const_get(event_class).from_body_and_headers(body, headers)

      Hermes.with_origin_headers(headers) do
        instrumenter.instrument("Hermes.EventProcessor.#{event_class}") do
          response = infer_handler(event_class).call(event)
          distributed_trace_repository.create(event)
          ProcessingResult.new(event, response)
        end
      end
    end

    private

    def_delegators :config, :instrumenter, :event_handler

    def infer_handler(event_class)
      event_handler.registration_for(event_class).handler
    end

    ProcessingResult = Struct.new(:event, :response)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hermes-rb-0.10.1 lib/hermes/event_processor.rb
hermes-rb-0.10.0 lib/hermes/event_processor.rb
hermes-rb-0.9.1 lib/hermes/event_processor.rb
hermes-rb-0.9.0 lib/hermes/event_processor.rb
hermes-rb-0.8.0 lib/hermes/event_processor.rb
hermes-rb-0.7.5 lib/hermes/event_processor.rb
hermes-rb-0.7.4 lib/hermes/event_processor.rb
hermes-rb-0.7.3 lib/hermes/event_processor.rb
hermes-rb-0.7.2 lib/hermes/event_processor.rb