Class: HPFeeds::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/hpfeeds/decoder.rb

Instance Method Summary collapse

Instance Method Details

#msg_auth(rand, ident, secret) ⇒ Object



30
31
32
33
# File 'lib/hpfeeds/decoder.rb', line 30

def msg_auth(rand, ident, secret)
  mac = Digest::SHA1.digest(rand + secret)
  msg_hdr(OP_AUTH, [ident.length].pack("C") + ident + mac)
end

#msg_publish(ident, chan, msg) ⇒ Object



39
40
41
# File 'lib/hpfeeds/decoder.rb', line 39

def msg_publish(ident, chan, msg)
  msg_hdr(OP_PUBLISH, [ident.length].pack("C") + ident + [chan.length].pack("C") + chan + msg)
end

#msg_subscribe(ident, chan) ⇒ Object



35
36
37
# File 'lib/hpfeeds/decoder.rb', line 35

def msg_subscribe(ident, chan)
  msg_hdr(OP_SUBSCRIBE, [ident.length].pack("C") + ident + chan)
end

#parse_header(header) ⇒ Object

Raises:



6
7
8
9
10
11
# File 'lib/hpfeeds/decoder.rb', line 6

def parse_header(header)
  raise Exception.new("Malformed header") if header.length < 5
  len = header[0,4].unpack("l>")[0]
  op = header[4,1].unpack("C")[0]
  return op, len
end

#parse_info(data) ⇒ Object

Raises:



13
14
15
16
17
18
19
# File 'lib/hpfeeds/decoder.rb', line 13

def parse_info(data)
  len = data[0,1].unpack("C")[0]
  raise Exception.new("Malformed data") if data.length <= len
  name = data[1,len]
  rand = data[(1+len)..-1]
  return name, rand
end

#parse_publish(data) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/hpfeeds/decoder.rb', line 21

def parse_publish(data)
  len = data[0,1].unpack("C")[0]
  name = data[1,len]
  len2 = data[(1+len),1].ord
  chan = data[(1+len+1),len2]
  payload = data[(1+len+1+len2)..-1]
  return name, chan, payload
end