class OneLogin::RubySaml::Attributes
SAML2 Attributes. Parse the Attributes from the AttributeStatement of the SAML Response.
Attributes
Public Class Methods
@param attrs [Hash] The attrs
must be a Hash with attribute
names as keys and *arrays* as values:
Attributes.new({ 'name' => ['value1', 'value2'], 'mail' => ['value1'], })
# File lib/onelogin/ruby-saml/attributes.rb, line 35 def initialize(attrs = {}) @attributes = attrs end
@return [Boolean] Get current status of backwards compatibility mode.
# File lib/onelogin/ruby-saml/attributes.rb, line 18 def self.single_value_compatibility @@single_value_compatibility end
Sets the backwards compatibility mode on/off. @param value [Boolean]
# File lib/onelogin/ruby-saml/attributes.rb, line 25 def self.single_value_compatibility=(value) @@single_value_compatibility = value end
Public Instance Methods
Make comparable to another Attributes collection based on attributes @param other [Attributes] An Attributes object to compare with @return [Boolean] True if are contains the same attributes and values
# File lib/onelogin/ruby-saml/attributes.rb, line 108 def ==(other) if other.is_a?(Attributes) all == other.all else super end end
Retrieve attribute value(s) @param name [String] The attribute name @return [String|Array] Depending on the single value compatibility status this returns:
- First value if single_value_compatibility = true response.attributes['mail'] # => 'user@example.com' - All values if single_value_compatibility = false response.attributes['mail'] # => ['user@example.com','user@example.net']
# File lib/onelogin/ruby-saml/attributes.rb, line 78 def [](name) self.class.single_value_compatibility ? single(canonize_name(name)) : multi(canonize_name(name)) end
@param name [String] The attribute name @param values [Array] The values
# File lib/onelogin/ruby-saml/attributes.rb, line 99 def add(name, values = []) attributes[canonize_name(name)] ||= [] attributes[canonize_name(name)] += Array(values) end
@return [Array] Return all attributes as an array
# File lib/onelogin/ruby-saml/attributes.rb, line 84 def all attributes end
Iterate over all attributes
# File lib/onelogin/ruby-saml/attributes.rb, line 42 def each attributes.each{|name, values| yield name, values} end
Test attribute presence by name @param name [String] The attribute name to be checked
# File lib/onelogin/ruby-saml/attributes.rb, line 50 def include?(name) attributes.has_key?(canonize_name(name)) end
Return all values for an attribute @param name [String] The attribute name @return [Array] Values of the attribute
# File lib/onelogin/ruby-saml/attributes.rb, line 66 def multi(name) attributes[canonize_name(name)] end
@param name [String] The attribute name @param values [Array] The values
# File lib/onelogin/ruby-saml/attributes.rb, line 91 def set(name, values) attributes[canonize_name(name)] = values end
Return first value for an attribute @param name [String] The attribute name @return [String] The value (First occurrence)
# File lib/onelogin/ruby-saml/attributes.rb, line 58 def single(name) attributes[canonize_name(name)].first if include?(name) end
Protected Instance Methods
stringifies all names so both 'email' and :email return the same result @param name [String] The attribute name @return [String] stringified name
# File lib/onelogin/ruby-saml/attributes.rb, line 122 def canonize_name(name) name.to_s end