Class: ResourceSet::ActionInvoker

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, resource, *args) ⇒ ActionInvoker

Returns a new instance of ActionInvoker.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/resource_set/action_invoker.rb', line 5

def initialize(action, resource, *args)
  @action = action
  @resource = resource
  @connection = resource.connection

  # Support for duplicate query parameter keys.
  @connection.options.params_encoder = Faraday::FlatParamsEncoder

  @args = args
  @options = args.last.kind_of?(Hash) ? args.last : {}
  @response = nil
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/resource_set/action_invoker.rb', line 3

def action
  @action
end

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/resource_set/action_invoker.rb', line 3

def args
  @args
end

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/resource_set/action_invoker.rb', line 3

def connection
  @connection
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/resource_set/action_invoker.rb', line 3

def options
  @options
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/resource_set/action_invoker.rb', line 3

def resource
  @resource
end

Class Method Details

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



18
19
20
# File 'lib/resource_set/action_invoker.rb', line 18

def self.call(action, resource, *args)
  new(action, resource, *args).handle_response
end

Instance Method Details

#construct_bodyObject



30
31
32
# File 'lib/resource_set/action_invoker.rb', line 30

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

#handle_responseObject



22
23
24
25
26
27
28
# File 'lib/resource_set/action_invoker.rb', line 22

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

#resolverObject



45
46
47
48
# File 'lib/resource_set/action_invoker.rb', line 45

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

#responseObject

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
# File 'lib/resource_set/action_invoker.rb', line 34

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