Sha256: dbc703b8b966af6919ba997e037d403a5a6b7fa054ed14c204504059984c9181

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

module Doorkeeper
  class InvalidSyntax < StandardError; end
  class DoorkeeperFor
    def initialize(options)
      options ||= {}
      fail InvalidSyntax unless options.is_a? Hash
      @filter_options = {}

      options.each do |k, v|
        self.send(k, v)
      end
    end

    # TODO: move this to Token class
    def validate_token(token)
      return false unless token
      token.accessible? && validate_token_scopes(token)
    end

    def filter_options
      @filter_options
    end

    private

    def scopes(scopes)
      @scopes = scopes.map(&:to_s)
    end

    def if(if_block)
      @filter_options[:if] = if_block
    end

    def unless(unless_block)
      @filter_options[:unless] = unless_block
    end

    # TODO: move this to Token class
    def validate_token_scopes(token)
      return true if @scopes.blank?
      token.scopes.any? { |scope| @scopes.include? scope }
    end
  end

  class AllDoorkeeperFor < DoorkeeperFor
    private

    def except(actions)
      @filter_options[:except] = actions
    end
  end

  class SelectedDoorkeeperFor < DoorkeeperFor
    def initialize(*args)
      options = args.pop if args.last.is_a? Hash
      super(options)
      only(args)
    end

    private

    def only(actions)
      @filter_options[:only] = actions
    end
  end

  class DoorkeeperForBuilder
    def self.create_doorkeeper_for(*args)
      case args.first
      when :all
        AllDoorkeeperFor.new(args[1] || {})
      when Hash, nil
        fail InvalidSyntax
      else
        SelectedDoorkeeperFor.new(*args)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
doorkeeper-1.3.1 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-1.3.0 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-1.2.0 lib/doorkeeper/doorkeeper_for.rb