Sha256: 7c116e1b2fb7cbdb94285bf22ca4eef012b84ab637343b4c3d3b2cf54c1ef09a

Contents?: true

Size: 1.68 KB

Versions: 81

Compression:

Stored size: 1.68 KB

Contents

module Riddle
  class Client
    # This class takes care of the translation of ints, strings and arrays to
    # the format required by the Sphinx service.
    class Message
      def initialize
        @message = ""
        @size_method = @message.respond_to?(:bytesize) ? :bytesize : :length
      end
      
      # Append raw data (only use if you know what you're doing)
      def append(*args)
        return if args.length == 0
        
        args.each { |arg| @message << arg }
      end
      
      # Append a string's length, then the string itself
      def append_string(str)
        @message << [str.send(@size_method)].pack('N') + str
      end
      
      # Append an integer
      def append_int(int)
        @message << [int].pack('N')
      end
      
      def append_64bit_int(int)
        @message << [int >> 32, int & 0xFFFFFFFF].pack('NN')
      end
      
      # Append a float
      def append_float(float)
        @message << [float].pack('f').unpack('L*').pack("N")
      end
      
      # Append multiple integers
      def append_ints(*ints)
        ints.each { |int| append_int(int) }
      end
      
      def append_64bit_ints(*ints)
        ints.each { |int| append_64bit_int(int) }
      end
      
      # Append multiple floats
      def append_floats(*floats)
        floats.each { |float| append_float(float) }
      end
      
      # Append an array of strings - first appends the length of the array,
      # then each item's length and value.
      def append_array(array)
        append_int(array.length)
        
        array.each { |item| append_string(item) }
      end
      
      # Returns the entire message
      def to_s
        @message
      end
    end
  end
end

Version data entries

81 entries across 81 versions & 18 rubygems

Version Path
DrMark-thinking-sphinx-0.9.7 lib/riddle/client/message.rb
DrMark-thinking-sphinx-0.9.8 lib/riddle/client/message.rb
DrMark-thinking-sphinx-0.9.9 lib/riddle/client/message.rb
DrMark-thinking-sphinx-1.1.14 vendor/riddle/lib/riddle/client/message.rb
DrMark-thinking-sphinx-1.1.15 vendor/riddle/lib/riddle/client/message.rb
DrMark-thinking-sphinx-1.1.6 vendor/riddle/lib/riddle/client/message.rb
bdimcheff-dm-sphinx-adapter-0.8.0 lib/riddle/client/message.rb
dpickett-thinking-sphinx-1.1.12 vendor/riddle/lib/riddle/client/message.rb
dpickett-thinking-sphinx-1.1.23 vendor/riddle/lib/riddle/client/message.rb
dpickett-thinking-sphinx-1.1.4 vendor/riddle/lib/riddle/client/message.rb
ebeigarts-thinking-sphinx-1.1.21 vendor/riddle/lib/riddle/client/message.rb
ebeigarts-thinking-sphinx-1.1.22 vendor/riddle/lib/riddle/client/message.rb
freelancing-god-riddle-0.9.8.1371.1 lib/riddle/client/message.rb
freelancing-god-riddle-0.9.8.1371.2 lib/riddle/client/message.rb
freelancing-god-riddle-0.9.8.1533.1 lib/riddle/client/message.rb
freelancing-god-riddle-0.9.8.1533.2 lib/riddle/client/message.rb
freelancing-god-riddle-0.9.8.1533.3 lib/riddle/client/message.rb
freelancing-god-riddle-0.9.8.1533.4 lib/riddle/client/message.rb
freelancing-god-riddle-0.9.8.1533.5 lib/riddle/client/message.rb
freelancing-god-thinking-sphinx-0.9.11 vendor/riddle/lib/riddle/client/message.rb