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.



8
9
10
11
12
# File 'lib/riddle/client/message.rb', line 8

def initialize
  @message = StringIO.new String.new(""), "w"
  @message.set_encoding 'ASCII-8BIT'
  @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)



15
16
17
# File 'lib/riddle/client/message.rb', line 15

def append(*args)
  args.each { |arg| @message << arg }
end

#append_64bit_int(int) ⇒ Object



30
31
32
# File 'lib/riddle/client/message.rb', line 30

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

#append_64bit_ints(*ints) ⇒ Object



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

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.



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

def append_array(array)
  append_int(array.length)

  array.each { |item| append_string(item) }
end

#append_boolean(bool) ⇒ Object



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

def append_boolean(bool)
  append_int(bool ? 1 : 0)
end

#append_float(float) ⇒ Object

Append a float



35
36
37
# File 'lib/riddle/client/message.rb', line 35

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

#append_floats(*floats) ⇒ Object

Append multiple floats



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

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

#append_int(int) ⇒ Object

Append an integer



26
27
28
# File 'lib/riddle/client/message.rb', line 26

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

#append_ints(*ints) ⇒ Object

Append multiple integers



44
45
46
# File 'lib/riddle/client/message.rb', line 44

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

#append_string(str) ⇒ Object

Append a string’s length, then the string itself



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

def append_string(str)
  string = Riddle.encode(str.dup, 'ASCII-8BIT')
  @message << [string.send(@size_method)].pack('N') + string
end

#to_sObject

Returns the entire message



66
67
68
# File 'lib/riddle/client/message.rb', line 66

def to_s
  @message.string
end