Class: Riddle::Client::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/riddle/client/message.rb

Overview

This class takes care of the translation of ints, strings and arrays to the format required by the Sphinx service.

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



6
7
8
9
# File 'lib/riddle/client/message.rb', line 6

def initialize
  @message = ""
  @size_method = @message.respond_to?(:bytesize) ? :bytesize : :length
end

Instance Method Details

#append(*args) ⇒ Object

Append raw data (only use if you know what you’re doing)



12
13
14
15
16
# File 'lib/riddle/client/message.rb', line 12

def append(*args)
  return if args.length == 0
  
  args.each { |arg| @message << arg }
end

#append_64bit_int(int) ⇒ Object



28
29
30
# File 'lib/riddle/client/message.rb', line 28

def append_64bit_int(int)
  @message << [int >> 32, int & 0xFFFFFFFF].pack('NN')
end

#append_64bit_ints(*ints) ⇒ Object



42
43
44
# File 'lib/riddle/client/message.rb', line 42

def append_64bit_ints(*ints)
  ints.each { |int| append_64bit_int(int) }
end

#append_array(array) ⇒ Object

Append an array of strings - first appends the length of the array, then each item’s length and value.



53
54
55
56
57
# File 'lib/riddle/client/message.rb', line 53

def append_array(array)
  append_int(array.length)
  
  array.each { |item| append_string(item) }
end

#append_float(float) ⇒ Object

Append a float



33
34
35
# File 'lib/riddle/client/message.rb', line 33

def append_float(float)
  @message << [float].pack('f').unpack('L*').pack("N")
end

#append_floats(*floats) ⇒ Object

Append multiple floats



47
48
49
# File 'lib/riddle/client/message.rb', line 47

def append_floats(*floats)
  floats.each { |float| append_float(float) }
end

#append_int(int) ⇒ Object

Append an integer



24
25
26
# File 'lib/riddle/client/message.rb', line 24

def append_int(int)
  @message << [int].pack('N')
end

#append_ints(*ints) ⇒ Object

Append multiple integers



38
39
40
# File 'lib/riddle/client/message.rb', line 38

def append_ints(*ints)
  ints.each { |int| append_int(int) }
end

#append_string(str) ⇒ Object

Append a string’s length, then the string itself



19
20
21
# File 'lib/riddle/client/message.rb', line 19

def append_string(str)
  @message << [str.send(@size_method)].pack('N') + str
end

#to_sObject

Returns the entire message



60
61
62
# File 'lib/riddle/client/message.rb', line 60

def to_s
  @message
end