Rails 1.x client talking to RESTful server
Hey all, I've got a client that is integrating a Rails 1.2.6 site with another site that exposes services RESTfully. Upgrading to Rails 2.x is not an option at this time. Does anyone have recommendations for methods other than direct Net::HTTP calls to communicate with the REST service? Techniques or Gem recommendations are welcome, but most of the gems I've seen seem to have a dependency on ActiveSupport 2.x, which I understand to be incompatible with Rails 1.x.
Thanks in a开发者_StackOverflowdvance for any input you can provide.
Try HTTParty. It's very light on the dependencies, and makes it braindead-easy to add consumption of JSON or XML resources to an application.
Thanks Chris Heald for your response. I did end up using Net::HTTP because it was more straightforward than I thought it was in the end. HTTParty looks like it could make this easier still, but for the benefit of future people with this problem, here's what I did.
# Assume @user_name and @password were previously declared to be the
# appropriate basic auth values and that the connection is open as @connection
def put(path, body, header={})
request = Net::HTTP::Put.new(path, header.merge({'Accept' => 'application/xml,application/json', 'Content-type'=>'application/json'}))
request.basic_auth(@user_name, @password)
@connection.request(request, body).body
end
def post(path, body, header={})
request = Net::HTTP::Post.new(path, header.merge({'Accept' => 'application/xml,application/json', 'Content-type'=>'application/json'}))
request.basic_auth(@user_name, @password)
@connection.request(request, body).body
end
def get(path, header={})
request = Net::HTTP::Get.new(path)
request.basic_auth(@user_name, @password)
@connection.request(request).body
end
I then called JSON::parse() on the output of these methods and got a hash representing the JSON that I could use as I saw fit.
精彩评论