class Seahorse::Client::Base
Attributes
@return [Configuration<Struct>]
@return [HandlerList]
Public Class Methods
Source
# File lib/seahorse/client/base.rb, line 127 def add_plugin(plugin) @plugins.add(plugin) end
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]
Source
# File lib/seahorse/client/base.rb, line 171 def api @api ||= Model::Api.new end
@return [Model::Api]
Source
# File lib/seahorse/client/base.rb, line 145 def clear_plugins @plugins.set([]) end
@see .set_plugins @see .add_plugin @see .remove_plugin @see .plugins @return [void]
Source
# File lib/seahorse/client/base.rb, line 185 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
@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>]
Source
# File lib/seahorse/client/base.rb, line 20 def initialize(plugins, options) @config = build_config(plugins, options) @handlers = build_handler_list(plugins) after_initialize(plugins) end
@api private
Source
# File lib/seahorse/client/base.rb, line 97 def new(options = {}) options = options.dup plugins = build_plugins(self.plugins + options.fetch(:plugins, [])) plugins = before_initialize(plugins, options) client = allocate client.send(:initialize, plugins, options) client end
Source
# File lib/seahorse/client/base.rb, line 166 def plugins Array(@plugins).freeze end
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>]
Source
# File lib/seahorse/client/base.rb, line 136 def remove_plugin(plugin) @plugins.remove(plugin) end
@see .clear_plugins @see .set_plugins @see .add_plugin @see .plugins @return [void]
Source
# File lib/seahorse/client/base.rb, line 177 def set_api(api) @api = api end
@param [Model::Api] api @return [Model::Api]
Source
# File lib/seahorse/client/base.rb, line 155 def set_plugins(plugins) @plugins.set(plugins) end
@param [Array<Plugin>] plugins @see .clear_plugins @see .add_plugin @see .remove_plugin @see .plugins @return [void]
Private Class Methods
Source
# File lib/seahorse/client/base.rb, line 201 def before_initialize(plugins, options) queue = Queue.new plugins.each { |plugin| queue.push(plugin) } until queue.empty? plugin = queue.pop next unless plugin.respond_to?(:before_initialize) plugins_before = options.fetch(:plugins, []) plugin.before_initialize(self, options) plugins_after = build_plugins(options.fetch(:plugins, []) - plugins_before) # Plugins with before_initialize can add other plugins plugins_after.each { |p| queue.push(p); plugins << p } end plugins end
Source
# File lib/seahorse/client/base.rb, line 197 def build_plugins(plugins) plugins.map { |plugin| plugin.is_a?(Class) ? plugin.new : plugin } end
Source
# File lib/seahorse/client/base.rb, line 217 def inherited(subclass) super subclass.instance_variable_set('@plugins', PluginList.new(@plugins)) end
Public Instance Methods
Source
# File lib/seahorse/client/base.rb, line 36 def build_request(operation_name, params = {}) Request.new( @handlers.for(operation_name), context_for(operation_name, params)) end
Builds and returns a {Request} for the named operation. The request will not have been sent. @param [Symbol, String] operation_name @return [Request]
Source
# File lib/seahorse/client/base.rb, line 43 def inspect "#<#{self.class.name}>" end
@api private
Source
# File lib/seahorse/client/base.rb, line 50 def operation_names self.class.api.operation_names - self.class.api.async_operation_names end
@return [Array<Symbol>] Returns a list of valid request operation
names. These are valid arguments to {#build_request} and are also valid methods.
Private Instance Methods
Source
# File lib/seahorse/client/base.rb, line 79 def after_initialize(plugins) plugins.reverse.each do |plugin| plugin.after_initialize(self) if plugin.respond_to?(:after_initialize) end end
Gives each plugin the opportunity to modify this client.
Source
# File lib/seahorse/client/base.rb, line 58 def build_config(plugins, options) config = Configuration.new config.add_option(:api) config.add_option(:plugins) plugins.each do |plugin| plugin.add_options(config) if plugin.respond_to?(:add_options) end config.build!(options.merge(api: self.class.api)) end
Constructs a {Configuration} object and gives each plugin the opportunity to register options with default values.
Source
# File lib/seahorse/client/base.rb, line 69 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
Gives each plugin the opportunity to register handlers for this client.
Source
# File lib/seahorse/client/base.rb, line 86 def context_for(operation_name, params) RequestContext.new( operation_name: operation_name, operation: config.api.operation(operation_name), client: self, params: params, config: config) end
@return [RequestContext]