Class: WebSocket::Frame::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/hawkular/operations/operations_api.rb

Overview

helper for parsing the “OperationName=json_payload” messages

Instance Method Summary collapse

Instance Method Details

#to_msg_hashObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hawkular/operations/operations_api.rb', line 35

def to_msg_hash
  operation_name, json = split('=', 2)

  # Check if there is a zip file following JSON.
  # This check is done only in the first 100KB, hoping it's unlikely to
  # have such large amount of JSON before a zip file is attached.
  magic_bits = [
    "\x50\x4B\x03\x04", # "PK" + 0x03 + 0x04 = Regular ZIP file
    "\x50\x4B\x05\x06", # "PK" + 0x05 + 0x06 = Empty ZIP
    "\x50\x4B\x07\x08"  # "PK" + 0x07 + 0x08 = Spanned ZIP
  ]
  search_chunk = json[0, 102_400]
  zip_file = nil

  magic_bits.each do |bits|
    idx = search_chunk.index(bits)

    next unless idx

    zip_file = json[idx..-1]
    json = json[0, idx]
    break
  end

  # Parse JSON and, if received, attach zip file
  json = JSON.parse(json)
  json[:attachments] = zip_file

  # Return processed data
  { operationName: operation_name, data: json }
rescue
  {}
end