class Droonga::Client::Connection::HTTP

Attributes

on_error[W]

Public Class Methods

new(options={}) click to toggle source
# File lib/droonga/client/connection/http.rb, line 52
def initialize(options={})
  @host    = options[:host] || "127.0.0.1"
  @port    = options[:port] || 80
  @timeout = options[:timeout] || 1
end

Public Instance Methods

build_request(message) click to toggle source
# File lib/droonga/client/connection/http.rb, line 162
def build_request(message)
  http_method = message["method"] || "GET"
  http_headers = message["headers"] || {}
  case http_method.to_s.upcase
  when "POST"
    request = Net::HTTP::Post.new(build_path(message, {}),
                                  http_headers)
    body = message["body"]
    if body.is_a?(Hash) or body.is_a?(Array)
      body = JSON.generate(body)
    end
    request.body = body
    request
  when "GET"
    parameters = message["body"] || {}
    Net::HTTP::Get.new(build_path(message, parameters),
                       http_headers)
  else
    raise InvalidHTTPMethodError.new(http_method, message)
  end
end
close() click to toggle source

Close the connection. This connection can’t be used anymore.

@return [void]

# File lib/droonga/client/connection/http.rb, line 159
def close
end
request(message, options={}) { |body| ... } click to toggle source

Sends a request message and receives one or more response messages.

@overload request(message, options={})

This is synchronously version.

@param message [Hash] Request message.
@param options [Hash] The options.
   TODO: WRITE ME

@return [Object] The response. TODO: WRITE ME

@overload request(message, options={}, &block)

This is asynchronously version.

@param message [Hash] Request message.
@param options [Hash] The options.
   TODO: WRITE ME
@yield [response]
   The block is called when response is received.
@yieldparam [Object] response
   The response.

@return [Request] The request object.
# File lib/droonga/client/connection/http.rb, line 82
def request(message, options={}, &block)
  sync = block.nil?
  if sync
    send(message, options) do |response|
      response.body
    end
  else
    thread = Thread.new do
      catch do |tag|
        send(message, options) do |response|
          begin
            yield(response.body)
          rescue LocalJumpError
            throw(tag)
          end
        end
      end
    end
    Request.new(thread)
  end
end
send(message, options={}) { |response| ... } click to toggle source

Sends low level request. Normally, you should use other convenience methods.

@param message [Hash] Request message. @param options [Hash] The options to send request.

TODO: WRITE ME

@return [void]

# File lib/droonga/client/connection/http.rb, line 140
def send(message, options={}, &block)
  http = Net::HTTP.new(@host, @port)
  open_timeout = @timeout
  read_timeout = @timeout
  open_timeout = options[:open_timeout] if options.key?(:open_timeout)
  read_timeout = options[:read_timeout] if options.key?(:read_timeout)
  http.open_timeout = open_timeout
  http.read_timeout = read_timeout
  request = build_request(message)
  http.start do
    http.request(request) do |response|
      yield(response) if block_given?
    end
  end
end
subscribe(message, options={}, &block) click to toggle source

Subscribes something and receives zero or more published messages.

@overload subscribe(message, options={}, &block)

This is asynchronously version.

@param message [Hash] Subscribe message.
@param options [Hash] The options.
   TODO: WRITE ME
@yield [message]
   The block is called when a published message is received.
   The block may be called zero or more times.
@yieldparam [Object] message
   The published message.

@return [Request] The request object.
# File lib/droonga/client/connection/http.rb, line 120
def subscribe(message, options={}, &block)
  thread = Thread.new do
    json_parser = Yajl::Parser.new
    json_parser.on_parse_complete = block
    send(message, options.merge(:read_timeout => nil)) do |response|
      response.read_body do |chunk|
        json_parser << chunk
      end
    end
  end
  Request.new(thread)
end

Private Instance Methods

build_droonga_path(type) click to toggle source
# File lib/droonga/client/connection/http.rb, line 195
def build_droonga_path(type)
  type = type.gsub(".", "/")
  "/droonga/#{type}"
end
build_path(message, parameters) click to toggle source
# File lib/droonga/client/connection/http.rb, line 185
def build_path(message, parameters)
  type = message["type"]
  base_path = message["path"] || build_droonga_path(type)
  if parameters.empty?
    base_path
  else
    "#{base_path}?#{Rack::Utils.build_nested_query(parameters)}"
  end
end
on_error(error) click to toggle source
# File lib/droonga/client/connection/http.rb, line 200
def on_error(error)
  @on_error.call(error) if @on_error
end