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_byte ⇒ Object
- #read_char ⇒ Object
- #read_data_object ⇒ Object
- #read_date ⇒ Object
-
#read_decimal ⇒ Object
same as Python.
- #read_double ⇒ Object
- #read_float ⇒ Object
- #read_int ⇒ Object
- #read_long ⇒ 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(4).unpack1("i!<") @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_byte ⇒ Object
17 18 19 |
# File 'lib/ignite/response.rb', line 17 def read_byte read(1).unpack1("C") end |
#read_char ⇒ Object
41 42 43 |
# File 'lib/ignite/response.rb', line 41 def read_char read(1).unpack1("c") end |
#read_data_object ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ignite/response.rb', line 93 def read_data_object type_code = read_byte case type_code when 1 read_byte when 2 read_short when 3 read_int when 4 read_long when 5 read_float when 6 read_double when 7 read_char when 8 read_bool when 9 read_string when 11 read_date when 30 read_decimal when 33 when 101 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ignite/response.rb', line 67 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(8).unpack1("E") end |
#read_float ⇒ Object
33 34 35 |
# File 'lib/ignite/response.rb', line 33 def read_float read(4).unpack1("e") end |
#read_int ⇒ Object
25 26 27 |
# File 'lib/ignite/response.rb', line 25 def read_int read(4).unpack1("i!<") end |
#read_long ⇒ Object
29 30 31 |
# File 'lib/ignite/response.rb', line 29 def read_long read(8).unpack1("l!<") end |
#read_short ⇒ Object
21 22 23 |
# File 'lib/ignite/response.rb', line 21 def read_short read(2).unpack1("s!<") 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 "Expected string, not type #{type}" unless type == 9 read_string end |
#read_timestamp ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/ignite/response.rb', line 85 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 |