从 Rails 应用程序使用 REST API [英] Consuming REST API from Rails Application

查看:30
本文介绍了从 Rails 应用程序使用 REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个 Rails 应用程序,我希望它使用 REST API 中的所有内容.我想要做的是让 Rails 将我的 Web 应用程序作为 API 的前端提供服务.据我所知(我现在正在关注 Rails),Rails 在 ORM 和直接访问数据库系统方面有很大的潜力.另一方面,我的平台的设计方式是通过定义的接口(在本例中为 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]

而且我希望我的网络应用程序具有如下页面:

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 应用来说,这是一个好的设计吗?
  • What are the necessary steps for my Rails Application to consume its data from a HTTP/Rest Backend? and,
  • Is this a good design for a Rails app?

谢谢!

推荐答案

ActiveResource 不再包含在 Rails 4.0 中.话说现在它几乎没有维护,并且很难为未根据Rails 方式"制定的 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".

经过一些研究,我非常喜欢使用 Faraday.它支持使用不同的 HTTP 请求适配器.当您决定并发时,它可以直接使用 EventMachine,也可以使用 Typhoeus 之类的库.它还支持类似 Rack 的中间件,以无缝包括身份验证.

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.

对于 Rails 中易于配置的 REST ORM,相对较新(大约一年前)Her 看起来很有前途,并且使用了法拉第.

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 RestClient.很简单.如果您需要 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆