Class: RelayHelper::Response
- Inherits:
-
Object
- Object
- RelayHelper::Response
- Defined in:
- lib/weechat-relay-helper/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#id ⇒ Object
Returns the value of attribute id.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #decompress! ⇒ Object
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #read(num_bytes) ⇒ Object
- #read_array ⇒ Object
- #read_buffer_of_bytes ⇒ Object
- #read_char ⇒ Object
- #read_compressed ⇒ Object
- #read_hashtable ⇒ Object
- #read_hdata ⇒ Object
- #read_id ⇒ Object
- #read_infolist ⇒ Object
- #read_infolist_item ⇒ Object
- #read_int ⇒ Object
- #read_length ⇒ Object
- #read_long ⇒ Object
- #read_part(type = nil) ⇒ Object
- #read_pointer ⇒ Object
- #read_string ⇒ Object
- #read_time ⇒ Object
- #read_type ⇒ Object
Constructor Details
#initialize(response) ⇒ Response
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/weechat-relay-helper/response.rb', line 5 def initialize response @text = response.force_encoding('ASCII-8BIT') @length = 0 @index = 0 @compressed = false @data = [] @id = '' read_length read_compressed decompress! if @compressed read_id while @index < @text.length @data << read_part end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/weechat-relay-helper/response.rb', line 4 def data @data end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/weechat-relay-helper/response.rb', line 4 def id @id end |
#text ⇒ Object
Returns the value of attribute text.
4 5 6 |
# File 'lib/weechat-relay-helper/response.rb', line 4 def text @text end |
Instance Method Details
#decompress! ⇒ Object
63 64 65 66 |
# File 'lib/weechat-relay-helper/response.rb', line 63 def decompress! unenc = Zlib.inflate(@text[5, @length]) @text[5, @length] = unenc end |
#read(num_bytes) ⇒ Object
20 21 22 23 24 |
# File 'lib/weechat-relay-helper/response.rb', line 20 def read num_bytes ret = @text[@index, num_bytes] @index += num_bytes ret end |
#read_array ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/weechat-relay-helper/response.rb', line 139 def read_array type = read_type count = read_int arr = [] count.times do arr << read_part(type) end arr end |
#read_buffer_of_bytes ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/weechat-relay-helper/response.rb', line 88 def read_buffer_of_bytes len = read(4).unpack('l>')[0] if len == -1 nil else read(len) end end |
#read_char ⇒ Object
70 71 72 |
# File 'lib/weechat-relay-helper/response.rb', line 70 def read_char read(1).unpack('c')[0] end |
#read_compressed ⇒ Object
60 61 62 |
# File 'lib/weechat-relay-helper/response.rb', line 60 def read_compressed @compressed = true unless read_char.zero? end |
#read_hashtable ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/weechat-relay-helper/response.rb', line 109 def read_hashtable key_type = read_type val_type = read_type count = read_int hsh = {} count.times do hsh[read_part(key_type)] = read_part(val_type) end hsh end |
#read_hdata ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/weechat-relay-helper/response.rb', line 119 def read_hdata hpath = read_string keys = read_string.split(',').map { |k| k.split(':') } count = read_int paths = hpath.split('/') objects = [] count.times do hsh = {} pointers = paths.map do read_pointer end hsh[:pointers] = pointers keys.each do |objdef| hsh.update({ objdef[0] => read_part(objdef[1])}) end objects << hsh end objects end |
#read_id ⇒ Object
67 68 69 |
# File 'lib/weechat-relay-helper/response.rb', line 67 def read_id @id = read_string end |
#read_infolist ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/weechat-relay-helper/response.rb', line 148 def read_infolist name = read_string count = read_int info = [] count.times do info << read_infolist_item end { name: name, data: info } end |
#read_infolist_item ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/weechat-relay-helper/response.rb', line 157 def read_infolist_item count = read_int ret = {} count.times do name = read_string type = read_type value = read_part(type) ret[name] = value end ret end |
#read_int ⇒ Object
73 74 75 |
# File 'lib/weechat-relay-helper/response.rb', line 73 def read_int read(4).unpack('l>')[0] end |
#read_length ⇒ Object
57 58 59 |
# File 'lib/weechat-relay-helper/response.rb', line 57 def read_length @length = read(4).unpack('L>')[0] end |
#read_long ⇒ Object
76 77 78 79 |
# File 'lib/weechat-relay-helper/response.rb', line 76 def read_long len = read(1).unpack('c')[0] read(len).to_i end |
#read_part(type = nil) ⇒ Object
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 |
# File 'lib/weechat-relay-helper/response.rb', line 28 def read_part type=nil type ||= read_type case type when 'chr' read_char when 'int' read_int when 'lon' read_long when 'str' read_string when 'buf' read_buffer_of_bytes when 'ptr' read_pointer when 'tim' read_time when 'htb' read_hashtable when 'hda' read_hdata when 'arr' read_array when 'inf' read_info when 'inl' read_infolist end end |
#read_pointer ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/weechat-relay-helper/response.rb', line 96 def read_pointer len = read(1).unpack('c')[0] ret = read(len).to_i if len == 1 && ret == 0 nil else ret end end |
#read_string ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/weechat-relay-helper/response.rb', line 80 def read_string len = read(4).unpack('l>')[0] if len == -1 nil else read(len) end end |
#read_time ⇒ Object
105 106 107 108 |
# File 'lib/weechat-relay-helper/response.rb', line 105 def read_time len = read(1).unpack('c')[0] Time.at(read(len).to_i) end |
#read_type ⇒ Object
25 26 27 |
# File 'lib/weechat-relay-helper/response.rb', line 25 def read_type read(3) end |