Module: Mycroft::Helpers

Included in:
Messages
Defined in:
lib/mycroft/helpers.rb

Instance Method Summary collapse

Instance Method Details

#parse_message(msg) ⇒ Object

Parses a message



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mycroft/helpers.rb', line 5

def parse_message(msg)
  msg = msg.to_s
  re = /([A-Z_]+) ({.*})$/
  msg_split = re.match(msg)
  if msg_split.nil?
    re = /^([A-Z_]+)$/
    msg_split = re.match(msg)
    raise "Error: Malformed Message" if not msg_split
    type = msg_split[1]
    data = {}
  else
    type = msg_split[1]
    data = JSON.parse(msg_split[2])
  end
  {type: type, data: data}
end

#send_message(type, message = nil) ⇒ Object

Sends a message of a specific type



23
24
25
26
27
28
29
# File 'lib/mycroft/helpers.rb', line 23

def send_message(type, message=nil)
  message = message.nil? ? message = '' : message.to_json
  body = type + ' ' + message
  body.strip!
  length = body.bytesize
  @client.write("#{length}\n#{body}")
end

#update_dependencies(deps) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/mycroft/helpers.rb', line 31

def update_dependencies(deps)
  deps.each do |capability, instance|
    @dependencies[capability] ||= {}
    instance.each do |app_id, status|
      @dependencies[capability][app_id] = status
    end
  end
end