class Hawkular::Operations::Client::WebSocket::Frame::Data

helper for parsing the “OperationName=json_payload” messages

Public Instance Methods

to_msg_hash() click to toggle source
   # File lib/hawkular/operations/operations_api.rb
35 def to_msg_hash
36   operation_name, json = split('=', 2)
37 
38   # Check if there is a zip file following JSON.
39   # This check is done only in the first 100KB, hoping it's unlikely to
40   # have such large amount of JSON before a zip file is attached.
41   magic_bits = [
42     "\x50\x4B\x03\x04", # "PK" + 0x03 + 0x04 = Regular ZIP file
43     "\x50\x4B\x05\x06", # "PK" + 0x05 + 0x06 = Empty ZIP
44     "\x50\x4B\x07\x08"  # "PK" + 0x07 + 0x08 = Spanned ZIP
45   ]
46   search_chunk = json[0, 102_400]
47   zip_file = nil
48 
49   magic_bits.each do |bits|
50     idx = search_chunk.index(bits)
51 
52     next unless idx
53 
54     zip_file = json[idx..-1]
55     json = json[0, idx]
56     break
57   end
58 
59   # Parse JSON and, if received, attach zip file
60   json = JSON.parse(json)
61   json[:attachments] = zip_file
62 
63   # Return processed data
64   { operationName: operation_name, data: json }
65 rescue
66   {}
67 end