Class: Goliath::Rack::Formatters::XML
- Inherits:
 - 
      Object
      
        
- Object
 - Goliath::Rack::Formatters::XML
 
 - Defined in:
 - lib/goliath/rack/formatters/xml.rb
 
Overview
A XML formatter. Attempts to convert your data into an XML document.
Instance Method Summary (collapse)
- - (Object) array_to_xml(content, root = 'results',, item = 'item'))
 - - (Object) call(env)
 - - (Object) hash_to_xml(content, root = 'results',, item = 'item'))
 - 
  
    
      - (Goliath::Rack::Formatters::XML) initialize(app) 
    
    
  
  
    constructor
  
  
  
  
  
  
  
    
Called by the framework to create the formatter.
 - - (Object) post_process(status, headers, body)
 - - (Object) string_to_xml(content)
 - - (Object) to_xml(content, fragment = true, root = 'results',, item = 'item'))
 - - (Object) xml_footer(root)
 - - (Object) xml_header(root)
 - - (Object) xml_item(key, value, root)
 - - (Boolean) xml_response?(headers)
 
Constructor Details
- (Goliath::Rack::Formatters::XML) initialize(app)
Called by the framework to create the formatter.
      15 16 17  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 15 def initialize(app) @app = app end  | 
  
Instance Method Details
- (Object) array_to_xml(content, root = 'results',, item = 'item'))
      68 69 70 71 72  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 68 def array_to_xml(content, root='results', item='item') xml_string = '' content.each { |value| xml_string += xml_item(item, value, root) } xml_string end  | 
  
- (Object) call(env)
      19 20 21 22 23 24 25 26 27  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 19 def call(env) async_cb = env['async.callback'] env['async.callback'] = Proc.new do |status, headers, body| async_cb.call(post_process(status, headers, body)) end status, headers, body = @app.call(env) post_process(status, headers, body) end  | 
  
- (Object) hash_to_xml(content, root = 'results',, item = 'item'))
      57 58 59 60 61 62 63 64 65 66  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 57 def hash_to_xml(content, root='results', item='item') xml_string = '' if content.key?('meta') xml_string += xml_item('meta', content['meta'], root) content.delete('meta') end content.each_pair { |key, value| xml_string += xml_item(key, value, root) } xml_string end  | 
  
- (Object) post_process(status, headers, body)
      29 30 31 32  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 29 def post_process(status, headers, body) body = [to_xml(body, false)] if xml_response?(headers) [status, headers, body] end  | 
  
- (Object) string_to_xml(content)
      53 54 55  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 53 def string_to_xml(content) ::Rack::Utils.escape_html(content.to_s) end  | 
  
- (Object) to_xml(content, fragment = true, root = 'results',, item = 'item'))
      38 39 40 41 42 43 44 45 46 47 48 49 50 51  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 38 def to_xml(content, fragment=true, root='results', item='item') xml_string = '' xml_string += xml_header(root) unless fragment xml_string += case(content.class.to_s) when "Hash" then hash_to_xml(content, root, item) when "Array" then array_to_xml(content, root, item) when "String" then string_to_xml(content) else string_to_xml(content) end xml_string += (root) unless fragment xml_string end  | 
  
      80 81 82  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 80 def (root) "</#{root}>" end  | 
  
- (Object) xml_header(root)
      74 75 76 77 78  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 74 def xml_header(root) "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<#{root} xmlns:opensearch='http://a9.com/-/spec/opensearch/1.1/'\n" + " xmlns:postrank='http://www.postrank.com/xsd/2007-11-30/postrank'>\n" end  | 
  
- (Object) xml_item(key, value, root)
      84 85 86  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 84 def xml_item(key, value, root) "<#{key}>#{to_xml(value, true, root)}</#{key}>\n" end  | 
  
- (Boolean) xml_response?(headers)
      34 35 36  | 
    
      # File 'lib/goliath/rack/formatters/xml.rb', line 34 def xml_response?(headers) headers['Content-Type'] =~ %r{^application/xml} end  |