Search code examples
ruby-on-railsweb-servicesrestactiverecordactiveresource

Ruby/Rails - consume webservice with ActiveResource


just getting started with Rails, I would like to consume a webservice (using ActiveResource) that has the following endpoint:

GET /user?some_header=XYZ

This is my ActiveResource Class:

    class User < ActiveResource::Base
      self.site = "url"
      set_collection_name 'user'  #avoid pluralization within the url
    end

How would a call for the above endpoint now look like?

I tried

    User.get('', headers={:some_header => "XYZ"})

but I'm getting a 404 (the request works when I fire it by hand).


Solution

  • Try

    User.all(:params=>{:some_header => "XYZ"})
    

    If you want to avoid format in your path, you can ovveride base collection_path method

       def collection_path(prefix_options = {}, query_options = nil)
            prefix_options, query_options = split_options(prefix_options) if query_options.nil?
            "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
        end
    

    to

       def collection_path(prefix_options = {}, query_options = nil)
            prefix_options, query_options = split_options(prefix_options) if query_options.nil?
            "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
          end