Class: RubyAsterisk::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-asterisk/response_parser.rb

Overview

Class for parsing response coming from Asterisk

Class Method Summary collapse

Class Method Details

._add_status(exten_array) ⇒ Object



19
20
21
22
23
24
# File 'lib/ruby-asterisk/response_parser.rb', line 19

def self._add_status(exten_array)
  exten_array.each do |hint|
    hint['DescriptiveStatus'] = DESCRIPTIVE_STATUS[hint['Status']]
  end
  exten_array
end

._parse_objects(response, parse_params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby-asterisk/response_parser.rb', line 26

def self._parse_objects(response, parse_params)
  object_array = []
  # AMI frames are CRLF-delimited on the wire; tolerate both \r\n and \n so
  # aggregated multi-event responses parse regardless of line endings.
  object_regex = /#{parse_params[:search_for]}\r?\n(.*?)\r?\n\r?\n/m
  response.join.scan(object_regex) do |match|
    object = {}
    match[0].split(/\r?\n/).each do |line|
      tokens = line.split(':', 2)
      object[tokens[0].strip] = tokens[1].strip unless tokens[1].nil?
    end
    object_array << object unless object.empty?
  end
  object_array = _add_status(object_array) if parse_params[:symbol].eql?(:hints)
  { parse_params[:symbol] => object_array }
end

.parse(raw_response, type) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ruby-asterisk/response_parser.rb', line 11

def self.parse(raw_response, type)
  if PARSE_DATA.include?(type)
    _parse_objects(raw_response, PARSE_DATA[type])
  else
    raw_response
  end
end