Class: RightScale::CloudApi::ResponseParser

Inherits:
Routine show all
Defined in:
lib/base/routines/response_parser.rb

Overview

The routine parses the current response.

The supportes content-types are: xml and json. In the case of any other content-type it does nothing.

Instance Attribute Summary

Attributes inherited from Routine

#data

Instance Method Summary collapse

Methods inherited from Routine

#cloud_api_logger, #execute, #invoke_callback_method, #options, #reset, #with_timer

Instance Method Details

#processObject

Main entry point.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/base/routines/response_parser.rb', line 36

def process
  # There is no way to parse an IO response
  return nil if data[:response][:instance].is_io?

  xml_parser   = Utils::get_xml_parser_class(data[:options][:xml_parser])
  content_type = (data[:response][:instance].headers || {})["content-type"].to_s
  body         = data[:response][:instance].body.to_s

  # If it was explicitly requested not to parse the response then return it as is.
  if data[:options][:raw_response]
    data[:result] = body
    return
  end

  # Find the appropriate parser.
  parser = if body._blank?
             Parser::Plain
           else
             case content_type
             when /xml/              then xml_parser
             when /json|javascript/  then Parser::Json
             else
               if data[:response][:instance].body.to_s[/\A<\?xml /]
                 # Sometimes Amazon does not set a proper header
                 xml_parser
               else
                 Parser::Plain
               end
             end
           end
  # Parse the response
  with_timer("Response parsing with #{parser}") do
    options = {}
    options[:encoding] = 'UTF-8' if /utf-8/i === content_type
    #
    cloud_api_logger.log("Attempting to parse cloud response with: '#{options[:encoding] || 'DEFAULT'}' encoding", :response_parser)
    data[:response][:parsed] = parser::parse(body, options)
  end
  data[:result] = data[:response][:parsed]
end