Sha256: f6bc95e3a4d532c419a45abc7afea411354c13fe0f15f115b2564f40b799cff1

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module ResourceKit
  class ActionInvoker
    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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resource_kit-0.0.3 lib/resource_kit/action_invoker.rb