ruby - Connecting to web services using Rails (HTTP requests)? -
i using ruby on rails 3 , trying implement apis retrieve account information web service. is, connect web service has account class , information show
action routed @ uri http://<site_name>/accounts/1
.
at time, in web service accounts_controller.rb
file have:
class accountscontroller < applicationcontroller def show @account = account.find(params[:id]) respond_to |format| format.html format.js format.json { render :json => @account.to_json } end end end
now need advice connecting web service. in client application, should have http request, here question: "the best" approach connect web service making http requests?
this code in 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, above code "the way" implement apis?
is advisable use third-party plugins , gems?
yes, since you're using restful routes, basic api. you're returning structured json data consumable application.
there other ways implement web services api (e.g. soap), , proper way.
since it's web services api, connecting correct url , parsing response way go on client side. though if need access many different resources might idea create flexible way of building request url.
Comments
Post a Comment