From 5858f93c0d09e64b2b2fa627418c1d029618af21 Mon Sep 17 00:00:00 2001 From: Brandon Robins Date: Mon, 23 Oct 2017 21:10:24 -0500 Subject: [PATCH] Add XML::Utils module --- lib/calligraphy.rb | 2 ++ lib/calligraphy/xml/utils.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 lib/calligraphy/xml/utils.rb diff --git a/lib/calligraphy.rb b/lib/calligraphy.rb index cd8fa1c..bbf7d8d 100644 --- a/lib/calligraphy.rb +++ b/lib/calligraphy.rb @@ -3,6 +3,8 @@ require 'calligraphy/web_dav_request' require 'calligraphy/xml/builder' require 'calligraphy/xml/namespace' require 'calligraphy/xml/node' +require 'calligraphy/xml/utils' module Calligraphy + DAV_NS = 'DAV:' end diff --git a/lib/calligraphy/xml/utils.rb b/lib/calligraphy/xml/utils.rb new file mode 100644 index 0000000..4fa6d3c --- /dev/null +++ b/lib/calligraphy/xml/utils.rb @@ -0,0 +1,18 @@ +module Calligraphy::XML + module Utils + def xml_for(body:, node:) + xml = Nokogiri::XML body + return :bad_request unless xml.errors.empty? + + namespace = nil + xml.root.namespace_definitions.each do |n| + namespace = "#{n.prefix}|" if n&.href == Calligraphy::DAV_NS && !n.prefix.nil? + end + namespace + + node = node.split(' ').map! { |n| namespace + n }.join(' ') if namespace + + xml.css(node).children + end + end +end