Sha256: ca3bd6b17e87c83e17ac1e1c25d0e9dc413c126f42810a4f63112b311301aba6

Contents?: true

Size: 1.74 KB

Versions: 18

Compression:

Stored size: 1.74 KB

Contents

module Sphinx
  # Unpack internal Sphinx representation of ints, floats, strings, and arrays.
  # needed by Sphinx search engine.
  class Response
    # Initialize new request.
    def initialize(response)
      @response = response
      @position = 0
      @size = response.length
    end
    
    # Gets current stream position.
    def position
      @position
    end
    
    # Gets response size.
    def size
      @size
    end
    
    # Returns <tt>true</tt> when response stream is out.
    def eof?
      @position >= @size
    end

    # Get int from stream.
    def get_int
      raise EOFError if @position + 4 > @size
      value = @response[@position, 4].unpack('N*').first
      @position += 4
      return value
    end

    # Get 64-bit int from stream.
    def get_int64
      raise EOFError if @position + 8 > @size
      hi, lo = @response[@position, 8].unpack('N*N*')
      @position += 8
      return (hi << 32) + lo
    end

    # Get array of <tt>count</tt> ints from stream.
    def get_ints(count)
      length = 4 * count
      raise EOFError if @position + length > @size
      values = @response[@position, length].unpack('N*' * count)
      @position += length
      return values
    end
    
    # Get string from stream.
    def get_string
      length = get_int
      raise EOFError if @position + length > @size
      value = length > 0 ? @response[@position, length] : ''
      @position += length
      return value
    end
    
    # Get float from stream.
    def get_float
      raise EOFError if @position + 4 > @size
      uval = @response[@position, 4].unpack('N*').first;
      @position += 4
      return ([uval].pack('L')).unpack('f*').first
    end
  end
end

Version data entries

18 entries across 18 versions & 6 rubygems

Version Path
kpumuk-rlibsphinxclient-0.2.0 lib/sphinx/response.rb
kpumuk-rlibsphinxclient-0.2.1 lib/sphinx/response.rb
ninjudd-model_set-0.10.0 vendor/sphinx_client/lib/sphinx/response.rb
ninjudd-model_set-0.10.1 vendor/sphinx_client/lib/sphinx/response.rb
ninjudd-model_set-0.10.2 vendor/sphinx_client/lib/sphinx/response.rb
ninjudd-model_set-0.10.3 vendor/sphinx_client/lib/sphinx/response.rb
ninjudd-model_set-0.10.4 vendor/sphinx_client/lib/sphinx/response.rb
ninjudd-model_set-0.10.5 vendor/sphinx_client/lib/sphinx/response.rb
ninjudd-model_set-0.9.2 vendor/sphinx_client/lib/sphinx/response.rb
sander6-enygma-0.1.0 lib/api/sphinx/response.rb
sander6-enygma-0.1.1 lib/api/sphinx/response.rb
zinx-0.0.5 lib/sphinx/sphinx/response.rb
zinx-0.0.4 lib/sphinx/sphinx/response.rb
model_set-1.0.0 vendor/sphinx_client/lib/sphinx/response.rb
model_set-0.11.1 vendor/sphinx_client/lib/sphinx/response.rb
model_set-0.11.0 vendor/sphinx_client/lib/sphinx/response.rb
model_set-0.10.6 vendor/sphinx_client/lib/sphinx/response.rb
rlibsphinxclient-0.2.2 lib/sphinx/response.rb