Class: OpenID::AX::KeyValueMessage
- Defined in:
- lib/openid/extensions/ax.rb
Overview
Abstract class that implements a message that has attribute keys and values. It contains the common code between fetch_response and store_request.
Direct Known Subclasses
Constant Summary
Constants inherited from AXMessage
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Attributes inherited from AXMessage
Instance Method Summary collapse
-
#[](type_uri) ⇒ Object
retrieve the list of values for this attribute.
-
#_get_extension_kv_args(aliases = nil) ⇒ Object
Get the extension arguments for the key/value pairs contained in this message.
-
#add_value(type_uri, value) ⇒ Object
Add a single value for the given attribute type to the message.
-
#count(type_uri) ⇒ Object
get the number of responses for this attribute.
-
#get(type_uri) ⇒ Object
retrieve the list of values for this attribute.
-
#get_single(type_uri, default = nil) ⇒ Object
Get a single value for an attribute.
-
#initialize ⇒ KeyValueMessage
constructor
A new instance of KeyValueMessage.
-
#parse_extension_args(ax_args) ⇒ Object
Parse attribute exchange key/value arguments into this object.
-
#set_values(type_uri, values) ⇒ Object
Set the values for the given attribute type.
Methods inherited from Extension
#get_extension_args, #to_message
Constructor Details
#initialize ⇒ KeyValueMessage
Returns a new instance of KeyValueMessage.
275 276 277 278 279 |
# File 'lib/openid/extensions/ax.rb', line 275 def initialize super @mode = nil @data = Hash.new { |hash, key| hash[key] = [] } end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
273 274 275 |
# File 'lib/openid/extensions/ax.rb', line 273 def data @data end |
Instance Method Details
#[](type_uri) ⇒ Object
retrieve the list of values for this attribute
384 385 386 |
# File 'lib/openid/extensions/ax.rb', line 384 def [](type_uri) @data[type_uri] end |
#_get_extension_kv_args(aliases = nil) ⇒ Object
Get the extension arguments for the key/value pairs contained in this message.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# 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 do |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 do |value, i| key = "value.#{name}.#{i + 1}" ax_args[key] = value end # 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 end ax_args end |
#add_value(type_uri, value) ⇒ Object
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.
285 286 287 |
# File 'lib/openid/extensions/ax.rb', line 285 def add_value(type_uri, value) @data[type_uri] = @data[type_uri] << value end |
#count(type_uri) ⇒ Object
get the number of responses for this attribute
389 390 391 |
# File 'lib/openid/extensions/ax.rb', line 389 def count(type_uri) @data[type_uri].size end |
#get(type_uri) ⇒ Object
retrieve the list of values for this attribute
379 380 381 |
# File 'lib/openid/extensions/ax.rb', line 379 def get(type_uri) @data[type_uri] end |
#get_single(type_uri, default = nil) ⇒ Object
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.
370 371 372 373 374 375 376 |
# File 'lib/openid/extensions/ax.rb', line 370 def get_single(type_uri, default = nil) values = @data[type_uri] return default if values.empty? raise Error, "More than one value present for #{type_uri.inspect}" if values.size != 1 values[0] end |
#parse_extension_args(ax_args) ⇒ Object
Parse attribute exchange key/value arguments into this object.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/openid/extensions/ax.rb', line 326 def parse_extension_args(ax_args) check_mode(ax_args) aliases = NamespaceMap.new ax_args.each do |k, v| next unless k.index("type.") == 0 type_uri = v name = k[5..-1] AX.check_alias(name) aliases.add_alias(type_uri, name) end aliases.each do |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([]) do |l, i| key = "value.#{name}.#{i}" v = ax_args[key] raise IndexError, "Missing #{key} in FetchResponse" if v.nil? l << v end end @data[type_uri] = values end end |
#set_values(type_uri, values) ⇒ Object
Set the values for the given attribute type. This replaces any values that have already been set for this attribute.
291 292 293 |
# File 'lib/openid/extensions/ax.rb', line 291 def set_values(type_uri, values) @data[type_uri] = values end |