2012-10-28 47 views
6

我想知道哪些提供商已被加載供OmniAuth gem使用。我已經試過這樣:查找加載的提供商OmniAuth

OmniAuth::Strategies.constants # a method provided by the standard lib Module class 
# => [:Developer, :OAuth, :Twitter] 

這:

OmniAuth.strategies.inspect # a method provided by the OmniAuth class, but which has no documentation or comments around it. 
# => [OmniAuth::Strategies::OAuth] 

我期望(或希望)答案是[:Developer, :Twitter]在我的測試代碼,我只加載Twitter的明確,以及開發者提供的是默認加載的。

(這一切都是那麼不同的庫可以加載正確的事情,它的工作,取決於什麼OmniAuth運行。)如果有一種方法

,你知道的話,請讓我知道。否則,我會實事求是,將OAuth從第一個例子中排除在列表之外。

Ruby是1.9.3和OmniAuth是V1.1.1

回答

6

OmniAuth ::戰略列出了戰略提供並登記。不是那些「使用」的。如果您深入瞭解OmniAuth builder的代碼,您會發現使用use作爲提供程序塊中的中間件可以將各種策略傳遞到Rack,這使得跟蹤策略變得更加困難。另一種「務實」的方法是猴子修補OmniAuth Builder並跟蹤提供者。

module OmniAuth 
    class Builder < ::Rack::Builder 
    def provider_patch(klass, *args, &block) 
     @@providers ||= [] 
     @@providers << klass 
     old_provider(klass, *args, &block) 
    end 
    alias old_provider provider 
    alias provider provider_patch 
    class << self 
     def providers 
     @@providers 
     end 
    end 
    end 
end 

在配置您的提供商之前包括此修補程序。一旦所有的提供者都被加載完畢,OmniAuth::Builder.providers會給你你想要的數組。

儘管開發者策略可用,但未加載。它只是如果你指定加載

provider :developer 
+0

感謝您抽出寶貴的時間與此,這是非常有益的和讚賞。 – iain

15

添加此處爲設計用戶,因爲我需要與iain相同的列表。我嘗試使用在我的設計初始化器在Rails項目的頂部接受的答案,但我得到一個錯誤(@@提供程序未定義)。

尋找到設計源代碼後,我用下面獲取符號數組:

Devise.omniauth_configs.keys # => [:facebook, :twitter] 
+6

你也可以使用Devise.omniauth_providers –