Commit 151919b

mo <mo@mokhan.ca>
2018-03-17 02:01:47
extract organization class.
1 parent 9e240b6
lib/saml/kit/composite_metadata.rb
@@ -9,6 +9,7 @@ module Saml
     # and SPSSODescriptor element.
     class CompositeMetadata < Metadata # :nodoc:
       include Enumerable
+
       attr_reader :service_provider, :identity_provider
 
       def initialize(xml)
@@ -19,6 +20,18 @@ module Saml
         ]
       end
 
+      def organization
+        find { |x| x.organization.present? }.try(:organization)
+      end
+
+      def organization_name
+        organization.name
+      end
+
+      def organization_url
+        organization.url
+      end
+
       def services(type)
         xpath = map do |x|
           "//md:EntityDescriptor/md:#{x.name}/md:#{type}"
lib/saml/kit/metadata.rb
@@ -1,5 +1,7 @@
 # frozen_string_literal: true
 
+require 'saml/kit/organization'
+
 module Saml
   module Kit
     # The Metadata object can be used to parse an XML string of metadata.
@@ -31,6 +33,9 @@ module Saml
       include Translatable
       include XmlParseable
       include XsdValidatable
+      extend Forwardable
+
+      def_delegator :organization, :organization_name, :organization_url
 
       validates_presence_of :metadata
       validate :must_contain_descriptor
@@ -54,16 +59,9 @@ module Saml
         search("/md:EntityDescriptor/md:#{name}/md:NameIDFormat").map(&:text)
       end
 
-      # Returns the Organization Name
-      def organization_name
-        xpath = '/md:EntityDescriptor/md:Organization/md:OrganizationName'
-        at_xpath(xpath).try(:text)
-      end
-
-      # Returns the Organization URL
-      def organization_url
-        xpath = '/md:EntityDescriptor/md:Organization/md:OrganizationURL'
-        at_xpath(xpath).try(:text)
+      def organization
+        @organization ||=
+          Organization.new(at_xpath('/md:EntityDescriptor/md:Organization'))
       end
 
       # Returns the Company
lib/saml/kit/organization.rb
@@ -0,0 +1,37 @@
+
+module Saml
+  module Kit
+    class Organization
+      include XmlParseable
+
+      attr_reader :content
+
+      def initialize(node)
+        @to_nokogiri = node
+        @content = node.to_s
+      end
+
+      # Returns the Organization Name
+      def name
+        at_xpath('./md:OrganizationName').try(:text)
+      end
+
+      # Returns the Organization URL
+      def url
+        at_xpath('./md:OrganizationURL').try(:text)
+      end
+
+      # @deprecated
+      def organization_name
+        Saml::Kit.deprecate('`organization_name` is deprecated. Use `organization.name`')
+        name
+      end
+
+      # @deprecated
+      def organization_url
+        Saml::Kit.deprecate('`organization_url` is deprecated. Use `organization.url`')
+        url
+      end
+    end
+  end
+end
lib/saml/kit.rb
@@ -42,6 +42,7 @@ require 'saml/kit/logout_response'
 require 'saml/kit/logout_request'
 require 'saml/kit/metadata'
 require 'saml/kit/null_assertion'
+require 'saml/kit/organization'
 require 'saml/kit/composite_metadata'
 require 'saml/kit/response'
 require 'saml/kit/identity_provider_metadata'