Class: RelayHelper::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/weechat-relay-helper/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of 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 < @length
    @data << read_part
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/weechat-relay-helper/response.rb', line 4

def data
  @data
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/weechat-relay-helper/response.rb', line 4

def id
  @id
end

#textObject

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_arrayObject



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_bytesObject



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_charObject



70
71
72
# File 'lib/weechat-relay-helper/response.rb', line 70

def read_char
  read(1).unpack('c')[0]
end

#read_compressedObject



60
61
62
# File 'lib/weechat-relay-helper/response.rb', line 60

def read_compressed
  @compressed = true unless read_char.zero?
end

#read_hashtableObject



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_hdataObject



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_idObject



67
68
69
# File 'lib/weechat-relay-helper/response.rb', line 67

def read_id
  @id = read_string
end

#read_infolistObject



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_itemObject



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_intObject



73
74
75
# File 'lib/weechat-relay-helper/response.rb', line 73

def read_int
  read(4).unpack('l>')[0]
end

#read_lengthObject



57
58
59
# File 'lib/weechat-relay-helper/response.rb', line 57

def read_length
  @length = read(4).unpack('L>')[0]
end

#read_longObject



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_pointerObject



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_stringObject



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_timeObject



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_typeObject



25
26
27
# File 'lib/weechat-relay-helper/response.rb', line 25

def read_type
  read(3)
end