问题描述
我建设我的第一个Rails应用程序,我希望它消耗一切从一个REST API。我想做的是让Rails的为我的web应用程序作为一个前端到我的API。至于我读过(我使用Rails盯着现在),Rails有很大的潜力与奥姆斯和直接访问数据库系统。我的平台,在另一方面,被设计以这样的方式,每一个层经由定义的接口访问的(在这种情况下,REST API),所以没有数据库可从任何客户端读取,而是通过其接口
I'm building my first Rails App and I want it to consume everything from a REST API. What I want to do is to have Rails serve my web application as a frontend to my API. As far as I've read (I'm staring with Rails right now), Rails has a lot of potential with ORMs and direct access to database systems. My platform, on the other hand, is designed in such a way that every layer is accessed via a defined interface (in this case a REST API), so no databases are read from any client, but via their interfaces.
例如,我的API公开以下资源:
For example, my API exposes the following resource:
https://api.example.com/v1/users/feature-xxx [GET]
和我想我的web应用程序有一个页面,如:
And I want my web app to have a page like:
https://example.com/feature
因此,用户会访问这个URL,并登录时,Rails应用程序将请求数据来从我的API此动态内容。
So the users will visit this URL and when logged in, the Rails app will request the data to generate this dynamic content from my API.
现在的问题是:
- 什么是必要的步骤,为我的Rails应用程序从一个HTTP / REST后端消耗其数据?和
- 这是一个Rails应用程序设计好?
谢谢!
推荐答案
不再被包含在Rails的4.0的ActiveResource。字是,它是勉强维持这些天,并且难以定制为将根据滑轨方式不配制REST API端点。
ActiveResource is no longer being included in Rails 4.0. Word is that it is barely maintained these days, and is difficult to customize for REST API endpoints that are not formulated according to the "Rails way".
经过一番研究,我在很大程度上有利于使用。它包括利用不同的HTTP请求适配器的支持。它可以直接使用EventMachine的,或者像百头巨怪库,当你决定要获得并发。它还支持机架般的中间件无缝包括,比如说,身份验证。
After some research, I am heavily favoring using Faraday. It includes support for utilizing different HTTP request adapters. It can use EventMachine directly, or libraries like Typhoeus for when you decide to get concurrent. It also supports Rack-like middleware for seamlessly including, say, authentication.
对于一个REST ORM在Rails中,很容易配置的,相对较新的(一岁)看起来非常有前途,并利用法拉第。
For a REST ORM in Rails that is easily configurable, the relatively new (about a year old) Her looks very promising, and utilizes Faraday.
更新
我完全3; 。很简单。如果您需要ORM风格的功能,那么你需要一个更高层次的抽象,但只是消耗的API,你不能打败它的简单。它只是不正是它应该没有大惊小怪的事,有合理的默认值,并有设置高级选项,例如身份验证头的能力。
I completely <3 RestClient. So simple. If you need ORM-style functionality, then you'll need a higher-level abstraction, but for simply consuming an API, you can't beat its simplicity. It just does exactly what it's supposed to do without fuss, has sensible defaults, and has the ability to set advanced options such as auth headers.
这篇关于从Rails应用程序使用REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!