Module: OnSIP::ResponseParser::ClassMethods

Included in:
OnSIP::ResponseParser
Defined in:
lib/onsip/response_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse_response(response, key_path = [], ignore_result_keys = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/onsip/response_parser.rb', line 5

def parse_response(response, key_path = [], ignore_result_keys = [])
  result = []

  return result unless response && !response.env.body.blank?

  r = response.env.body

  key_path.each_with_index do |key, i|
    if i < (key_path.length - 1) && r[key]
      r = r[key]
    elsif i == (key_path.length - 1) && r[key] && r[key].kind_of?(Array)
      a = r[key]
      result = a.flatten.map { |h| h.delete_if { |k| ignore_result_keys.include?(k) }; h }
    elsif i == (key_path.length - 1) && r[key] && r[key].kind_of?(Hash)
      h = r[key]
      h.delete_if { |k| ignore_result_keys.include?(k) }
      result = [h]
    else
      break
    end
  end

  result
rescue StandardError => e
  raise OnSIPRequestException.new(:message => 'Problem with parsing response',
                                  :exception => e,
                                  :response => response)
end