Class: Ignite::Response
- Inherits:
-
Object
- Object
- Ignite::Response
- Defined in:
- lib/ignite/response.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#initialize(client) ⇒ Response
constructor
A new instance of Response.
- #read(len) ⇒ Object
- #read_bool ⇒ Object
- #read_bool_array ⇒ Object
- #read_byte ⇒ Object
- #read_byte_array ⇒ Object
- #read_char ⇒ Object
- #read_data_object ⇒ Object
- #read_date ⇒ Object
-
#read_decimal ⇒ Object
same as Python.
- #read_double ⇒ Object
- #read_double_array ⇒ Object
- #read_float ⇒ Object
- #read_int ⇒ Object
- #read_long ⇒ Object
- #read_long_array ⇒ Object
- #read_short ⇒ Object
- #read_string ⇒ Object
- #read_string_object ⇒ Object
- #read_timestamp ⇒ Object
Constructor Details
#initialize(client) ⇒ Response
Returns a new instance of Response.
5 6 7 8 9 10 11 |
# File 'lib/ignite/response.rb', line 5 def initialize(client) @client = client # use buffer so errors don't leave unread data on socket len = client.read(SIZE_INT).unpack1(PACK_INT) @buffer = StringIO.new(client.read(len)) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/ignite/response.rb', line 3 def client @client end |
Instance Method Details
#read(len) ⇒ Object
13 14 15 |
# File 'lib/ignite/response.rb', line 13 def read(len) @buffer.read(len) end |
#read_bool ⇒ Object
45 46 47 |
# File 'lib/ignite/response.rb', line 45 def read_bool read_byte != 0 end |
#read_bool_array ⇒ Object
78 79 80 |
# File 'lib/ignite/response.rb', line 78 def read_bool_array read_byte_array.map { |v| v != 0 } end |
#read_byte ⇒ Object
17 18 19 |
# File 'lib/ignite/response.rb', line 17 def read_byte read(SIZE_BYTE).unpack1(PACK_BYTE) end |
#read_byte_array ⇒ Object
66 67 68 |
# File 'lib/ignite/response.rb', line 66 def read_byte_array read_array(SIZE_BYTE, PACK_BYTE) end |
#read_char ⇒ Object
41 42 43 |
# File 'lib/ignite/response.rb', line 41 def read_char read(SIZE_CHAR).unpack1(PACK_CHAR) end |
#read_data_object ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ignite/response.rb', line 109 def read_data_object type_code = read_byte case type_code when TYPE_BYTE read_byte when TYPE_SHORT read_short when TYPE_INT read_int when TYPE_LONG read_long when TYPE_FLOAT read_float when TYPE_DOUBLE read_double when TYPE_CHAR read_char when TYPE_BOOL read_bool when TYPE_STRING read_string when TYPE_DATE read_date when TYPE_BYTE_ARRAY read_byte_array when TYPE_LONG_ARRAY read_long_array when TYPE_DOUBLE_ARRAY read_double_array when TYPE_BOOL_ARRAY read_bool_array when TYPE_DECIMAL read_decimal when TYPE_TIMESTAMP when TYPE_NULL nil else raise Error, "Type not supported yet: #{type_code}. Please create an issue." end end |
#read_date ⇒ Object
60 61 62 63 64 |
# File 'lib/ignite/response.rb', line 60 def read_date msecs_since_epoch = read_long sec = msecs_since_epoch / 1000 Time.at(sec).to_date end |
#read_decimal ⇒ Object
same as Python
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ignite/response.rb', line 83 def read_decimal scale = read_int length = read_int data = read(length).unpack("C*") sign = (data[0] & 0x80) != 0 data[0] = data[0] & 0x7f result = 0 data.reverse.each_with_index do |v, i| result += v * 0x100 ** i end result = result / BigDecimal("10") ** BigDecimal(scale) result = -result if sign result end |
#read_double ⇒ Object
37 38 39 |
# File 'lib/ignite/response.rb', line 37 def read_double read(SIZE_DOUBLE).unpack1(PACK_DOUBLE) end |
#read_double_array ⇒ Object
74 75 76 |
# File 'lib/ignite/response.rb', line 74 def read_double_array read_array(SIZE_DOUBLE, PACK_DOUBLE) end |
#read_float ⇒ Object
33 34 35 |
# File 'lib/ignite/response.rb', line 33 def read_float read(SIZE_FLOAT).unpack1(PACK_FLOAT) end |
#read_int ⇒ Object
25 26 27 |
# File 'lib/ignite/response.rb', line 25 def read_int read(SIZE_INT).unpack1(PACK_INT) end |
#read_long ⇒ Object
29 30 31 |
# File 'lib/ignite/response.rb', line 29 def read_long read(SIZE_LONG).unpack1(PACK_LONG) end |
#read_long_array ⇒ Object
70 71 72 |
# File 'lib/ignite/response.rb', line 70 def read_long_array read_array(SIZE_LONG, PACK_LONG) end |
#read_short ⇒ Object
21 22 23 |
# File 'lib/ignite/response.rb', line 21 def read_short read(SIZE_SHORT).unpack1(PACK_SHORT) end |
#read_string ⇒ Object
49 50 51 52 |
# File 'lib/ignite/response.rb', line 49 def read_string len = read_int read(len) end |
#read_string_object ⇒ Object
54 55 56 57 58 |
# File 'lib/ignite/response.rb', line 54 def read_string_object type = read_byte raise Error, "Expected string, not type #{type}" unless type == TYPE_STRING read_string end |
#read_timestamp ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/ignite/response.rb', line 101 def msecs_since_epoch = read_long msec_fraction_in_nsecs = read_int sec = msecs_since_epoch / 1000 nsec = (msecs_since_epoch % 1000) * 1000000 + msec_fraction_in_nsecs Time.at(sec, nsec, :nanosecond) end |