The lightweight REST framework for java called Restlet (http://www.restlet.org/) is able to handle JSON. Cool no need for the heavyweight called XML.
So here is an example of how you make Ruby on Rails call a webservice created with Restlet.
First have a look at the HTTP library for ruby. Do not forget to install the JSON library for Ruby.
And here is the ruby code, I am connecting with my Restlet example.
require 'net/http'
require 'json'
....
def show_user(id=nil)
return nil if id == nil
url = URI.parse("http://localhost:8100/users/#{id}")
req = Net::HTTP::Get.new(url.path)
# We want JSON returned
req['Accept'] = 'application/json'
resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
if !resp.kind_of?(Net::HTTPSuccess) then
case resp
when Net::HTTPBadRequest
raise ArgumentError
when Net::HTTPInternalServerError
raise Error, "Server error"
when Net::HTTPClientError
raise Error, "Client error"
end
end
data = resp.body
return nil if data.nil?
results = JSON.parse(data)
return results
end
Labels: Restlet, Ruby on Rails
0 Comments:
Subscribe to:
Post Comments (Atom)