Class: Gruf::Authentication::Strategies

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/authentication/strategies.rb

Overview

Provides a modifiable repository of strategies for authentication

Defined Under Namespace

Classes: StrategyDescendantError

Class Method Summary collapse

Class Method Details

.[](label) ⇒ Object

Return a strategy via a hash accessor syntax



49
50
51
# File 'lib/gruf/authentication/strategies.rb', line 49

def [](label)
  _strategies[label.to_sym]
end

.add(name, strategy = nil, &block) ⇒ Class

Add an authentication strategy, either through a class or a block

Parameters:

  • name (String)
  • strategy (Class|NilClass) (defaults to: nil)

Returns:

  • (Class)

Raises:

  • (NoMethodError)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gruf/authentication/strategies.rb', line 33

def add(name, strategy = nil, &block)
  base = Gruf::Authentication::Base
  strategy ||= Class.new(base)
  strategy.class_eval(&block) if block_given?

  # all strategies require the valid? method
  raise NoMethodError unless strategy.method_defined?(:valid?)

  raise StrategyDescendantError, "Strategies must descend from #{base}" unless strategy.ancestors.include?(base)

  _strategies[name.to_sym] = strategy
end

.any?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/gruf/authentication/strategies.rb', line 72

def any?
  to_h.keys.count > 0
end

.clearHash

Returns:

  • (Hash)


79
80
81
# File 'lib/gruf/authentication/strategies.rb', line 79

def clear
  @strategies = {}
end

.eachObject

Iterate over each



56
57
58
59
60
# File 'lib/gruf/authentication/strategies.rb', line 56

def each
  _strategies.each do |s|
    yield s
  end
end

.to_hHash<Class>

Returns:

  • (Hash<Class>)


65
66
67
# File 'lib/gruf/authentication/strategies.rb', line 65

def to_h
  _strategies
end