File: response.rb

package info (click to toggle)
ruby-riddle 2.3.1-2~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: sql: 25,022; php: 5,992; ruby: 4,757; sh: 59; makefile: 5
file content (96 lines) | stat: -rw-r--r-- 2,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

module Riddle
  class Client
    # Used to interrogate responses from the Sphinx daemon. Keep in mind none
    # of the methods here check whether the data they're grabbing are what the
    # user expects - it just assumes the user knows what the data stream is
    # made up of.
    class Response
      # Create with the data to interpret
      def initialize(str)
        @str = str
        @marker = 0
      end

      # Return the next string value in the stream
      def next
        len = next_int
        result = @str[@marker, len]
        @marker += len

        Riddle.encode(result)
      end

      # Return the next integer value from the stream
      def next_int
        int = @str[@marker, 4].unpack('N*').first
        @marker += 4

        int
      end

      def next_64bit_int
        high, low = @str[@marker, 8].unpack('N*N*')[0..1]
        @marker += 8

        (high << 32) + low
      end

      # Return the next float value from the stream
      def next_float
        float = @str[@marker, 4].unpack('N*').pack('L').unpack('f*').first
        @marker += 4

        float
      end

      # Returns an array of string items
      def next_array
        count = next_int
        items = []
        count.times do
          items << self.next
        end

        items
      end

      # Returns an array of int items
      def next_int_array
        count = next_int
        items = []
        count.times do
          items << self.next_int
        end

        items
      end

      def next_float_array
        count = next_int
        items = []
        count.times do
          items << self.next_float
        end

        items
      end

      def next_64bit_int_array
        byte_count = next_int
        items = []
        (byte_count / 2).times do
          items << self.next_64bit_int
        end

        items
      end

      # Returns the length of the streamed data
      def length
        @str.length
      end
    end
  end
end