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.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



160
161
162
# File 'lib/saml2/attribute.rb', line 160

def attributes
  @attributes
end

Instance Method Details

#build(builder) ⇒ Object



209
210
211
212
213
214
# File 'lib/saml2/attribute.rb', line 209

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



166
167
168
169
170
171
# File 'lib/saml2/attribute.rb', line 166

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.



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

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