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.



460
461
462
463
464
# File 'lib/openid/message.rb', line 460

def initialize
  @alias_to_namespace = {}
  @namespace_to_alias = {}
  @implicit_namespaces = []
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)


510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/openid/message.rb', line 510

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, implicit = false) ⇒ Object

Add an alias from this namespace URI to the alias.



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/openid/message.rb', line 475

def add_alias(namespace_uri, desired_alias, implicit=false)
  # 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
  @implicit_namespaces << namespace_uri if implicit
  return desired_alias
end

#aliasesObject



548
549
550
551
# File 'lib/openid/message.rb', line 548

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

#eachObject



535
536
537
# File 'lib/openid/message.rb', line 535

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

#get_alias(namespace_uri) ⇒ Object



466
467
468
# File 'lib/openid/message.rb', line 466

def get_alias(namespace_uri)
  @namespace_to_alias[namespace_uri]
end

#get_namespace_uri(namespace_alias) ⇒ Object



470
471
472
# File 'lib/openid/message.rb', line 470

def get_namespace_uri(namespace_alias)
  @alias_to_namespace[namespace_alias]
end

#implicit?(namespace_uri) ⇒ Boolean

Returns:

  • (Boolean)


544
545
546
# File 'lib/openid/message.rb', line 544

def implicit?(namespace_uri)
  return @implicit_namespaces.member?(namespace_uri)
end

#member?(namespace_uri) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#namespace_urisObject



539
540
541
542
# File 'lib/openid/message.rb', line 539

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