Class: OpenID::AX::KeyValueMessage

Inherits:
AXMessage show all
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

FetchResponse, StoreRequest

Constant Summary

Constants inherited from AXMessage

AXMessage::NS_URI

Instance Attribute Summary collapse

Attributes inherited from AXMessage

#mode, #ns_alias, #ns_uri

Instance Method Summary collapse

Methods inherited from Extension

#get_extension_args, #to_message

Constructor Details

#initializeKeyValueMessage

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

#dataObject (readonly)

Returns the value of attribute data.



274
275
276
# File 'lib/openid/extensions/ax.rb', line 274

def data
  @data
end

Instance Method Details

#[](type_uri) ⇒ Object

retrieve the list of values for this attribute



385
386
387
# File 'lib/openid/extensions/ax.rb', line 385

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{|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_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



390
391
392
# File 'lib/openid/extensions/ax.rb', line 390

def count(type_uri)
  @data[type_uri].size
end

#get(type_uri) ⇒ Object

retrieve the list of values for this attribute



380
381
382
# File 'lib/openid/extensions/ax.rb', line 380

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.



369
370
371
372
373
374
375
376
377
# 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_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
# 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_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