简体   繁体   English

使用Rails(HTTP请求)连接到Web服务?

[英]Connecting to web services using Rails (HTTP requests)?

I am using Ruby on Rails 3 and I am trying to implement APIs to retrieve account information from a web service. 我正在使用Ruby on Rails 3,我正在尝试实现API以从Web服务检索帐户信息。 That is, I would like to connect to a web service that has the Account class and get information from the show action routed at the URI http://<site_name>/accounts/1 . 也就是说,我想连接到具有Account类的Web服务,并从URI http://<site_name>/accounts/1路由的show action获取信息。

At this time, in the web service accounts_controller.rb file I have: 这时,在web服务accounts_controller.rb文件中,我有:

class AccountsController < ApplicationController
  def show
    @account = Account.find(params[:id])

    respond_to do |format|
      format.html
      format.js
      format.json { render :json => @account.to_json }
    end
  end
end

Now I need some advice for connecting to the web service. 现在我需要一些建议来连接到Web服务。 In the client application, I should have a HTTP GET request, but here is my question: What is "the best" approach to connect to a web service making HTTP requests? 在客户端应用程序中,我应该有一个HTTP GET请求,但这是我的问题:连接到发出HTTP请求的Web服务的“最佳”方法是什么?

This code in the client application works: 客户端应用程序中的此代码有效:

url = URI.parse('http://<site_name>/accounts/1.json')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}

@output = JSON(res.body)["account"]

but, is the above code "the way" to implement APIs? 但是,上面的代码是“实现API的方式”吗?

Is it advisable to use third-party plugins and gems? 是否可以使用第三方插件和宝石?

Yes, since you're using RESTful routes, they are your basic API. 是的,因为您使用的是RESTful路由,所以它们是您的基本API。 You're also returning structured JSON data easily consumable by an application. 您还可以返回应用程序可轻松使用的结构化JSON数据。

There are other ways to implement a web services API (eg SOAP ), but this is a good and proper way. 还有其他方法可以实现Web服务API(例如SOAP ),但这是一种很好的方法。

Since it's a web services API, connecting to the correct URL and parsing the response is the way to go on the client side. 由于它是一个Web服务API,连接到正确的URL并解析响应是客户端的方法。 Though if you need to access many different resources it might be a good idea to create a flexible way of building the request URL. 虽然如果您需要访问许多不同的资源,那么创建一种构建请求URL的灵活方法可能是个好主意。

If you don't need low-level tweak-ability offered by Net::HTTP, instead take a look at using Open-URI , which comes with Ruby. 如果您不需要Net :: HTTP提供的低级调整功能,那么请看一下使用Ruby附带的Open-URI It makes it easy to request a page and receive the body back. 它可以轻松地请求页面并接收身体。 Open-URI doesn't have all the bells and whistles but for a lot of what I do it's plenty good. Open-URI并没有所有的花里胡哨,但对于我所做的很多事情来说,这是非常好的。

A simple use looks like: 一个简单的用法看起来像:

require 'open-uri'
body = open('http://www.example.com').read

The docs have many other examples. 文档还有很多其他的例子。

These are other HTTP clients I like: 这些是我喜欢的其他HTTP客户端:

They are more tweakable and can handle multiple connections at once if that's what you need. 它们更具可调性,如果您需要,它可以同时处理多个连接。 For instance, Typhoeus has a suite of simplified calls, similar to Open-URI's. 例如,Typhoeus有一套简化的调用,类似于Open-URI。 From the docs: 来自文档:

response = Typhoeus::Request.get("http://www.pauldix.net")
response = Typhoeus::Request.head("http://www.pauldix.net")
response = Typhoeus::Request.put("http://localhost:3000/posts/1", :body => "whoo, a body")
response = Typhoeus::Request.post("http://localhost:3000/posts", :params => {:title => "test post", :content => "this is my test"})
response = Typhoeus::Request.delete("http://localhost:3000/posts/1")

HTTPClient has similar shortened methods too. HTTPClient也有类似的缩短方法。

I'd recommend using Rails' ActiveResource for most cases. 在大多数情况下,我建议使用Rails的ActiveResource Failing that, httparty . 没错httparty

I would use ActiveResource if you just need a simple way to pull in rest-based resources. 如果您只需要一种简单的方法来获取基于休息的资源,我会使用ActiveResource。 It's already included in rails and pretty trivial to set up. 它已经包含在rails中并且设置起来非常简单。 You just specify a base url and resource name in your ActiveResource subclass and then you can CRUD rest-based resources with an interface similar to that of ActiveRecord. 您只需在ActiveResource子类中指定基本URL和资源名称,然后就可以使用类似于ActiveRecord的接口来CRUD基于休息的资源。

rest-client是最容易连接到RESTful Web服务的最受欢迎的gem

I think one of the best solutions out there is the Her gem . 我认为最好的解决方案之一就是她的宝石 It encapsulates restful requests providing objects with active-record like behavior. 它封装了提供具有活动记录行为的对象的restful请求。

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

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