class ResourceKit::ActionInvoker

Attributes

action[R]
args[R]
connection[R]
options[R]
resource[R]

Public Class Methods

call(action, resource, *args) click to toggle source
# File lib/resource_kit/action_invoker.rb, line 13
def self.call(action, resource, *args)
  new(action, resource, *args).handle_response
end
new(action, resource, *args) click to toggle source
# File lib/resource_kit/action_invoker.rb, line 5
def initialize(action, resource, *args)
  @action = action
  @resource = resource
  @connection = resource.connection
  @args = args
  @options = args.last.kind_of?(Hash) ? args.last : {}
end

Public Instance Methods

construct_body() click to toggle source
# File lib/resource_kit/action_invoker.rb, line 25
def construct_body
  action.body.call(*args[0..(action.body.arity - 1)])
end
handle_response() click to toggle source
# File lib/resource_kit/action_invoker.rb, line 17
def handle_response
  if handler = action.handlers[response.status] || action.handlers[:any]
    resource.instance_exec(response, *args, &handler) # Since the handler is a block, it does not enforce parameter length checking
  else
    response.body
  end
end
resolver() click to toggle source
# File lib/resource_kit/action_invoker.rb, line 40
def resolver
  path = action.path.kind_of?(Proc) ? resource.instance_eval(&action.path) : action.path
  EndpointResolver.new(path: path, query_param_keys: action.query_keys)
end
response() click to toggle source
# File lib/resource_kit/action_invoker.rb, line 29
def response
  return @response if @response

  raise ArgumentError, "Verb '#{action.verb}' is not allowed" unless ALLOWED_VERBS.include?(action.verb)

  @response = connection.send(action.verb, resolver.resolve(options)) do |request|
    request.body = construct_body if action.body and [:post, :put, :patch, :delete].include?(action.verb)
    append_hooks(:before, request)
  end
end

Private Instance Methods

append_hooks(hook_type, request) click to toggle source
# File lib/resource_kit/action_invoker.rb, line 47
def append_hooks(hook_type, request)
  (action.hooks[hook_type] || []).each do |hook|
    case hook
    when Proc
      resource.instance_exec(*args, request, &hook)
    when Symbol
      resource.send(hook, *args, request)
    end
  end
end