class OpenID::AX::KeyValueMessage
Abstract class that implements a message that has attribute keys and values. It contains the common code between fetch_response and store_request.
Attributes
Public Class Methods
# File lib/openid/extensions/ax.rb, line 275 def initialize super() @mode = nil @data = Hash.new { |hash, key| hash[key] = [] } end
Public Instance Methods
retrieve the list of values for this attribute
# File lib/openid/extensions/ax.rb, line 385 def [](type_uri) @data[type_uri] end
Get the extension arguments for the key/value pairs contained in this message.
# File lib/openid/extensions/ax.rb, line 297 def _get_extension_kv_args(aliases = nil) aliases = NamespaceMap.new if aliases.nil? ax_args = new_args @data.each{|type_uri, values| name = aliases.add(type_uri) ax_args['type.'+name] = type_uri if values.size > 1 ax_args['count.'+name] = values.size.to_s values.each_with_index{|value, i| key = "value.#{name}.#{i+1}" ax_args[key] = value } # for attributes with only a single value, use a # nice shortcut to only show the value w/o the count else values.each do |value| key = "value.#{name}" ax_args[key] = value end end } return ax_args end
Add a single value for the given attribute type to the message. If there are already values specified for this type, this value will be sent in addition to the values already specified.
# File lib/openid/extensions/ax.rb, line 285 def add_value(type_uri, value) @data[type_uri] = @data[type_uri] << value end
get the number of responses for this attribute
# File lib/openid/extensions/ax.rb, line 390 def count(type_uri) @data[type_uri].size end
retrieve the list of values for this attribute
# File lib/openid/extensions/ax.rb, line 380 def get(type_uri) @data[type_uri] end
Get a single value for an attribute. If no value was sent for this attribute, use the supplied default. If there is more than one value for this attribute, this method will fail.
# File lib/openid/extensions/ax.rb, line 369 def get_single(type_uri, default = nil) values = @data[type_uri] return default if values.empty? if values.size != 1 raise Error, "More than one value present for #{type_uri.inspect}" else return values[0] end end
Parse attribute exchange key/value arguments into this object.
# File lib/openid/extensions/ax.rb, line 326 def parse_extension_args(ax_args) check_mode(ax_args) aliases = NamespaceMap.new ax_args.each{|k, v| if k.index('type.') == 0 type_uri = v name = k[5..-1] AX.check_alias(name) aliases.add_alias(type_uri,name) end } aliases.each{|type_uri, name| count_s = ax_args['count.'+name] count = count_s.to_i if count_s.nil? value = ax_args['value.'+name] if value.nil? raise IndexError, "Missing #{'value.'+name} in FetchResponse" elsif value.empty? values = [] else values = [value] end elsif count_s.to_i == 0 values = [] else values = (1..count).inject([]){|l,i| key = "value.#{name}.#{i}" v = ax_args[key] raise IndexError, "Missing #{key} in FetchResponse" if v.nil? l << v } end @data[type_uri] = values } end
Set the values for the given attribute type. This replaces any values that have already been set for this attribute.
# File lib/openid/extensions/ax.rb, line 291 def set_values(type_uri, values) @data[type_uri] = values end