aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/vendor/xml_node.rb
diff options
context:
space:
mode:
authorTobias Lütke <tobias.luetke@gmail.com>2006-03-05 18:59:58 +0000
committerTobias Lütke <tobias.luetke@gmail.com>2006-03-05 18:59:58 +0000
commit03d37a2d68c1940f65d5a65a51ae747955a5b075 (patch)
treeaf6825facc53769e9c16a6cb97a3cc281c125613 /actionpack/lib/action_controller/vendor/xml_node.rb
parent4f00c70580e376691bd1d6c1f9d09efbaa7bf9c9 (diff)
downloadrails-03d37a2d68c1940f65d5a65a51ae747955a5b075.tar.gz
rails-03d37a2d68c1940f65d5a65a51ae747955a5b075.tar.bz2
rails-03d37a2d68c1940f65d5a65a51ae747955a5b075.zip
Added new infrastructure support for REST webservices.
By default application/xml posts are handled by creating a XmlNode object with the same name as the root element of the submitted xml. M$ ActionController::Base.param_parsers['application/atom+xml'] = Proc.new do |data| node = REXML::Document.new(post) { node.root.name => node.root } end XmlSimple and Yaml web services were retired, ActionController::Base.param_parsers carries an example which shows how to get this functio$ request.[formatted_post?, xml_post?, yaml_post? and post_format] were all deprecated in favor of request.content_type [Tobias Luetke] Closes #4081 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3777 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/vendor/xml_node.rb')
-rw-r--r--actionpack/lib/action_controller/vendor/xml_node.rb98
1 files changed, 98 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/vendor/xml_node.rb b/actionpack/lib/action_controller/vendor/xml_node.rb
new file mode 100644
index 0000000000..c4e2f98fbf
--- /dev/null
+++ b/actionpack/lib/action_controller/vendor/xml_node.rb
@@ -0,0 +1,98 @@
+require 'rexml/document'
+
+# SimpleXML like xml parser. Written by leon breet from the ruby on rails Mailing list
+#
+class XmlNode
+ attr :node
+
+ def initialize(node, options = {})
+ @node = node
+ @children = {}
+ @raise_errors = options[:raise_errors]
+ end
+
+ def self.from_xml(xml_or_io)
+ document = REXML::Document.new(xml_or_io)
+ if document.root
+ XmlNode.new(document.root)
+ else
+ XmlNode.new(document)
+ end
+ end
+
+ def node_encoding
+ @node.encoding
+ end
+
+ def node_name
+ @node.name
+ end
+
+ def node_value
+ @node.text
+ end
+
+ def node_value=(value)
+ @node.text = value
+ end
+
+ def xpath(expr)
+ matches = nil
+ REXML::XPath.each(@node, expr) do |element|
+ matches ||= XmlNodeList.new
+ matches << (@children[element] ||= XmlNode.new(element))
+ end
+ matches
+ end
+
+ def method_missing(name, *args)
+ name = name.to_s
+ nodes = nil
+ @node.each_element(name) do |element|
+ nodes ||= XmlNodeList.new
+ nodes << (@children[element] ||= XmlNode.new(element))
+ end
+ nodes
+ end
+
+ def <<(node)
+ if node.is_a? REXML::Node
+ child = node
+ elsif node.respond_to? :node
+ child = node.node
+ end
+ @node.add_element child
+ @children[child] ||= XmlNode.new(child)
+ end
+
+ def [](name)
+ @node.attributes[name.to_s]
+ end
+
+ def []=(name, value)
+ @node.attributes[name.to_s] = value
+ end
+
+ def to_s
+ @node.to_s
+ end
+
+ def to_i
+ to_s.to_i
+ end
+end
+
+class XmlNodeList < Array
+ def [](i)
+ i.is_a?(String) ? super(0)[i] : super(i)
+ end
+
+ def []=(i, value)
+ i.is_a?(String) ? self[0][i] = value : super(i, value)
+ end
+
+ def method_missing(name, *args)
+ name = name.to_s
+ self[0].__send__(name, *args)
+ end
+end \ No newline at end of file