ActiveResource expects the endpoint to return data in a very specific format.
For a long time, we used ActiveResource in my company to interact between applications. Recently, however, we have begun to lean toward HTTParty because it performs much less voodoo magic and tends to be a very small hair extension exercise.
Here is an example of how we are now using HTTParty:
module CoreResources class Job include HTTParty base_uri Rails.configuration.core_resource_uri basic_auth Rails.configuration.core_resource_user, Rails.configuration.core_resource_password def self.search(entity) get("/api/v1/jobs.json", :query => {:entity_id => entity.id}) end def self.find(id) result = get("/api/v1/jobs/#{id}.json") raise CoreResources::JobNotFound.new if result.response.code == "404" raise "Unexpected response from resource job find: #{result.response.code} #{result.response.to_s}" if result.response.code =~ /^(?:4|5)..$/ result end end end
The problem with ActiveResource is that it will use specially designed json or xml markup and create instances of ActiveResource objects and nested objects based on them. He was having trouble calling collect because something in the json response had to be formatted as an enumeration and wasn’t (probably the parent node had to be an array or something not sure), which caused it to explode.
With HTTParty, you get a json-parsed collection to work with.
It’s easy enough for me to do this:
jobs = CoreResources::Job.search(my_entity) puts jobs.inspect # [{ # "id" => 4, # "created_by_id" => 12, # "description" => "I like pie" # }, # { # "id" => 5", # "created_by_id" => 12, # "description" => "Mmm, cake" # }]
Allows me to access jobs through a simple array / hash construct jobs[0].fetch("description") , unlike ActiveResource: jobs[0].description . ActiveResource runs slower to impose these collections, takes up memory with them unnecessarily, and offers you to duplicate code that should just be served by the endpoint in your ActiveResource model (again, if you use a third-party API, which may not be otherwise of choice, but I never got ARes to interact with third-party APIs).
We ran into many other ActiveResource issues where it makes this pointless dynamic generation of class names based on nested resources from your endpoint, but half the time does it wrong ... It's really just a mess.
Moral of the story: now a lot more HTTParty fan. This endpoint probably just doesn't return the data in the correct format, and unless ActiveResource becomes a hackfest to get it to read it correctly.