Class: OneLogin::RubySaml::Attributes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/onelogin/ruby-saml/attributes.rb

Overview

SAML2 Attributes. Parse the Attributes from the AttributeStatement of the SAML Response.

Constant Summary collapse

@@single_value_compatibility =

By default Attributes#[] is backwards compatible and returns only the first value for the attribute Setting this to ‘false` returns all values for an attribute

true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Attributes



35
36
37
# File 'lib/onelogin/ruby-saml/attributes.rb', line 35

def initialize(attrs = {})
  @attributes = attrs
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/onelogin/ruby-saml/attributes.rb', line 9

def attributes
  @attributes
end

Class Method Details

.single_value_compatibilityBoolean



18
19
20
# File 'lib/onelogin/ruby-saml/attributes.rb', line 18

def self.single_value_compatibility
  @@single_value_compatibility
end

.single_value_compatibility=(value) ⇒ Object

Sets the backwards compatibility mode on/off.



25
26
27
# File 'lib/onelogin/ruby-saml/attributes.rb', line 25

def self.single_value_compatibility=(value)
  @@single_value_compatibility = value
end

Instance Method Details

#==(other) ⇒ Boolean

Make comparable to another Attributes collection based on attributes



108
109
110
111
112
113
114
# File 'lib/onelogin/ruby-saml/attributes.rb', line 108

def ==(other)
  if other.is_a?(Attributes)
    all == other.all
  else
    super
  end
end

#[](name) ⇒ String|Array

Retrieve attribute value(s)



78
79
80
# 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

#add(name, values = []) ⇒ Object



99
100
101
102
# File 'lib/onelogin/ruby-saml/attributes.rb', line 99

def add(name, values = [])
  attributes[canonize_name(name)] ||= []
  attributes[canonize_name(name)] += Array(values)
end

#allHash



84
85
86
# File 'lib/onelogin/ruby-saml/attributes.rb', line 84

def all
  attributes
end

#eachObject

Iterate over all attributes



42
43
44
# File 'lib/onelogin/ruby-saml/attributes.rb', line 42

def each
  attributes.each{|name, values| yield name, values}
end

#fetch(name) ⇒ String|Array

Fetch attribute value using name or regex



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/onelogin/ruby-saml/attributes.rb', line 124

def fetch(name)
  attributes.each_key do |attribute_key|
    if name.is_a?(Regexp)
      if name.respond_to? :match?
        return self[attribute_key] if name.match?(attribute_key)
      else 
        return self[attribute_key] if name.match(attribute_key)
      end
    elsif canonize_name(name) == canonize_name(attribute_key)
      return self[attribute_key]
    end
  end
  nil
end

#include?(name) ⇒ Boolean

Test attribute presence by name



50
51
52
# File 'lib/onelogin/ruby-saml/attributes.rb', line 50

def include?(name)
  attributes.has_key?(canonize_name(name))
end

#multi(name) ⇒ Array

Return all values for an attribute



66
67
68
# File 'lib/onelogin/ruby-saml/attributes.rb', line 66

def multi(name)
  attributes[canonize_name(name)]
end

#set(name, values) ⇒ Object Also known as: []=



91
92
93
# File 'lib/onelogin/ruby-saml/attributes.rb', line 91

def set(name, values)
  attributes[canonize_name(name)] = values
end

#single(name) ⇒ String

Return first value for an attribute



58
59
60
# File 'lib/onelogin/ruby-saml/attributes.rb', line 58

def single(name)
  attributes[canonize_name(name)].first if include?(name)
end