Sha256: e55e436edd1a4305facfe28fc0f9b3ac73eb89d6c83c5052bad569cfc1fb78fe

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module APIClientBase
  module Client
    module ClassMethods

      def api_action(action_name, opts={})
        define_method action_name do |args|
          namespace = self.class.name.deconstantize.constantize

          request_class_name = [action_name.to_s.classify, "Request"].join
          request_class = namespace.const_get(request_class_name)

          response_class_name = [action_name.to_s.classify, "Response"].join
          response_class = namespace.const_get(response_class_name)

          if opts[:args].is_a?(Array)
            request_args = opts[:args].each_with_object({}).
              with_index { |(arg, hash), i| hash[arg] = args[i] }
          else
            request_args = args
          end

          default_request_opts_method =
            self.class.api_client_base_client_options[:default_opts]
          default_request_opts = send(default_request_opts_method)

          request = request_class.new(default_request_opts.merge(request_args))
          raw_response = request.()
          response_class.new(raw_response: raw_response)
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api_client_base-0.1.0 lib/api_client_base/client/class_methods.rb