Sha256: 4dbdc3fbd81c0ac7b230d8c0cc80500c10cddac063b0d6f85cc1ae7b36eaf43b

Contents?: true

Size: 1.54 KB

Versions: 16

Compression:

Stored size: 1.54 KB

Contents

module Doorkeeper
  class InvalidSyntax < StandardError; end
  class DoorkeeperFor
    def initialize(options)
      options ||= {}
      raise 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? and validate_token_scopes(token)
    end

    def filter_options
      @filter_options
    end

    private
    def scopes(scopes)
      @scopes = scopes
    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
        raise InvalidSyntax
      else
        SelectedDoorkeeperFor.new(*args)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
doorkeeper-0.7.4 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.7.3 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.7.2 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.7.1 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.7.0 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.7 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.6 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.5 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.4 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.3 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.2 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.1 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.0 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.6.0.rc1 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.5.0 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-0.5.0.rc1 lib/doorkeeper/doorkeeper_for.rb