Class: ResourceKit::ActionInvoker

Inherits:
Object
  • Object
show all
Defined in:
lib/resource_kit/action_invoker.rb

Class Method Summary collapse

Class Method Details

.call(action, connection, *args) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/resource_kit/action_invoker.rb', line 3

def self.call(action, connection, *args)
  raise ArgumentError, "Verb '#{action.verb}' is not allowed" unless action.verb.in?(ALLOWED_VERBS)
  options = args.last.kind_of?(Hash) ? args.last : {}
  resolver = EndpointResolver.new(path: action.path)

  if action.body and action.verb.in?([:post, :put, :patch])
    # This request is going to have a response body. Handle it.
    response = connection.send(action.verb, resolver.resolve(options)) do |request|
      request.body = construct_body(*args, action)
    end
  else
    response = connection.send(action.verb, resolver.resolve(options))
  end

  handle_response(response, action)
end

.construct_body(*args, action) ⇒ Object



28
29
30
# File 'lib/resource_kit/action_invoker.rb', line 28

def self.construct_body(*args, action)
  action.body.call(*args[0..(action.body.arity - 1)])
end

.handle_response(response, action) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/resource_kit/action_invoker.rb', line 20

def self.handle_response(response, action)
  if handler = action.handlers[response.status]
    handler.call(response)
  else
    response.body
  end
end