Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

At this time, in the web service accounts_controller.rb 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 some advice for connecting to the web service. 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?

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?

Is it advisable to use third-party plugins and gems?

like image 452
user502052 Avatar asked Feb 18 '11 16:02

user502052


4 Answers

Yes, since you're using RESTful routes, they are your basic API. You're also returning structured JSON data easily consumable by an application.

There are other ways to implement a web services API (e.g. SOAP), but this is a good and proper way.

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. 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.

like image 118
Andrew Marshall Avatar answered Nov 15 '22 18:11

Andrew Marshall


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. 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.

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:

  • HTTPClient
  • Typhoeus

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. 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.

like image 26
the Tin Man Avatar answered Nov 15 '22 16:11

the Tin Man


I'd recommend using Rails' ActiveResource for most cases. Failing that, httparty.

like image 2
idlefingers Avatar answered Nov 15 '22 16:11

idlefingers


I would use ActiveResource if you just need a simple way to pull in rest-based resources. It's already included in rails and pretty trivial to set up. 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.

like image 1
dishelevedest Avatar answered Nov 15 '22 18:11

dishelevedest