Sha256: 7092f49d5488c8f9c0acb8b2a0561efe58b46941a91cbeb1f61de107b13082ad

Contents?: true

Size: 1.01 KB

Versions: 7

Compression:

Stored size: 1.01 KB

Contents

require_relative 'message_queue'
require_relative 'endpoint_recorder'

module PinchHitter::Service
  class EndpointHandlers
    def handlers
      @handlers ||= {}
    end

    def store_message(endpoint, body)
      handler_for(endpoint).store body.squish
    end

    def respond_to(endpoint='/', request=nil)
      message = handler_for(endpoint).respond_to(request)
      message.squish if message
    end

    def requests(endpoint)
      handler_for(endpoint).requests
    end

    def handler_for(endpoint='/')
      handlers[normalize(endpoint)] || store_handler(endpoint)
    end

    def register_module(endpoint, mod)
      handler = Object.new
      handler.extend(mod)
      store_handler(endpoint, handler)
    end

    def store_handler(endpoint, handler=MessageQueue.new)
      handlers[normalize(endpoint)] = EndpointRecorder.new handler
    end

    def normalize(endpoint)
      return endpoint if endpoint =~ /^\//
      "/#{endpoint}"
    end

    def reset
      handlers.values.each(&:reset)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pinch_hitter-0.5.5 lib/pinch_hitter/service/endpoint_handlers.rb
pinch_hitter-0.5.4 lib/pinch_hitter/service/endpoint_handlers.rb
pinch_hitter-0.5.3 lib/pinch_hitter/service/endpoint_handlers.rb
pinch_hitter-0.5.2 lib/pinch_hitter/service/endpoint_handlers.rb
pinch_hitter-0.5.1 lib/pinch_hitter/service/endpoint_handlers.rb
pinch_hitter-0.5 lib/pinch_hitter/service/endpoint_handlers.rb
pinch_hitter-0.4 lib/pinch_hitter/service/endpoint_handlers.rb