module Kingsman

def self.add_mapping(resource, options)

def self.add_mapping(resource, options)
  mapping = Mapping.new(resource, options)
  @@mappings[mapping.name] = mapping
  @@default_scope ||= mapping.name
  Kingsman::Controllers::Helpers.define_helpers(mapping)
  mapping # important to return the mapping
end

def self.add_module(module_name, options = {})

def self.add_module(module_name, options = {})
  ALL.insert (options[:insert_at] || -1), module_name
  if strategy = options[:strategy]
    strategy = (strategy == true ? module_name : strategy)
    STRATEGIES[module_name] = strategy
  end
  if controller = options[:controller]
    controller = (controller == true ? module_name : controller)
    CONTROLLERS[module_name] = controller
  end
  NO_INPUT << strategy if options[:no_input]
  if route = options[:route]
    case route
    when TrueClass
      key, value = module_name, []
    when Symbol
      key, value = route, []
    when Hash
      key, value = route.keys.first, route.values.flatten
    else
      raise ArgumentError, ":route should be true, a Symbol or a Hash"
    end
    URL_HELPERS[key] ||= []
    URL_HELPERS[key].concat(value)
    URL_HELPERS[key].uniq!
    ROUTES[module_name] = key
  end
  if options[:model]
    path = (options[:model] == true ? "kingsman/models/#{module_name}" : options[:model])
    camelized = ActiveSupport::Inflector.camelize(module_name.to_s)
    Kingsman::Models.send(:autoload, camelized.to_sym, path)
  end
  Kingsman::Mapping.add_module module_name
end

def self.available_router_name

def self.available_router_name
  router_name || :main_app
end

def self.configure_warden! #:nodoc:

:nodoc:
See lib/kingsman/rails/routes.rb - ActionDispatch::Routing::RouteSet#finalize_with_kingsman!
A method used internally to complete the setup of warden manager after routes are loaded.
def self.configure_warden! #:nodoc:
  @@warden_configured ||= begin
    # warden_config.failure_app   = Kingsman::SessionsController.action(:unauthenticated)
    warden_config.failure_app   = Kingsman::Delegator.new
    warden_config.default_scope = Kingsman.default_scope
    warden_config.intercept_401 = false
    Kingsman.mappings.each_value do |mapping|
      warden_config.scope_defaults mapping.name, strategies: mapping.strategies
      warden_config.serialize_into_session(mapping.name) do |record|
        mapping.to.serialize_into_session(record)
      end
      warden_config.serialize_from_session(mapping.name) do |args|
        mapping.to.serialize_from_session(*args)
      end
    end
    @@warden_config_blocks.map { |block| block.call Kingsman.warden_config }
    true
  end
end

def self.friendly_token(length = 20)

By default, length is 20 characters.
Generate a friendly string randomly to be used as token.
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

def self.include_helpers(scope)

Include helpers in the given scope to AC and AV.
def self.include_helpers(scope)
  ActiveSupport.on_load(:jets_controller) do
    include scope::Helpers if defined?(scope::Helpers)
    include scope::UrlHelpers
  end
  ActiveSupport.on_load(:action_view) do
    include scope::UrlHelpers
  end
end

def self.mailer

Get the mailer class from the mailer reference object.
def self.mailer
  @@mailer_ref.constantize
end

def self.mailer=(class_name)

Set the mailer reference object to access the mailer.
def self.mailer=(class_name)
  @@mailer_ref = class_name
end

def self.omniauth(provider, *args)


config.omniauth :github, APP_ID, APP_SECRET

Specify an OmniAuth provider.
def self.omniauth(provider, *args)
  config = Kingsman::OmniAuth::Config.new(provider, args)
  @@omniauth_configs[config.strategy_name.to_sym] = config
end

def self.omniauth_providers

def self.omniauth_providers
  omniauth_configs.keys
end

def self.regenerate_helpers!

Regenerates url helpers considering Kingsman.mapping
def self.regenerate_helpers!
  Kingsman::Controllers::UrlHelpers.remove_helpers!
  Kingsman::Controllers::UrlHelpers.generate_helpers!
end

def self.secure_compare(a, b)

constant-time comparison algorithm to prevent timing attacks
def self.secure_compare(a, b)
  return false if a.blank? || b.blank? || a.bytesize != b.bytesize
  l = a.unpack "C#{a.bytesize}"
  res = 0
  b.each_byte { |byte| res |= byte ^ l.shift }
  res == 0
end

def self.setup

a fresh initializer with all configuration values.
Default way to set up Kingsman. Run jets generate kingsman:install to create
def self.setup
  yield self
end

def self.warden(&block)

end
end
manager.oauth(:twitter)
# Configure warden to use other strategies, like oauth.
config.warden do |manager|

config.allow_unconfirmed_access_for = 2.days
Kingsman.setup do |config|

initialization.
Sets warden configuration using a block that will be invoked on warden
def self.warden(&block)
  @@warden_config_blocks << block
end