简体   繁体   English

使用rails来使用Web服务/ apis

[英]Using rails to consume web services/apis

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. 我是rails世界的新手,我正在尝试构建一个应用程序,只是允许我根据用户输入在亚马逊和这些网站上搜索内容。

I've done a little reasearch and it seems the httparty gem is a good place to start? 我做了一点研究,似乎httparty宝石是一个很好的起点? 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. 我正在研究这样的应用程序,所以让我提出一些想法。

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: 首先,如果你是Rails的全新人,那么作为第一步,我建议采用并行方法解决问题,有两条轨道:

  1. Learn about Rails 了解Rails
  2. Learn about how to interact with an API (make requests and parse responses) in Ruby 了解如何在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. 这是两个独立的问题,虽然你可能最终将它们一起实现,但最好先考虑它们。

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. 即使只是一个简单的应用程序也需要一定数量的用户交互,可能会将数据保存到数据库等等,这些问题与从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. Rails上有无数的教程,但仅举一例,您可以尝试使用Michael Harti的Rails学习Web开发作为起点。

The second point, consuming API data, is distinct from the problems of designing the app itself. 第二点,消费API数据,不同于设计应用程序本身的问题。 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. 要了解有关此问题的更多方面,我建议使用一个流行的API客户端gem(如你所提到的) HTTParty ,我自己使用它。 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). 而不是立即尝试在Rails应用程序中使用HTTParty方法,作为练习我建议在控制台(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. 如果您安装gem( gem install httparty ),则可以从控制台请求它( require 'httparty' )并立即发出请求并解析来自API的响应。

Eg: 例如:

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

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. 一旦你对Rails更熟悉并且访问ruby中的API,那么你可以继续实际构建应用程序本身。 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: 根据您对API的请求的复杂程度,您可以选择如何在Rails应用程序中构建API请求:

  1. Make the requests part of the Rails app itself, directly from controller actions. 直接从控制器操作使请求成为Rails应用程序本身的一部分。 This is really only okay if you are only going to support a very limited number of request types (eg 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. 任何更复杂的东西,你的控制器都会变胖,这在MVC框架中是禁止的。
  2. Create a separate ruby class (usually called an API wrapper) to group together methods for making requests and parsing responses from the API. 创建一个单独的ruby类(通常称为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. 使用像HTTParty这样的gem,你可以通过在类中添加一行include HTTParty来实现这一点,它将模块添加到类中。 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. 使用专用gem访问特定API,或自己创建一个(或多个)。 There are gems for most popular services, eg Twitter , Linkedin , Flickr , etc. See this list of API client gems for more. 有大多数流行服务的宝石,例如TwitterLinkedinFlickr等。请参阅此API客户端宝石列表以获取更多信息。 This takes away a lot of the pain of interacting with an API, but will only work for a subset of all services. 这消除了与API交互的许多痛苦,但仅适用于所有服务的子集。

You mention you're thinking of using HTTParty, which I can recommend as I am using it myself. 你提到你正在考虑使用HTTParty,我可以推荐它,因为我自己使用它。 There are alternatives such as Faraday (see this list of HTTP clients for more), but I find for most tasks HTTParty will do fine. 还有其他选择,例如Faraday (请参阅此HTTP客户端列表以获取更多信息),但我发现大多数任务都是HTTParty会做得很好。 The documentation may be a bit sparse, but there are a bunch of examples that you can work from. 文档可能有点稀疏,但您可以使用一些示例

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM