Class: ResourceSet::ActionInvoker
- Inherits:
-
Object
- Object
- ResourceSet::ActionInvoker
- Defined in:
- lib/resource_set/action_invoker.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
Instance Method Summary collapse
- #construct_body ⇒ Object
- #handle_response ⇒ Object
-
#initialize(action, resource, *args) ⇒ ActionInvoker
constructor
A new instance of ActionInvoker.
- #resolver ⇒ Object
- #response ⇒ Object
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..params_encoder = Faraday::FlatParamsEncoder @args = args @options = args.last.kind_of?(Hash) ? args.last : {} @response = nil end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/resource_set/action_invoker.rb', line 3 def action @action end |
#args ⇒ Object (readonly)
Returns the value of attribute args.
3 4 5 |
# File 'lib/resource_set/action_invoker.rb', line 3 def args @args end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/resource_set/action_invoker.rb', line 3 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/resource_set/action_invoker.rb', line 3 def @options end |
#resource ⇒ Object (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_body ⇒ Object
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_response ⇒ Object
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 |
#resolver ⇒ Object
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 |
#response ⇒ Object
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()) do |request| request.body = construct_body if action.body and [:post, :put, :patch, :delete].include?(action.verb) append_hooks(:before, request) end end |