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 |
# 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 |
# File 'lib/riddle/client/message.rb', line 12 def append(*args) args.each { |arg| @message << arg } end |
#append_64bit_int(int) ⇒ Object
27 28 29 |
# File 'lib/riddle/client/message.rb', line 27 def append_64bit_int(int) @message << [int.to_i >> 32, int.to_i & 0xFFFFFFFF].pack('NN') end |
#append_64bit_ints(*ints) ⇒ Object
45 46 47 |
# File 'lib/riddle/client/message.rb', line 45 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.
56 57 58 59 60 |
# File 'lib/riddle/client/message.rb', line 56 def append_array(array) append_int(array.length) array.each { |item| append_string(item) } end |
#append_boolean(bool) ⇒ Object
36 37 38 |
# File 'lib/riddle/client/message.rb', line 36 def append_boolean(bool) append_int(bool ? 1 : 0) end |
#append_float(float) ⇒ Object
Append a float
32 33 34 |
# File 'lib/riddle/client/message.rb', line 32 def append_float(float) @message << [float].pack('f').unpack('L*').pack("N") end |
#append_floats(*floats) ⇒ Object
Append multiple floats
50 51 52 |
# File 'lib/riddle/client/message.rb', line 50 def append_floats(*floats) floats.each { |float| append_float(float) } end |
#append_int(int) ⇒ Object
Append an integer
23 24 25 |
# File 'lib/riddle/client/message.rb', line 23 def append_int(int) @message << [int.to_i].pack('N') end |
#append_ints(*ints) ⇒ Object
Append multiple integers
41 42 43 |
# File 'lib/riddle/client/message.rb', line 41 def append_ints(*ints) ints.each { |int| append_int(int) } end |
#append_string(str) ⇒ Object
Append a string’s length, then the string itself
17 18 19 20 |
# File 'lib/riddle/client/message.rb', line 17 def append_string(str) string = Riddle.encode(str.dup, 'ASCII-8BIT') @message << [string.send(@size_method)].pack('N') + string end |
#to_s ⇒ Object
Returns the entire message
63 64 65 |
# File 'lib/riddle/client/message.rb', line 63 def to_s @message end |