class Samlsso::Attributes

Wraps all attributes and provides means to query them for single or multiple values.

For backwards compatibility Attributes#[] returns first value for the attribute. Turn off compatibility to make it return all values as an array:

Attributes.single_value_compatibility = false

Public Class Methods

new(attrs = {}) click to toggle source

Initialize Attributes collection, optionally taking a Hash of attribute names and values.

The attrs must be a Hash with attribute names as keys and arrays as values:

Attributes.new({
  'name' => ['value1', 'value2'],
  'mail' => ['value1'],
})
# File lib/samlsso/attributes.rb, line 32
def initialize(attrs = {})
  @attributes = attrs
end
single_value_compatibility() click to toggle source

Get current status of backwards compatibility mode.

# File lib/samlsso/attributes.rb, line 16
def self.single_value_compatibility
  @@single_value_compatibility
end
single_value_compatibility=(value) click to toggle source

Sets the backwards compatibility mode on/off.

# File lib/samlsso/attributes.rb, line 21
def self.single_value_compatibility=(value)
  @@single_value_compatibility = value
end

Public Instance Methods

==(other) click to toggle source

Make comparable to another Attributes collection based on attributes

Calls superclass method
# File lib/samlsso/attributes.rb, line 88
def ==(other)
  if other.is_a?(Attributes)
    all == other.all
  else
    super
  end
end
[](name) click to toggle source

By default returns first value for an attribute.

Depending on the single value compatibility status this returns first value

Attributes.single_value_compatibility = true # Default
response.attributes['mail']  # => 'user@example.com'

Or all values:

Attributes.single_value_compatibility = false
response.attributes['mail']  # => ['user@example.com','user@example.net']
# File lib/samlsso/attributes.rb, line 66
def [](name)
  self.class.single_value_compatibility ? single(canonize_name(name)) : multi(canonize_name(name))
end
[]=(name, values)
Alias for: set
add(name, values = []) click to toggle source

Add new attribute or new value(s) to an existing attribute

# File lib/samlsso/attributes.rb, line 82
def add(name, values = [])
  attributes[canonize_name(name)] ||= []
  attributes[canonize_name(name)] += Array(values)
end
all() click to toggle source

Return all attributes as an array

# File lib/samlsso/attributes.rb, line 71
def all
  attributes
end
each() { |name, values| ... } click to toggle source

Iterate over all attributes

# File lib/samlsso/attributes.rb, line 38
def each
  attributes.each{|name, values| yield name, values}
end
include?(name) click to toggle source

Test attribute presence by name

# File lib/samlsso/attributes.rb, line 43
def include?(name)
  attributes.has_key?(canonize_name(name))
end
multi(name) click to toggle source

Return all values for an attribute

# File lib/samlsso/attributes.rb, line 53
def multi(name)
  attributes[canonize_name(name)]
end
set(name, values) click to toggle source

Set values for an attribute, overwriting all existing values

# File lib/samlsso/attributes.rb, line 76
def set(name, values)
  attributes[canonize_name(name)] = values
end
Also aliased as: []=
single(name) click to toggle source

Return first value for an attribute

# File lib/samlsso/attributes.rb, line 48
def single(name)
  attributes[canonize_name(name)].first if include?(name)
end

Protected Instance Methods

attributes() click to toggle source
# File lib/samlsso/attributes.rb, line 103
def attributes
  @attributes
end
canonize_name(name) click to toggle source

stringifies all names so both 'email' and :email return the same result

# File lib/samlsso/attributes.rb, line 99
def canonize_name(name)
  name.to_s
end