Syl
Syl

Reputation: 3829

i want to use a REST api, i cannot manage to set active resource to use it

The app i am working on use the API from TrackMania 2/Maniaplanet

The current part i am trying to use is the request to return one player data :

http://ws.maniaplanet.com/players/sylario/

where sylario is the user name. Removing the last "/" return a 404.

Currenrly I have managed to make active resource generate the following request by asking "#{playername}/" :

http://ws.maniaplanet.com:80/players/sylario/.json

I'd like to remove the .json as the API return a 404.

Here is my model class :

class Player < ActiveResource::Base
  self.site = "http://ws.maniaplanet.com/"
  self.user="********"
  self.password="************"
end

The request from a controller:

Player.find("sylario/")

I also forsee problem when rails will receive the HTML formated answer, but one thing at a time.

So, how to remove the .json?

Upvotes: 0

Views: 817

Answers (2)

Mark Fletcher
Mark Fletcher

Reputation: 31

With version 4 of ActiveResource you can now use include_format_in_path to remove the.json and also set the accept header:

class Player < ActiveResource::Base
  self.site = "http://ws.maniaplanet.com/"
  self.user="********"
  self.password="************"
  self.include_format_in_path = false
end

Upvotes: 3

Dan Knox
Dan Knox

Reputation: 644

UPDATE Player.find(:one, :from => "/players/sylario/") will skip the generated element path and not append the .json to your URL.

Can you post the code for your controller or wherever you are making the actual request to the service?

Also, if you need to strip the extension from the URL I believe you will have to monkey patch ActiveResource. You could do the following in an initializer file:

class ActiveResource::Base
  def element_path(id, prefix_options = {}, query_options = nil)
    check_prefix_options(prefix_options)

    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
end

This overrides the method ActiveResource uses to generate the URL and does not append the format extension.

Upvotes: 1

Related Questions