class Seahorse::Client::Base

Attributes

config[R]

@return [Configuration<Struct>]

handlers[R]

@return [HandlerList]

Public 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 127
def add_plugin(plugin)
  @plugins.add(plugin)
end
api() click to toggle source

@return [Model::Api]

# File lib/seahorse/client/base.rb, line 171
def api
  @api ||= Model::Api.new
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 145
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 187
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
extend(options = {})
Alias for: define
new(plugins, options) click to toggle source

@api private

# 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
new(options = {}) click to toggle 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
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 166
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 136
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 177
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 155
def set_plugins(plugins)
  @plugins.set(plugins)
end

Private Class Methods

before_initialize(plugins, options) click to toggle source
# File lib/seahorse/client/base.rb, line 215
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
build_plugins(plugins) click to toggle source
# File lib/seahorse/client/base.rb, line 211
def build_plugins(plugins)
  plugins.map { |plugin| plugin.is_a?(Class) ? plugin.new : plugin }
end
define_operation_methods() click to toggle source
# File lib/seahorse/client/base.rb, line 199
def define_operation_methods
  operations_module = Module.new
  @api.operation_names.each do |method_name|
    operations_module.send(:define_method, method_name) do |*args, &block|
      params = args[0] || {}
      options = args[1] || {}
      build_request(method_name, params).send_request(options, &block)
    end
  end
  include(operations_module)
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/seahorse/client/base.rb, line 231
def inherited(subclass)
  super
  subclass.instance_variable_set('@plugins', PluginList.new(@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 36
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 43
def inspect
  "#<#{self.class.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 50
def operation_names
  self.class.api.operation_names - self.class.api.async_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 79
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 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
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 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
context_for(operation_name, params) click to toggle source

@return [RequestContext]

# 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