Class: SAML2::Attribute

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

Direct Known Subclasses

X500, RequestedAttribute

Defined Under Namespace

Modules: NameFormats Classes: X500

Instance Attribute Summary collapse

Attributes inherited from Base

#xml

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(name = nil, value = nil, friendly_name = nil, name_format = nil) ⇒ Attribute

Create a new generic Attribute



86
87
88
89
90
91
92
# File 'lib/saml2/attribute.rb', line 86

def initialize(name = nil, value = nil, friendly_name = nil, name_format = nil)
  super()
  @name = name
  @value = value
  @friendly_name = friendly_name
  @name_format = name_format
end

Instance Attribute Details

#friendly_nameString?



76
77
78
# File 'lib/saml2/attribute.rb', line 76

def friendly_name
  @friendly_name
end

#nameString



74
75
76
# File 'lib/saml2/attribute.rb', line 74

def name
  @name
end

#name_formatString?



76
77
78
# File 'lib/saml2/attribute.rb', line 76

def name_format
  @name_format
end

#valueObject?



78
79
80
# File 'lib/saml2/attribute.rb', line 78

def value
  @value
end

Class Method Details

.create(name, value = nil) ⇒ Attribute

Create an appropriate object to represent an attribute.

This will create the most appropriate object (i.e. an X500 if possible) to represent this attribute, based on its name.



39
40
41
# File 'lib/saml2/attribute.rb', line 39

def create(name, value = nil)
  (class_for(name) || self).new(name, value)
end

.element'Attribute'

The XML element that this attribute class serializes as.



51
52
53
# File 'lib/saml2/attribute.rb', line 51

def element
  "Attribute"
end

.from_xml(node) ⇒ Base?

Create an appropriate object to represent the given XML element.



20
21
22
23
24
25
26
27
# File 'lib/saml2/attribute.rb', line 20

def from_xml(node)
  # pass through for subclasses
  return super unless self == Attribute

  # look for an appropriate subclass
  klass = class_for(node)
  klass ? klass.from_xml(node) : super
end

.namespace'saml'

The XML namespace that this attribute class serializes as.



45
46
47
# File 'lib/saml2/attribute.rb', line 45

def namespace
  "saml"
end

Instance Method Details

#build(builder) ⇒ void

This method returns an undefined value.

Serialize this object to XML, as part of a larger document



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/saml2/attribute.rb', line 95

def build(builder)
  builder[self.class.namespace].__send__(self.class.element, "Name" => name) do |attribute|
    attribute.parent["FriendlyName"] = friendly_name if friendly_name
    attribute.parent["NameFormat"] = name_format if name_format
    Array.wrap(value).each do |value|
      xsi_type, val = convert_to_xsi(value)
      attribute["saml"].AttributeValue(val) do |attribute_value|
        attribute_value.parent["xsi:type"] = xsi_type if xsi_type
      end
    end
  end
end

#from_xml(node) ⇒ void

This method returns an undefined value.

Parse an XML element into this object.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/saml2/attribute.rb', line 109

def from_xml(node)
  super
  @name = node["Name"]
  @friendly_name = node["FriendlyName"]
  @name_format = node["NameFormat"]
  values = node.xpath("saml:AttributeValue", Namespaces::ALL).map do |value|
    convert_from_xsi(value.attribute_with_ns("type", Namespaces::XSI), value.content && value.content.strip)
  end
  @value = case values.length
           when 0 then nil
           when 1 then values.first
           else; values
           end
end