Class: PinchHitter::Service::EndpointHandlers

Inherits:
Object
  • Object
show all
Defined in:
lib/pinch_hitter/service/endpoint_handlers.rb

Instance Method Summary collapse

Instance Method Details

#handler_for(endpoint = '/') ⇒ Object



25
26
27
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 25

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

#handler_for?(endpoint) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 29

def handler_for?(endpoint)
  handlers.has_key?(endpoint)
end

#handlersObject



7
8
9
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 7

def handlers
  @handlers ||= {}
end

#register_module(endpoint, mod) ⇒ Object



33
34
35
36
37
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 33

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

#resetObject



43
44
45
46
47
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 43

def reset
  handlers.values.each do |handler|
    handler.reset if handler.respond_to? :reset
  end
end

#respond_to(endpoint = '/', body = nil, request = nil, response = nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 16

def respond_to(endpoint='/', body=nil, request=nil, response = nil)
  handler = handler_for(endpoint)
  if handler.respond_to?(:handle_request)
    handler.handle_request(request, response)
  else
    handler.respond_to(body).squish
  end
end

#store_handler(endpoint, handler = MessageQueue.new) ⇒ Object



39
40
41
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 39

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

#store_message(endpoint, message) ⇒ Object



11
12
13
14
# File 'lib/pinch_hitter/service/endpoint_handlers.rb', line 11

def store_message(endpoint, message)
  handler = handler_for(endpoint)
  handler.store(message.squish) if handler.respond_to? :store
end