Sha256: d508d61b83196f17b3a9dd78b8b95cb707a05ea3d1bfdd71500f27a9fe7ec065

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

require 'singleton'

module Apress
  module Api
    module Callbacks
      class Config
        include Singleton

        class << self
          delegate :add_service, :services, to: :instance
          delegate :allowed_client?, :add_client, to: :instance
          delegate :add_handler, :handlers, to: :instance
          delegate :valid_event?, to: :instance
        end

        def add_service(event:, service:)
          events[event] << service
        end

        def add_handler(service:, event:, handler:)
          (handlers_config[service][event] ||= []) << handler
        end

        def add_client(access_id)
          clients << access_id
        end

        def allowed_client?(client)
          clients.include?(client.access_id)
        end

        def services(event)
          events[event]
        end

        def handlers(service:, event:)
          handlers_config.fetch(service).fetch(event)
        end

        private

        def handlers_config
          @handlers_config ||= ::Hash.new { |hash, key| hash[key] = {} }
        end

        def events
          @events ||= ::Hash.new { |hash, key| hash[key] = [] }
        end

        def clients
          @clients ||= Set.new
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apress-api-1.24.0 lib/apress/api/callbacks/config.rb
apress-api-1.23.0 lib/apress/api/callbacks/config.rb
apress-api-1.22.1 lib/apress/api/callbacks/config.rb
apress-api-1.22.0 lib/apress/api/callbacks/config.rb