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?
ruby api ruby-on-rails ruby-on-rails-3 connection
user502052
source share