Module: Seahorse::Client::HandlerBuilder

Included in:
Base, Plugin, Request
Defined in:
lib/seahorse/client/handler_builder.rb

Overview

This module provides the ability to add handlers to a class or module. The including class or extending module must respond to ‘#handlers`, returning a HandlerList.

Instance Method Summary collapse

Instance Method Details

#handle(*args, &block) ⇒ Object Also known as: handler



24
25
26
27
28
# File 'lib/seahorse/client/handler_builder.rb', line 24

def handle(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  handler_class = block ? handler_for(*args, &block) : args.first
  handlers.add(handler_class, options)
end

#handle_request(*args, &block) ⇒ Object



9
10
11
12
13
14
# File 'lib/seahorse/client/handler_builder.rb', line 9

def handle_request(*args, &block)
  handler(*args) do |context|
    block.call(context)
    @handler.call(context)
  end
end

#handle_response(*args, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/seahorse/client/handler_builder.rb', line 16

def handle_response(*args, &block)
  handler(*args) do |context|
    resp = @handler.call(context)
    block.call(resp) if resp.context.http_response.status_code > 0
    resp
  end
end

#handler_for(name = nil, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
36
37
38
# File 'lib/seahorse/client/handler_builder.rb', line 32

def handler_for(name = nil, &block)
  if name
    const_set(name, new_handler(block))
  else
    new_handler(block)
  end
end

#new_handler(block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
44
45
# File 'lib/seahorse/client/handler_builder.rb', line 41

def new_handler(block)
  Class.new(Handler) do
    define_method(:call, &block)
  end
end