class Simple::OAuth2::Scopes

Scopes helper for scopes validation

Public Class Methods

new(access_scopes, scopes = []) click to toggle source

Helper class initializer

@param access_scopes [Array] scopes of AccessToken class @param scopes [Array<String, Symbol>] array, symbol, string of any object that responds to `to_a`

# File lib/simple_oauth2/scopes.rb, line 19
def initialize(access_scopes, scopes = [])
  @scopes = to_array(scopes)
  @access_scopes = to_array(access_scopes)
end
valid?(access_scopes, scopes) click to toggle source

Checks if requested scopes are valid

@param access_scopes [Array] scopes of AccessToken class @param scopes [Array<String, Symbol>] array, symbol, string of any object that responds to `to_a`

# File lib/simple_oauth2/scopes.rb, line 10
def self.valid?(access_scopes, scopes)
  new(access_scopes, scopes).valid?
end

Public Instance Methods

valid?() click to toggle source

Checks if requested scopes (passed and processed on initialization) are presented in the AccessToken

@return [Boolean] true if requested scopes are empty or present in access_scopes

# File lib/simple_oauth2/scopes.rb, line 28
def valid?
  @scopes.empty? || present_in_access_token?
end

Private Instance Methods

present_in_access_token?() click to toggle source

Checks if scopes present in access_scopes

@return [Boolean] true if requested scopes present in access_scopes

# File lib/simple_oauth2/scopes.rb, line 38
def present_in_access_token?
  Set.new(@access_scopes) >= Set.new(@scopes)
end
to_array(scopes) click to toggle source

Converts scopes set to the array

@param scopes [Array<String, Symbol>, to_a]

string, symbol, array or object that responds to `to_a`

@return [Array<String>] array of scopes

# File lib/simple_oauth2/scopes.rb, line 48
def to_array(scopes)
  collection = if scopes.is_a?(Array) || scopes.respond_to?(:to_a)
                 scopes.to_a
               elsif scopes.is_a?(String) || scopes.is_a?(Symbol)
                 scopes.split(',')
               end

  collection.map(&:to_s)
end