class Droonga::Client

Constants

DEFAULT_DATASET
DEFAULT_HOST
DEFAULT_PORT
DEFAULT_PROTOCOL
DEFAULT_TAG
DEFAULT_TARGET_ROLE
DEFAULT_TIMEOUT_SECONDS
VERSION

Attributes

on_error[W]
protocol[R]

Public Class Methods

new(options={}) click to toggle source

Creates a new Droonga Engine client.

@param options [Hash] Options to connect Droonga Engine. @option options [String] :tag (“droonga”) The tag of the request message. @option options [String] :host (“127.0.0.1”)

The host name or IP address of the Droonga Engine to be connected.

@option options [Integer] :port (24224)

The port number of the Droonga Engine to be connected.

@option options [String] :receiver_host (Socket.gethostname)

The host name or IP address to receive response from the Droonga Engine.

@option options [Integer] :receiver_port (0)

The port number to receive response from the Droonga Engine.

@option options [Integer] :timeout (5)

The timeout value for connecting to, writing to and reading
from Droonga Engine.

@option options [Boolean] :completion (true)

Do or do not complete required fields of input messages.

@option options [Boolean] :validation (true)

Do or do not validate input messages.
# File lib/droonga/client.rb, line 86
def initialize(options={})
  @protocol = options[:protocol] || DEFAULT_PROTOCOL
  @connection = create_connection(options)
  @connection.on_error = lambda do |error|
    on_error(ConnectionError.new(error))
  end

  @completion = options[:completion] != false
  @validation = options[:validation] != false

  @completer = MessageCompleter.new(:default_dataset => options[:default_dataset],
                                    :default_timeout => options[:default_timeout],
                                    :default_target_role => options[:default_target_role])
  @validator = MessageValidator.new
end
open(options={}) { |client| ... } click to toggle source

Opens a new connection and yields a {Client} object to use the connection. The client is closed after the given block is finished.

@param (see initialize) @option (see initialize)

@yield [client] Gives the opened client. It is alive while yielding. @yieldparam client [Client] The opened client.

@return The return value from the given block.

# File lib/droonga/client.rb, line 57
def open(options={})
  client = new(options)
  begin
    yield(client)
  ensure
    client.close
  end
end

Public Instance Methods

close() click to toggle source

Close the connection used by the client. You can’t send any request anymore.

@return [void]

# File lib/droonga/client.rb, line 124
def close
  @connection.close
end
complete(message) click to toggle source
# File lib/droonga/client.rb, line 128
def complete(message)
  case @protocol
  when :http
    http_request = @connection.build_request(message)
    http_headers = {}
    http_request.canonical_each do |name, value|
      http_headers[name] = value
    end
    {
      "method"  => http_request.method,
      "path"    => http_request.path,
      "headers" => http_headers,
      "body"    => http_request.body,
    }
  when :droonga
    do_completion(message, :completion => true)
  else
    nil
  end
end
request(message, options={}, &block) click to toggle source
# File lib/droonga/client.rb, line 108
def request(message, options={}, &block)
  message = do_completion(message, options)
  do_validation(message, options)
  @connection.request(message, options, &block)
end
send(message, options={}, &block) click to toggle source
# File lib/droonga/client.rb, line 102
def send(message, options={}, &block)
  message = do_completion(message, options)
  do_validation(message, options)
  @connection.send(message, options, &block)
end
subscribe(message, options={}, &block) click to toggle source
# File lib/droonga/client.rb, line 114
def subscribe(message, options={}, &block)
  message = do_completion(message, options)
  do_validation(message, options)
  @connection.subscribe(message, options, &block)
end

Private Instance Methods

create_connection(options) click to toggle source
# File lib/droonga/client.rb, line 150
def create_connection(options)
  case @protocol
  when :http
    Connection::HTTP.new(options)
  when :droonga
    Connection::DroongaProtocol.new(options)
  end
end
do_completion(message, options={}) click to toggle source
# File lib/droonga/client.rb, line 159
def do_completion(message, options={})
  if options[:completion].nil?
    return message unless @completion
  else
    return message if options[:completion] == false
  end
  @completer.complete(message)
end
do_validation(message, options={}) click to toggle source
# File lib/droonga/client.rb, line 168
def do_validation(message, options={})
  if options[:validation].nil?
    return unless @validation
  else
    return if options[:validation] == false
  end
  @validator.validate(message)
end
on_error(error) click to toggle source
# File lib/droonga/client.rb, line 177
def on_error(error)
  @on_error.call(error) if @on_error
end