Sha256: fc295e2e7db2219a11d0f210cdc10535c957ca072095136e181a5599d6debd78

Contents?: true

Size: 877 Bytes

Versions: 2

Compression:

Stored size: 877 Bytes

Contents

module AuthStrategist
  class Authorize
    attr_reader :strategy

    def self.call(options = {})
      new(options).call
    end

    def initialize(options = {})
      @strategy = choose_strategy(options)
    end

    def call
      strategy.authorize!
    end

    private

    def choose_strategy(options = {})
      strategy_name = options.delete(:strategy)
      raise(strategy_name_blank) if strategy_name.nil?

      strategy_class = strategies[strategy_name]
      raise(strategy_not_found(strategy_name)) if strategy_class.nil?

      strategy_class.new(options)
    end

    def strategies
      AuthStrategist.strategies.to_h
    end

    def strategy_not_found(strategy_name)
      StandardError.new("Strategy :#{strategy_name} was not found.")
    end

    def strategy_name_blank
      StandardError.new(':strategy option must not be blank.')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
auth_strategist-0.6.0 lib/auth_strategist/authorize.rb
auth_strategist-0.5.0 lib/auth_strategist/authorize.rb