Sha256: ff8d6b15e4b55a1c459df85056b20813c8ec22fde50d98214cb8d3d331a36932

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

require 'saml2/namespaces'

module SAML2
  class Organization
    def self.from_xml(node)
      return nil unless node

      new(nodes_to_hash(node.xpath('md:OrganizationName', Namespaces::ALL)),
        nodes_to_hash(node.xpath('md:OrganizationDisplayName', Namespaces::ALL)),
        nodes_to_hash(node.xpath('md:OrganizationURL', Namespaces::ALL)))
    end

    def initialize(name, display_name, url)
      if !name.is_a?(Hash)
        name = { nil => name}
      end
      if !display_name.is_a?(Hash)
        display_name = { nil => display_name }
      end
      if !url.is_a?(Hash)
        url = { nil => url }
      end

      @name, @display_name, @url = name, display_name, url
    end

    def name(lang = nil)
      self.class.localized_name(@name, lang)
    end

    def display_name(lang = nil)
      self.class.localized_name(@display_name, lang)
    end

    def url(lang = nil)
      self.class.localized_name(@url, lang)
    end

    def build(builder)
      builder['md'].Organization do |organization|
        self.class.build(organization, @name, 'OrganizationName')
        self.class.build(organization, @display_name, 'OrganizationDisplayName')
        self.class.build(organization, @url, 'OrganizationURL')
      end
    end

    private

    def self.build(builder, hash, element)
      hash.each do |lang, value|
        builder['md'].__send__(element, value, 'xml:lang' => lang)
      end
    end

    def self.nodes_to_hash(nodes)
      hash = {}
      nodes.each do |node|
        hash[node['xml:lang'].to_sym] = node.content && node.content.strip
      end
      hash
    end

    def self.localized_name(hash, lang)
      case lang
        when :all
          hash
        when nil
          !hash.empty? && hash.first.last
        else
          hash[lang.to_sym]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
saml2-1.1.5 lib/saml2/organization.rb
saml2-1.1.4 lib/saml2/organization.rb
saml2-1.1.3 lib/saml2/organization.rb
saml2-1.1.2 lib/saml2/organization.rb
saml2-1.1.1 lib/saml2/organization.rb
saml2-1.1.0 lib/saml2/organization.rb
saml2-1.0.10 lib/saml2/organization.rb
saml2-1.0.9 lib/saml2/organization.rb