0

I am using ActiveResource to manage accessing an external service.

The external service has an URL like:

http://api.cars.com/v1/cars/car_id/range/range_num?filter=filter1,filter2

Here's my Car class:

    class Car < ActiveResource::Base                                                                                 

          class << self                                                                                                                                                                 
            def element_path(id, prefix_options = {}, query_options = nil)                                                                                                              
              prefix_options, query_options = split_options(prefix_options) if query_options.nil?                                                                                       
                  "#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"                                                                  
                end                                                                                                                                                                         

                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                                                                                                  end                              

self.site = "http://api.cars.com/"                                                                     
self.prefix = "/v1/"
self.format = :json

end

When I set up my object to get a particular car in rails console:

> car = car.new
> car.get('1234')

I get a URL like this:

http://api.cars.com/v1/cars//1234.json

How do I get the URL to include the range and range_num elements?

Also, i don't want the .json extension on the end of the URL. I've attempted overriding the element_name and collection_name methods as described here: How to remove .xml and .json from url when using active resource but it doesn't seem to be working for me...

Thanks in advance for any ideas!

Community
  • 1
  • 1
Sly
  • 743
  • 13
  • 38

1 Answers1

0

Get rid of the forward slash in the URL

 "#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"                                                                   

becomes:

"#{prefix(prefix_options)}#{collection_name}#{URI.parser.escape id.to_s}#{query_string(query_options)}"                                                                   
Chris Woolum
  • 2,854
  • 20
  • 20