Class: IssueCentre::Response
- Inherits:
-
Object
- Object
- IssueCentre::Response
- Defined in:
- lib/issue_centre/response.rb
Class Method Summary collapse
-
.parse(savon_response, options = {}) ⇒ Array
Parse the response retrieved during any request.
-
.recurse_and_build_model(fragment, model_name, arr, obj) ⇒ Array, Hash
Traverse through the namespace tree, building the object(s) along the way.
Class Method Details
.parse(savon_response, options = {}) ⇒ Array
Parse the response retrieved during any request.
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, = {}) # retrieve the embedded xml = savon_response.xpath( '//return') # check for responses we don't understand if .blank? raise IssueCentre::ParseError.new( savon_response.body.keys.first) end # parse the embbeded xml doc = Nokogiri::XML.parse( .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
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' # 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 |