class Atmos::Parser

Public Class Methods

parser() click to toggle source
# File lib/atmos/parser.rb, line 9
def self.parser
   @@parser
end
parser=(which) click to toggle source
# File lib/atmos/parser.rb, line 14
def self.parser=(which)
   
   @@parser = which
   if (@@parser == NOKOGIRI)
      require 'nokogiri'
   elsif (@@parser == REXML)
      require 'rexml/document'
   else
      raise Atmos::Exceptions::ArgumentException, "The XML parser has not been set to a known parser."
   end
end
response_check_action_error(action, response) click to toggle source

Utility method to check the atmos server XML response for errors. Throws exception on error.

# File lib/atmos/parser.rb, line 59
def self.response_check_action_error(action, response)
   doc     = nil
   code    = nil
   msg     = nil
   exclass = Atmos::Exceptions::AtmosException
   
   Atmos::LOG.info("body: #{response.body}")
   if (!response.body.nil? && response.body.match(/<\?xml/))
      if (parser == NOKOGIRI)
      
         doc  = Nokogiri::XML(response.body)
      
         code = doc.xpath('//Error/Code')[0]  
         code = code.content if (!code.nil?)
      
         msg  = doc.xpath('//Error/Message')[0]
         msg  = msg.content if (!msg.nil?)
                         
      elsif (parser == REXML)
     
         code = response_get_string(response, '//Error/Code')
         msg  = response_get_string(response, '//Error/Message')

      else
         raise Atmos::Exceptions::InvalidStateException, "The XML parser has not been set to a known parser."
      end
   end
   
   if (!code.nil?)
      
      Atmos::LOG.info("code: #{code}")
      Atmos::LOG.info("msg: #{msg}")

      if (!REST[action][:errors].nil? && !REST[action][:errors][code].nil?)
         tmp = REST[action][:errors][code][:message]
         msg = tmp if (!tmp.nil?)

         tmp = REST[action][:errors][code][:class]
         exclass = tmp if (!tmp.nil?)
      end
                  
      raise exclass, msg
      
   end
   
   true
end
response_get_array(response, string) click to toggle source
# File lib/atmos/parser.rb, line 27
def self.response_get_array(response, string)
   rv = nil
   
   if (parser == NOKOGIRI)
      
      doc = Nokogiri::XML(response.body)
      rv  = doc.xpath(string).map do |elt|
         elt.content
      end
  
   elsif (parser == REXML)

      doc = ::REXML::Document.new(response.body)
      rv  = doc.elements.to_a(string).map do |elt|
         elt.text
      end
      
   end

   rv
end
response_get_string(response, string) click to toggle source
# File lib/atmos/parser.rb, line 50
def self.response_get_string(response, string)
   response_get_array(response,string)[0]
end