Class: SAML2::AttributeStatement

Inherits:
Base
  • Object
show all
Defined in:
lib/saml2/attribute.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#xml

Instance Method Summary collapse

Methods inherited from Base

#decrypt, from_xml, #inspect, load_object_array, load_string_array, lookup_qname, #to_s, #to_xml

Constructor Details

#initialize(attributes = []) ⇒ AttributeStatement

Returns a new instance of AttributeStatement.



165
166
167
168
# File 'lib/saml2/attribute.rb', line 165

def initialize(attributes = [])
  super()
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



163
164
165
# File 'lib/saml2/attribute.rb', line 163

def attributes
  @attributes
end

Instance Method Details

#build(builder) ⇒ Object



213
214
215
216
217
218
# File 'lib/saml2/attribute.rb', line 213

def build(builder)
  builder["saml"].AttributeStatement("xmlns:xs" => Namespaces::XS,
                                     "xmlns:xsi" => Namespaces::XSI) do |statement|
    @attributes.each { |attr| attr.build(statement) }
  end
end

#from_xml(node) ⇒ Object



170
171
172
173
174
175
# File 'lib/saml2/attribute.rb', line 170

def from_xml(node)
  super
  @attributes = node.xpath("saml:Attribute", Namespaces::ALL).map do |attr|
    Attribute.from_xml(attr)
  end
end

#to_h(name = :both) ⇒ Object

Convert the SAML2::AttributeStatement to a Hash

Repeated attributes become an array.

Parameters:

  • name (defaults to: :both)

    optional [:name, :friendly_name, :both] Which name field to use as keys to the hash. If :both is specified, attributes may be duplicated under both names.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/saml2/attribute.rb', line 185

def to_h(name = :both)
  return to_h(:friendly_name).merge(to_h(:name)) if name == :both

  result = {}
  attributes.each do |attribute|
    key = attribute.send(name)
    # fall back to name on missing friendly name;
    # no need for the opposite, because name is required
    key ||= attribute.name if name == :friendly_name

    prior_value = result[key]
    result[key] = if prior_value
                    value = Array.wrap(prior_value)
                    # repeated key; convert to array
                    if attribute.value.is_a?(Array)
                      # both values are arrays; concatenate them
                      value.concat(attribute.value)
                    else
                      value << attribute.value
                    end
                    value
                  else
                    attribute.value
                  end
  end
  result
end