1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise/omniauth/config.rb

46 lines
1.3 KiB
Ruby
Raw Normal View History

2010-10-14 14:04:02 -04:00
module Devise
module OmniAuth
class StrategyNotFound < NameError
def initialize(strategy)
@strategy = strategy
super("Could not find a strategy with name `#{strategy}'. " \
2011-11-24 03:32:48 -05:00
"Please ensure it is required or explicitly set it using the :strategy_class option.")
end
end
2010-10-14 14:04:02 -04:00
class Config
attr_accessor :strategy
attr_reader :args, :options, :provider, :strategy_name
2010-10-14 14:04:02 -04:00
def initialize(provider, args)
@provider = provider
@args = args
@options = @args.last.is_a?(Hash) ? @args.last : {}
@strategy = nil
@strategy_name = options[:name] || @provider
@strategy_class = options.delete(:strategy_class)
end
2010-10-14 14:04:02 -04:00
def strategy_class
@strategy_class ||= find_strategy || autoload_strategy
2011-10-17 13:42:40 -04:00
end
def find_strategy
::OmniAuth.strategies.find do |strategy_class|
strategy_class.to_s =~ /#{::OmniAuth::Utils.camelize(strategy_name)}$/ ||
strategy_class.default_options[:name] == strategy_name
end
end
def autoload_strategy
name = ::OmniAuth::Utils.camelize(provider.to_s)
if ::OmniAuth::Strategies.const_defined?(name)
::OmniAuth::Strategies.const_get(name)
2011-10-17 13:42:40 -04:00
else
raise StrategyNotFound, name
2011-10-17 13:42:40 -04:00
end
2010-10-14 14:04:02 -04:00
end
end
end
end