1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
# frozen_string_literal: true
module Seahorse
module Client
# 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}.
module HandlerBuilder
def handle_request(*args, &block)
handler(*args) do |context|
block.call(context)
@handler.call(context)
end
end
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
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
alias handler handle
# @api private
def handler_for(name = nil, &block)
if name
const_set(name, new_handler(block))
else
new_handler(block)
end
end
# @api private
def new_handler(block)
Class.new(Handler) do
define_method(:call, &block)
end
end
end
end
end
|