module ApiWarden

Constants

SCOPES
VERSION

Public Class Methods

configure() { |self| ... } click to toggle source

Configuration for ApiWarden, use like:

ApiWarden.configure do |config|
  config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/0' }
end
# File lib/api_warden.rb, line 20
def self.configure
  yield self
end
find_scope(name) click to toggle source
# File lib/api_warden.rb, line 55
def self.find_scope(name)
  name = validate_scope_name(name)
  SCOPES[name]
end
friendly_token(length = 20) click to toggle source

Generate a friendly string randomly to be used as token. By default, length is 20 characters.

# File lib/api_warden.rb, line 89
def self.friendly_token(length = 20)
  # To calculate real characters, we must perform this operation.
  # See SecureRandom.urlsafe_base64
  rlength = (length * 3) / 4
  SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
end
redis() { |conn| ... } click to toggle source
# File lib/api_warden.rb, line 60
def self.redis
  raise ArgumentError, 'requires a block' unless block_given?
  redis_pool.with do |conn|
    retryable = true
    begin
      yield conn
    rescue Redis::CommandError => ex
      # Failover can cause the server to become a slave, need
      # to disconnect and reopen the socket to get back to the master.
      (conn.disconnect!; retryable = false; retry) if retryable && ex.message =~ /READONLY/
      raise
    end
  end
end
redis=(hash) click to toggle source
# File lib/api_warden.rb, line 79
def self.redis=(hash)
  @redis = if hash.is_a?(ConnectionPool)
    hash
  elsif hash
    RedisConnection.create(hash)
  end
end
redis_pool() click to toggle source
# File lib/api_warden.rb, line 75
def self.redis_pool
  @redis ||= RedisConnection.create
end
remove_ward_by(scope) click to toggle source

@return [Boolean] true if removed successfully, false otherwise.

# File lib/api_warden.rb, line 45
def self.remove_ward_by(scope)
  if scope = find_scope(scope)
    Helpers.remove_helpers(scope)
    SCOPES.delete(scope.name)
    true
  else
    false
  end
end
ward_by(scope, options = {}) click to toggle source

Add a scope to ward. Some methods related with the scope will be generated and mixed into ActionController::Base.

Examples

ApiWarden.ward_by('users')
ApiWarden.ward_by('users', expire_time_for_access_token: 2.days.seconds)
ApiWarden.ward_by('users', value_for_access_token: proc { |access_token, *args| ... })

@param scope [String] @param options [Hash] see Scope#initialize

# File lib/api_warden.rb, line 35
def self.ward_by(scope, options = {})
  name = validate_scope_name(scope)
  raise "Scope #{name} already defined" if find_scope(name)

  scope = Scope.new(name, options)
  SCOPES[name] = scope
  Helpers.define_helpers(scope)
end

Private Class Methods

validate_scope_name(scope) click to toggle source
# File lib/api_warden.rb, line 97
def self.validate_scope_name(scope)
  scope.to_s.singularize.downcase
end