使用 rails 消费 web services/apis [英] Using rails to consume web services/apis

查看:54
本文介绍了使用 rails 消费 web services/apis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 世界的新手,正在尝试构建一个应用程序,让我可以根据用户输入在 Amazon 和此类网站上搜索内容.

I'm new to the rails world and am trying to build a app to simply allow me to search things on Amazon and such sites based on the users input.

我做了一些研究,似乎 httparty gem 是一个不错的起点?到目前为止,我找到的文件并不是最好的.他们并没有真正给我很多信息(代码放在哪里等).

I've done a little reasearch and it seems the httparty gem is a good place to start? The documents ive found so far though are not the best. They don't really give me a lot of information (where to put the code etc).

是否有任何现有的教程或代码示例可供我使用或查看以让我更好地了解它的工作原理?

Are there any existing tutorials out there or code examples that I can either use or take a look at to give me a better idea of how this thing works?

推荐答案

我现在正在开发一个这样的应用程序,所以让我提供一些想法.

I'm working on an app like this right now, so let me offer a few thoughts.

首先,如果您完全是 Rails 的新手,那么作为第一步,我建议您采用平行的方法来解决这个问题,有两条路线:

First, if you're a complete newcomer to Rails, then as a first step I'd suggest taking a parallel approach to the problem, with two tracks:

  1. 了解 Rails
  2. 了解如何在 Ruby 中与 API 交互(发出请求和解析响应)

这是两个独立的问题,尽管您最终可能会一起实施它们,但最好先分别考虑它们.

These are two separate problems, and although you may end up implementing them together, it's best to think about them in isolation first.

对于第一个,我建议先编写几个简单的应用程序,以了解其工作原理.即使只是一个简单的应用程序也需要一定量的用户交互,可能将记录保存到数据库等,这些问题与从 API 消费数据是分开的.关于 Rails 的教程数不胜数,但仅举一个,您可以尝试 Michael Harti 的 Learn Web Development with Rails 作为一个起点.

For the first one, I'd suggest writing a couple simple apps first to get a feel for how things work. Even only a simple app will require a certain amount of user interaction, possibly saving records to the DB, etc., problems that are separate from consuming data from an API. There are an endless number of tutorials out there on Rails, but just to mention one, you might try Michael Harti's Learn Web Development with Rails as a starting point.

第二点,使用 API 数据,不同于设计应用程序本身的问题.要了解有关问题这方面的更多信息,我建议使用流行的 API 客户端 gem(如您所提到的)HTTParty,我自己用的.与其立即尝试在 Rails 应用程序中使用 HTTParty 方法,不如作为练习,我建议在控制台 (irb) 中尝试一下.如果您安装 gem (gem install httparty),则可以从控制台请求它 (require 'httparty') 并立即从 API 发出请求和解析响应.

The second point, consuming API data, is distinct from the problems of designing the app itself. To learn more about this aspect of the problem, I'd suggest using a popular API client gem like (as you mentioned) HTTParty, which I use myself. Rather than immediately try to use the HTTParty methods in a Rails app, as an exercise I'd suggest playing around a bit in a console (irb). If you install the gem (gem install httparty) you can then require it (require 'httparty') from the console and immediately make requests and parse responses from the APIs.

例如:

irb(main):001:0> require 'httparty'
=> true
irb(main):002:0> response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
=> ...

一旦您对 Rails 和在 ruby​​ 中访问 API 更加熟悉,那么您就可以继续实际构建应用程序本身.根据您对 API 的请求的复杂程度,您有几个选项可以决定如何在 Rails 应用中构建 API 请求:

Once you've got a bit more familiar with Rails and with accessing an API in ruby, then you could move on to actually building the app itself. Depending on how complex your requests to the API(s) are going to be, you have a few options as to how to structure your API requests in your Rails app:

  1. 直接从控制器操作将请求作为 Rails 应用程序本身的一部分.如果您只打算在有限数量的服务上支持非常有限数量的请求类型(例如简单搜索),那么这真的没问题.任何更复杂的事情都会让您的控制器变胖,这在 MVC 框架中是禁忌.
  2. 创建一个单独的 ruby​​ 类(通常称为 API 包装器),将用于发出请求和解析来自 API 的响应的方法组合在一起.使用像 HTTParty 这样的 gem,您只需在类中添加一行 include HTTParty,即可将模块添加到类中.有很多关于如何做到这一点的例子,这是一个.
  3. 使用专用 gem 访问特定 API,或自己创建一个(或多个).大多数流行的服务都有宝石,例如推特领英Flickr 等.请参阅此列表API 客户端 gems 了解更多.这消除了与 API 交互的许多痛苦,但仅适用于所有服务的一个子集.
  1. Make the requests part of the Rails app itself, directly from controller actions. This is really only okay if you are only going to support a very limited number of request types (e.g. simple search) on a limited number of services. Anything more complex and your controller will get fat, which is a no-no in MVC frameworks.
  2. Create a separate ruby class (usually called an API wrapper) to group together methods for making requests and parsing responses from the API. With a gem like HTTParty you can do this just by adding a line include HTTParty in the class, which adds the module to the class. There are lots of examples out there on how to do this, here is one.
  3. Use a dedicated gem for accessing specific APIs, or create one (or more) yourself. There are gems for most popular services, e.g. Twitter, Linkedin, Flickr, etc. See this list of API client gems for more. This takes away a lot of the pain of interacting with an API, but will only work for a subset of all services.

您提到您正在考虑使用 HTTParty,我可以推荐它,因为我自己正在使用它.还有其他替代方法,例如 Faraday(请参阅 此 HTTP 客户端列表 了解更多),但我发现对于大多数任务 HTTParty 都可以.文档可能有点少,但有很多示例可供您参考.

You mention you're thinking of using HTTParty, which I can recommend as I am using it myself. There are alternatives such as Faraday (see this list of HTTP clients for more), but I find for most tasks HTTParty will do fine. The documentation may be a bit sparse, but there are a bunch of examples that you can work from.

希望有帮助!

这篇关于使用 rails 消费 web services/apis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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