Class: Riddle::Client::Message
- Inherits:
-
Object
- Object
- Riddle::Client::Message
- 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
-
#append(*args) ⇒ Object
Append raw data (only use if you know what you’re doing).
- #append_64bit_int(int) ⇒ Object
- #append_64bit_ints(*ints) ⇒ Object
-
#append_array(array) ⇒ Object
Append an array of strings - first appends the length of the array, then each item’s length and value.
- #append_boolean(bool) ⇒ Object
-
#append_float(float) ⇒ Object
Append a float.
-
#append_floats(*floats) ⇒ Object
Append multiple floats.
-
#append_int(int) ⇒ Object
Append an integer.
-
#append_ints(*ints) ⇒ Object
Append multiple integers.
-
#append_string(str) ⇒ Object
Append a string’s length, then the string itself.
-
#initialize ⇒ Message
constructor
A new instance of Message.
-
#to_s ⇒ Object
Returns the entire message.
Constructor Details
#initialize ⇒ Message
Returns a new instance of Message.
6 7 8 9 10 |
# File 'lib/riddle/client/message.rb', line 6 def initialize = StringIO.new String.new(""), "w" .set_encoding 'ASCII-8BIT' @size_method = .respond_to?(:bytesize) ? :bytesize : :length end |
Instance Method Details
#append(*args) ⇒ Object
Append raw data (only use if you know what you’re doing)
13 14 15 |
# File 'lib/riddle/client/message.rb', line 13 def append(*args) args.each { |arg| << arg } end |
#append_64bit_int(int) ⇒ Object
28 29 30 |
# File 'lib/riddle/client/message.rb', line 28 def append_64bit_int(int) << [int.to_i >> 32, int.to_i & 0xFFFFFFFF].pack('NN') end |
#append_64bit_ints(*ints) ⇒ Object
46 47 48 |
# File 'lib/riddle/client/message.rb', line 46 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.
57 58 59 60 61 |
# File 'lib/riddle/client/message.rb', line 57 def append_array(array) append_int(array.length) array.each { |item| append_string(item) } end |
#append_boolean(bool) ⇒ Object
37 38 39 |
# File 'lib/riddle/client/message.rb', line 37 def append_boolean(bool) append_int(bool ? 1 : 0) end |
#append_float(float) ⇒ Object
Append a float
33 34 35 |
# File 'lib/riddle/client/message.rb', line 33 def append_float(float) << [float].pack('f').unpack('L*').pack("N") end |
#append_floats(*floats) ⇒ Object
Append multiple floats
51 52 53 |
# File 'lib/riddle/client/message.rb', line 51 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) << [int.to_i].pack('N') end |
#append_ints(*ints) ⇒ Object
Append multiple integers
42 43 44 |
# File 'lib/riddle/client/message.rb', line 42 def append_ints(*ints) ints.each { |int| append_int(int) } end |
#append_string(str) ⇒ Object
Append a string’s length, then the string itself
18 19 20 21 |
# File 'lib/riddle/client/message.rb', line 18 def append_string(str) string = Riddle.encode(str.dup, 'ASCII-8BIT') << [string.send(@size_method)].pack('N') + string end |
#to_s ⇒ Object
Returns the entire message
64 65 66 |
# File 'lib/riddle/client/message.rb', line 64 def to_s .string end |