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.



268
269
270
271
272
273
# File 'lib/openid/extensions/ax.rb', line 268

def initialize
  super()
  @mode = nil
  @data = {}
  @data.default = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



267
268
269
# File 'lib/openid/extensions/ax.rb', line 267

def data
  @data
end

Instance Method Details

#[](type_uri) ⇒ Object

retrieve the list of values for this attribute



370
371
372
# File 'lib/openid/extensions/ax.rb', line 370

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.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/openid/extensions/ax.rb', line 291

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
    ax_args['count.'+name] = values.size.to_s

    values.each_with_index{|value, i|
      key = "value.#{name}.#{i+1}"
      ax_args[key] = value
    }
  }
  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.



279
280
281
# File 'lib/openid/extensions/ax.rb', line 279

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



375
376
377
# File 'lib/openid/extensions/ax.rb', line 375

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

#get(type_uri) ⇒ Object

retrieve the list of values for this attribute



365
366
367
# File 'lib/openid/extensions/ax.rb', line 365

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.



354
355
356
357
358
359
360
361
362
# File 'lib/openid/extensions/ax.rb', line 354

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.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/openid/extensions/ax.rb', line 311

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.



285
286
287
# File 'lib/openid/extensions/ax.rb', line 285

def set_values(type_uri, values)
  @data[type_uri] = values
end