Class: IssueCentre::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/issue_centre/response.rb

Class Method Summary collapse

Class Method Details

.parse(savon_response, options = {}) ⇒ Array

Parse the response retrieved during any request.

Parameters:

  • savon_response (XML)

    Savon’s representation of the response

  • options (Hash) (defaults to: {})

    Any default attributes that should be added to the model

Returns:

  • (Array)

    Array of hashes with combined response details



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/issue_centre/response.rb', line 15

def parse( savon_response, options = {})

  # retrieve the embedded xml
  embedded_xml = savon_response.xpath( '//return')

  # check for responses we don't understand
  if embedded_xml.blank?
    raise IssueCentre::ParseError.new( savon_response.body.keys.first)
  end

  # parse the embbeded xml
  doc = Nokogiri::XML.parse( embedded_xml.text)
  model_name = which_model( doc)
  
  case doc.internal_subset.name
  when 'generateKey'
    # special case for session keys
    return doc.text
  else                    
    if doc.root.name == 'return'
      # Embedded XML within an embedded XML. Yuk.
      raise IssueCentre::AuthenticationError.new( doc.text)
    end
  end

  arr, obj = [], {}
  arr, obj = recurse_and_build_model( doc, model_name, arr, obj)
  arr << obj unless obj.empty?
  return arr
end

.recurse_and_build_model(fragment, model_name, arr, obj) ⇒ Array, Hash

Traverse through the namespace tree, building the object(s) along the way

Parameters:

  • fragment (Nokogiri::XML)

    Fragment of the XML tree

  • model_name (String)

    IssueCentre model being built

  • arr (Array)

    Array of object hashes built so far

  • obj (Hash)

    Object to be built (or being built)

Returns:

  • (Array)

    Array of hashes representing the XML objects

  • (Hash)

    Hash representing the last object being built (which may be empty)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/issue_centre/response.rb', line 59

def recurse_and_build_model( fragment, model_name, arr, obj)
  fragment.elements.each do |element|
    arr, obj = recurse_and_build_model( element, model_name, arr, obj)
  end
  fragment.attribute_nodes.each do |attr|
    case attr.name
    when 'defaultContract'
      obj[ :id] = attr.value.to_i
      obj[ :default_contract] = true
      obj[ :name] = 'defaultContract'
    when 'company'
      obj[ :company_name] = attr.value
    when 'country'
      obj[ :country_id] = attr.value
    when 'isDefault'
      obj[ :is_default] = attr.value == "1"
    when 'ticketCount', 'changeType', 'supportType', 'totalrecords'
      # skip (we don't need summaries)
    else
      obj[ attr.name.underscore.to_sym] = attr.value
    end
  end
  if fragment.attribute_nodes.empty? && fragment.elements.empty?
    # We're now close to the leaves themselves
    fragment.children.each do |child|
      if child.text?
        case fragment.name
        when 'date', 'dateclosed'
          obj[ fragment.name.underscore.to_sym] =
            Time.at( child.text.to_i / 1000)
        else
          obj[ fragment.name.underscore.to_sym] = child.text
        end
      end
    end
  end
  if fragment.name == model_name
    arr << obj unless obj.empty?
    obj = {}
  end
  return arr, obj
end