开发者

Writing a RoR API nested into a RoR site

开发者 https://www.devze.com 2023-04-04 04:43 出处:网络
Just curious how I would go about making a Rails API that uses开发者_StackOverflow中文版 basic auth or an API key, nested inside a Rails web application.

Just curious how I would go about making a Rails API that uses开发者_StackOverflow中文版 basic auth or an API key, nested inside a Rails web application.

I understand the making of the site, and the API separately. However, I'd like them to be a part of the same package (for lack of a better term)

For example, would this work (folder structure)

\app
\app\controllers
\app\... models views etc
\api\app\controllers
\api\app\.. models views etc

So I could use the link http:\\localhost\customers\1 or http:\\localhost\api\customers\1

This way the API could be exposed but use the same data model as the web application.

Or am I going about this problem the wrong way, and there's quite clearly a better way?

I'm using rails 3 if it makes a difference, but more looking for an idea on how to solve this. I couldn't find architecture-overflow.com :)


You could use the namespace routing feature for this:

namespace :api do
  resources :customers
end

resources :customers

This would allow you to have the directories in your app mapped like this:

app/controllers/customer_controller.rb
app/controllers/api/customer_controller.rb
app/models/customer.rb
app/views/customers/index.html.erb
app/views/api/customers/index.json.erb
... etc ...


This is a very interesting problem, which we tried to solve in our application as well. There does not seem a standard 'Rails' way to this (yet).

For every resource that we want exposed through an API, we have created a new API controller, which does just that.

The reasoning behind this:

  • the error-handling between UI and API differs:
    • UI: redirect or not, and show information flash
    • API: return clear error-codes and messages in xml/json
  • the code in the API is most of the times simpler, but the error-handling is more elaborate, because the parameters are filled in by a third party (more could go wrong)
  • the code in the UI might need to collect data from different sources to show on the UI (not a pure resource)
  • the code is clearer, although there is some duplication

Most of the duplication is handled by reusing the models. I am not entirely sure if there are better ways, but for us this feels like a good approach.

Now our API is pure xml, and we are now struggling to find a clean way to let our API render xml and json objects cleanly. Our models that are presented as resources in the API all have a specific to_xml method (that selects the columns and also renders links to related resources).

Hope this helps.


You can create RESTFUL API's like this

  namespace :api, defaults: {format: 'json'} do
    resources :stores do
      collection do
        post :check_token
      end
    end
  end

Now you create your respective controllers and views with nested folder of name api like above.

0

精彩评论

暂无评论...
验证码 换一张
取 消