class Seahorse::Client::Base

Attributes

config[R]

@return [Configuration<Struct>]

handlers[R]

@return [HandlerList]

Public Class Methods

new(plugins, options) click to toggle source

@api private

# File lib/seahorse/client/base.rb, line 18
def initialize(plugins, options)
  @config = build_config(plugins, options)
  @handlers = build_handler_list(plugins)
  after_initialize(plugins)
end

Private Class Methods

add_plugin(plugin) click to toggle source

Registers a plugin with this client.

@example Register a plugin

ClientClass.add_plugin(PluginClass)

@example Register a plugin by name

ClientClass.add_plugin('gem-name.PluginClass')

@example Register a plugin with an object

plugin = MyPluginClass.new(options)
ClientClass.add_plugin(plugin)

@param [Class, Symbol, String, Object] plugin @see .clear_plugins @see .set_plugins @see .remove_plugin @see .plugins @return [void]

# File lib/seahorse/client/base.rb, line 130
def add_plugin(plugin)
  @plugins.add(plugin)
end
api() click to toggle source

@return [Model::Api]

# File lib/seahorse/client/base.rb, line 174
def api
  @api ||= Model::Api.new
end
before_initialize(plugins, options) click to toggle source
# File lib/seahorse/client/base.rb, line 216
def before_initialize(plugins, options)
  plugins.each do |plugin|
    plugin.before_initialize(self, options) if plugin.respond_to?(:before_initialize)
  end
end
build_plugins() click to toggle source
# File lib/seahorse/client/base.rb, line 212
def build_plugins
  plugins.map { |plugin| plugin.is_a?(Class) ? plugin.new : plugin }
end
clear_plugins() click to toggle source

@see .set_plugins @see .add_plugin @see .remove_plugin @see .plugins @return [void]

# File lib/seahorse/client/base.rb, line 148
def clear_plugins
  @plugins.set([])
end
define(options = {}) click to toggle source

@option options [Model::Api, Hash] :api ({}) @option options [Array<Plugin>] :plugins ([]) A list of plugins to

add to the client class created.

@return [Class<Client::Base>]

# File lib/seahorse/client/base.rb, line 190
def define(options = {})
  subclass = Class.new(self)
  subclass.set_api(options[:api] || api)
  Array(options[:plugins]).each do |plugin|
    subclass.add_plugin(plugin)
  end
  subclass
end
Also aliased as: extend
define_operation_methods() click to toggle source
# File lib/seahorse/client/base.rb, line 202
def define_operation_methods
  @api.operation_names.each do |method_name|
    define_method(method_name) do |*args, &block|
      params = args[0] || {}
      options = args[1] || {}
      build_request(method_name, params).send_request(options, &block)
    end
  end
end
extend(options = {})
Alias for: define
inherited(subclass) click to toggle source
# File lib/seahorse/client/base.rb, line 222
def inherited(subclass)
  subclass.instance_variable_set('@plugins', PluginList.new(@plugins))
end
new(options = {}) click to toggle source
# File lib/seahorse/client/base.rb, line 100
def new(options = {})
  plugins = build_plugins
  options = options.dup
  before_initialize(plugins, options)
  client = allocate
  client.send(:initialize, plugins, options)
  client
end
plugins() click to toggle source

Returns the list of registered plugins for this Client. Plugins are inherited from the client super class when the client is defined. @see .clear_plugins @see .set_plugins @see .add_plugin @see .remove_plugin @return [Array<Plugin>]

# File lib/seahorse/client/base.rb, line 169
def plugins
  Array(@plugins).freeze
end
remove_plugin(plugin) click to toggle source

@see .clear_plugins @see .set_plugins @see .add_plugin @see .plugins @return [void]

# File lib/seahorse/client/base.rb, line 139
def remove_plugin(plugin)
  @plugins.remove(plugin)
end
set_api(api) click to toggle source

@param [Model::Api] api @return [Model::Api]

# File lib/seahorse/client/base.rb, line 180
def set_api(api)
  @api = api
  define_operation_methods
  @api
end
set_plugins(plugins) click to toggle source

@param [Array<Plugin>] plugins @see .clear_plugins @see .add_plugin @see .remove_plugin @see .plugins @return [void]

# File lib/seahorse/client/base.rb, line 158
def set_plugins(plugins)
  @plugins.set(plugins)
end

Public Instance Methods

build_request(operation_name, params = {}) click to toggle source

Builds and returns a {Request} for the named operation. The request will not have been sent. @param [Symbol, String] operation_name @return [Request]

# File lib/seahorse/client/base.rb, line 34
def build_request(operation_name, params = {})
  Request.new(
    @handlers.for(operation_name),
    context_for(operation_name, params))
end
inspect() click to toggle source

@api private

# File lib/seahorse/client/base.rb, line 47
def inspect
  "#<#{self.class.name}>"
end
operation(name) click to toggle source

@param [String] name @return [Model::Operation]

# File lib/seahorse/client/base.rb, line 42
def operation(name)
  config.api.operation(name)
end
operation_names() click to toggle source

@return [Array<Symbol>] Returns a list of valid request operation

names. These are valid arguments to {#build_request} and are also
valid methods.
# File lib/seahorse/client/base.rb, line 54
def operation_names
  self.class.api.operation_names
end

Private Instance Methods

after_initialize(plugins) click to toggle source

Gives each plugin the opportunity to modify this client.

# File lib/seahorse/client/base.rb, line 82
def after_initialize(plugins)
  plugins.reverse.each do |plugin|
    plugin.after_initialize(self) if plugin.respond_to?(:after_initialize)
  end
end
build_config(plugins, options) click to toggle source

Constructs a {Configuration} object and gives each plugin the opportunity to register options with default values.

# File lib/seahorse/client/base.rb, line 62
def build_config(plugins, options)
  config = Configuration.new
  config.add_option(:api)
  plugins.each do |plugin|
    plugin.add_options(config) if plugin.respond_to?(:add_options)
  end
  config.build!(options.merge(api: self.class.api))
end
build_handler_list(plugins) click to toggle source

Gives each plugin the opportunity to register handlers for this client.

# File lib/seahorse/client/base.rb, line 72
def build_handler_list(plugins)
  plugins.inject(HandlerList.new) do |handlers, plugin|
    if plugin.respond_to?(:add_handlers)
      plugin.add_handlers(handlers, @config)
    end
    handlers
  end
end
context_for(operation_name, params) click to toggle source

@return [RequestContext]

# File lib/seahorse/client/base.rb, line 89
def context_for(operation_name, params)
  RequestContext.new(
    operation_name: operation_name,
    operation: operation(operation_name),
    client: self,
    params: params,
    config: config)
end