Module: OnSIP::ResponseParser::ClassMethods

Includes:
Exceptions
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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/onsip/response_parser.rb', line 7

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

    if valid_response?(response)
      value_at_key_path(response.env.body, key_path, ignore_result_keys)
    else
      raise StandardError, 'Problem validating OnSIP response'
    end

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

#valid_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/onsip/response_parser.rb', line 23

def valid_response?(response)
  if response && response.env[:status] == 200 && !response.env.body.blank?
    r = response.env.body
    r['Response']['Context']['Action']['IsCompleted'] == 'false' ? false : true
  else
    false
  end
end

#value_at_key_path(body, key_path = [], ignore_result_keys = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/onsip/response_parser.rb', line 32

def value_at_key_path(body, key_path = [], ignore_result_keys = [])
  result = []

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

  result
end