Class: Atmos::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/atmos/parser.rb

Constant Summary collapse

NOKOGIRI =
"nokogiri"
REXML =
"rexml"
@@parser =
nil

Class Method Summary collapse

Class Method Details

.parserObject



9
10
11
# File 'lib/atmos/parser.rb', line 9

def self.parser
   @@parser
end

.parser=(which) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

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



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
101
102
103
104
105
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



50
51
52
# File 'lib/atmos/parser.rb', line 50

def self.response_get_string(response, string)
   response_get_array(response,string)[0]
end