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.



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

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)


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

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.



471
472
473
474
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
507
508
# File 'lib/openid/message.rb', line 471

def add_alias(namespace_uri, desired_alias, implicit = false)
  # Check that desired_alias is not an openid protocol field as
  # per the spec.
  Util.truthy_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.truthy_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
  desired_alias
end

#aliasesObject



550
551
552
553
# File 'lib/openid/message.rb', line 550

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

#each(&block) ⇒ Object



537
538
539
# File 'lib/openid/message.rb', line 537

def each(&block)
  @namespace_to_alias.each(&block)
end

#get_alias(namespace_uri) ⇒ Object



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

def get_alias(namespace_uri)
  @namespace_to_alias[namespace_uri]
end

#get_namespace_uri(namespace_alias) ⇒ Object



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

def get_namespace_uri(namespace_alias)
  @alias_to_namespace[namespace_alias]
end

#implicit?(namespace_uri) ⇒ Boolean

Returns:

  • (Boolean)


546
547
548
# File 'lib/openid/message.rb', line 546

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

#member?(namespace_uri) ⇒ Boolean

Returns:

  • (Boolean)


533
534
535
# File 'lib/openid/message.rb', line 533

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

#namespace_urisObject



541
542
543
544
# File 'lib/openid/message.rb', line 541

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