main
1# frozen_string_literal: true
2
3require 'saml/kit/namespaces'
4
5module Saml
6 module Kit
7 module XmlParseable
8 NAMESPACES = {
9 NameFormat: ::Saml::Kit::Namespaces::ATTR_SPLAT,
10 ds: ::Xml::Kit::Namespaces::XMLDSIG,
11 md: ::Saml::Kit::Namespaces::METADATA,
12 saml: ::Saml::Kit::Namespaces::ASSERTION,
13 samlp: ::Saml::Kit::Namespaces::PROTOCOL,
14 xmlenc: ::Xml::Kit::Namespaces::XMLENC,
15 }.freeze
16
17 # Returns the SAML document returned as a Hash.
18 def to_h
19 @to_h ||= Hash.from_xml(to_s) || {}
20 end
21
22 # Returns the XML document as a String.
23 #
24 # @param pretty [Boolean] true to return a human friendly version
25 # of the XML.
26 def to_xml(pretty: nil)
27 pretty ? to_nokogiri.to_xml(indent: 2) : to_s
28 end
29
30 # Returns the SAML document as an XHTML string.
31 # This is useful for rendering in a web page.
32 def to_xhtml
33 Nokogiri::XML(to_xml, &:noblanks).to_xhtml
34 end
35
36 def present?
37 to_s.present?
38 end
39
40 # @!visibility private
41 def to_nokogiri
42 @to_nokogiri ||= Nokogiri::XML(to_s)
43 end
44
45 # @!visibility private
46 def at_xpath(xpath)
47 return unless present?
48
49 to_nokogiri.at_xpath(xpath, NAMESPACES)
50 end
51
52 # @!visibility private
53 def search(xpath)
54 to_nokogiri.search(xpath, NAMESPACES)
55 end
56
57 # Returns the XML document as a [String].
58 def to_s
59 content
60 end
61 end
62 end
63end