我想用一个REST API,我不能设法设置活动资源使用它 [英] i want to use a REST api, i cannot manage to set active resource to use it

查看:135
本文介绍了我想用一个REST API,我不能设法设置活动资源使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序我正在使用API​​从赛道狂飙2 / Maniaplanet

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/

,其中sylario是用户名。删除最后一个/返回404。

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

Currenrly我已成功地使活动资源产生下面的请求通过询问#{} playername /

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

http://ws.maniaplanet.com:80/players/sylario/。 JSON

我想删除以.json作为API返回404。

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

下面是我的模型类:

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

从控制器请求:

Player.find("sylario/")

我也福西问题时,Rails会收到HTML格式化的答案,但一次一件事。

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

那么,如何去除以.json?

So, how to remove the .json?

推荐答案

更新的Player.find(:一,:从=>/播放/ sylario /)将跳过生成元素路径而以.json不追加到您的网址。

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

您可以张贴code为控制器或你正在实际的请求到服务?

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

另外,如果你需要剥去从URL扩展,我相信你将不得不猴补丁的ActiveResource。你可以这样做在初始化文件中:

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

这将覆盖方法的ActiveResource用来生成URL,并且不附加格式扩展名。

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

这篇关于我想用一个REST API,我不能设法设置活动资源使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆