Class: OpenID::NamespaceMap

Inherits:
Object
  • Object
show all
Defined in:
lib/openid/message.rb

Overview

Maintains a bidirectional map between namespace URIs and aliases.

Instance Method Summary collapse

Constructor Details

#initializeNamespaceMap

Returns a new instance of NamespaceMap.



447
448
449
450
# File 'lib/openid/message.rb', line 447

def initialize
  @alias_to_namespace = {}
  @namespace_to_alias = {}
end

Instance Method Details

#add(namespace_uri) ⇒ Object

Add this namespace URI to the mapping, without caring what alias it ends up with.

Raises:

  • (StandardError)


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/openid/message.rb', line 495

def add(namespace_uri)
  # see if this namepace is already mapped to an alias
  _alias = @namespace_to_alias[namespace_uri]
  return _alias if _alias

  # Fall back to generating a numberical alias
  i = 0
  while true
    _alias = 'ext' + i.to_s
    begin
      add_alias(namespace_uri, _alias)
    rescue IndexError
      i += 1
    else
      return _alias
    end
  end

  raise StandardError, 'Unreachable'
end

#add_alias(namespace_uri, desired_alias) ⇒ Object

Add an alias from this namespace URI to the alias.



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/openid/message.rb', line 461

def add_alias(namespace_uri, desired_alias)
  # Check that desired_alias is not an openid protocol field as
  # per the spec.
  Util.assert(!OPENID_PROTOCOL_FIELDS.include?(desired_alias),
         "#{desired_alias} is not an allowed namespace alias")

  # check that there is not a namespace already defined for the
  # desired alias
  current_namespace_uri = @alias_to_namespace.fetch(desired_alias, nil)
  if current_namespace_uri and current_namespace_uri != namespace_uri
    raise IndexError, "Cannot map #{namespace_uri} to alias #{desired_alias}. #{current_namespace_uri} is already mapped to alias #{desired_alias}"
  end

  # Check that desired_alias does not contain a period as per the
  # spec.
  if desired_alias.is_a?(String)
      Util.assert(desired_alias.index('.').nil?,
             "#{desired_alias} must not contain a dot")
  end

  # check that there is not already a (different) alias for this
  # namespace URI.
  _alias = @namespace_to_alias[namespace_uri]
  if _alias and _alias != desired_alias
    raise IndexError, "Cannot map #{namespace_uri} to alias #{desired_alias}. It is already mapped to alias #{_alias}"
  end

  @alias_to_namespace[desired_alias] = namespace_uri
  @namespace_to_alias[namespace_uri] = desired_alias
  return desired_alias
end

#aliasesObject



529
530
531
532
# File 'lib/openid/message.rb', line 529

def aliases
  # Return an iterator over the aliases
  return @alias_to_namespace.keys()
end

#eachObject



520
521
522
# File 'lib/openid/message.rb', line 520

def each
  @namespace_to_alias.each {|k,v| yield k,v}
end

#get_alias(namespace_uri) ⇒ Object



452
453
454
# File 'lib/openid/message.rb', line 452

def get_alias(namespace_uri)
  @namespace_to_alias[namespace_uri]
end

#get_namespace_uri(namespace_alias) ⇒ Object



456
457
458
# File 'lib/openid/message.rb', line 456

def get_namespace_uri(namespace_alias)
  @alias_to_namespace[namespace_alias]
end

#member?(namespace_uri) ⇒ Boolean

Returns:

  • (Boolean)


516
517
518
# File 'lib/openid/message.rb', line 516

def member?(namespace_uri)
  @namespace_to_alias.has_key?(namespace_uri)
end

#namespace_urisObject



524
525
526
527
# File 'lib/openid/message.rb', line 524

def namespace_uris
  # Return an iterator over the namespace URIs
  return @namespace_to_alias.keys()
end