2010-12-20 74 views
3

我想打開我的Rails 2.3應用程序(在Heroku上託管)給開發人員。我想到了兩種方法:如何將公共API添加到Rails應用程序?

  1. 使用respond_to | format |的應用程序,以及一個只允許授權開發人員使用API​​的密鑰
  2. 使用專用於API的第二個Heroku帳戶,共享原始應用程序的數據庫。現在,什麼會更好:Rails,Sinatra或Grape

我知道這是一個模糊的問題。你有什麼好的文章或建築模式可以幫助我嗎?

感謝,

凱文

回答

4

我想補充一個新的路由

website.com/api/widget/

website.com/api/another_service/

而且使一個控制器在API路由下看起來很自然。

然後我會創建一個模塊,並在之前檢查過濾器,但是您要檢查API密鑰。

class WidgetController < ActionController::Base 
    include 'api_keyable' 
    :before_filer :validate_api_key 

    def create 
     #However you create a widget from the params. 
     # respond to XML, YAML, JSON, whatever 
    end 

end 

如果你後來發現要處理一個應用程序,並在其他website.com/api/wadgets website.com/api/widgets,最終用戶將永遠不會知道,你的代碼不應該需要變化很大。

7

我們使用葡萄。它很簡單,並允許更清晰的分離和語義。 API不是一個真正的控制器。