Class: Goliath::Rack::Formatters::XML

Inherits:
Object
  • Object
show all
Includes:
AsyncMiddleware
Defined in:
lib/goliath/rack/formatters/xml.rb

Overview

A XML formatter. Attempts to convert your data into an XML document.

Examples:

use Goliath::Rack::Formatters::XML

Instance Method Summary (collapse)

Methods included from AsyncMiddleware

#call, #initialize

Constructor Details

This class inherits a constructor from Goliath::Rack::AsyncMiddleware

Instance Method Details

- (Object) array_to_xml(content, root = 'results',, item = 'item'))



53
54
55
56
57
# File 'lib/goliath/rack/formatters/xml.rb', line 53

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) hash_to_xml(content, root = 'results',, item = 'item'))



42
43
44
45
46
47
48
49
50
51
# File 'lib/goliath/rack/formatters/xml.rb', line 42

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(env, status, headers, body)



14
15
16
17
# File 'lib/goliath/rack/formatters/xml.rb', line 14

def post_process(env, status, headers, body)
  body = [to_xml(body, false)] if xml_response?(headers)
  [status, headers, body]
end

- (Object) string_to_xml(content)



38
39
40
# File 'lib/goliath/rack/formatters/xml.rb', line 38

def string_to_xml(content)
  ::Rack::Utils.escape_html(content.to_s)
end

- (Object) to_xml(content, fragment = true, root = 'results',, item = 'item'))



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/goliath/rack/formatters/xml.rb', line 23

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 += xml_footer(root) unless fragment
  xml_string
end


65
66
67
# File 'lib/goliath/rack/formatters/xml.rb', line 65

def xml_footer(root)
  "</#{root}>"
end

- (Object) xml_header(root)



59
60
61
62
63
# File 'lib/goliath/rack/formatters/xml.rb', line 59

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)



69
70
71
# File 'lib/goliath/rack/formatters/xml.rb', line 69

def xml_item(key, value, root)
  "<#{key}>#{to_xml(value, true, root)}</#{key}>\n"
end

- (Boolean) xml_response?(headers)

Returns:

  • (Boolean)


19
20
21
# File 'lib/goliath/rack/formatters/xml.rb', line 19

def xml_response?(headers)
  headers['Content-Type'] =~ %r{^application/xml}
end