Connecting to web services using Rails (HTTP requests)?

I am using Ruby on Rails 3 and I am trying to implement an API to retrieve account information from a web service. That is, I would like to connect to a web service that has an Account class and get information from the show action redirected to the URI http://<site_name>/accounts/1 .

At this time, in the accounts_controller.rb web service file, I have:

 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 advice for connecting to a web service. In the client application, I should have a GET HTTP request, but my question is: what is the β€œbest” approach for connecting to a web service that creates HTTP requests?

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 a β€œway” to implement the API?

Can I use third-party plugins and gems?

+6
ruby api ruby-on-rails ruby-on-rails-3 connection
source share
6 answers

Yes, since you use RESTful routes, this is your main API. You also return structured JSON data that can easily be used by the application.

There are other ways to implement the web services API (such as SOAP ), but this is a good and correct way.

Since the web services API, connecting to the correct URL and parsing the answers is the way to the client side. Although, if you need to access many different resources, it might be a good idea to create a flexible way to create a request URL.

+4
source

If you don't want the low-level configuration offered by Net :: HTTP, take a look at the Open-URI that comes with Ruby instead. This makes it easy to request a page and return the body back. The Open-URI does not have all the bells and whistles, but for many, I do a lot of good things.

Simple use looks like this:

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

There are many other examples in the docs.

These are the other HTTP clients that I like:

They are more adaptable and can handle several connections at once, if necessary. For example, Typhoeus has a set of simplified calls, similar to the 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 truncated methods.

+3
source

I would recommend using Rails ActiveResource for most cases. Otherwise httparty .

+2
source

I would use ActiveResource if you just need an easy way to draw resources based on rest. It is already included in the rails and quite trivial to set up. You simply specify the base url and resource name in your ActiveResource subclass, and then you can use CRUD-based resources based on an interface similar to the ActiveRecord interface.

+1
source

rest-client is the most popular stone for easy connection to RESTful web services.

+1
source

I think one of the best solutions is Her gem . It encapsulates sedative requests, providing objects with an active record as behavior.

0
source

All Articles