如何确定返回对象的内容?

时间:2012-12-18 02:42:01

标签: ruby ruby-on-rails-3 rubygems

我认为这非常简单,但我已经尝试了几天没有解决方案。我尝试过的所有内容都返回了错误。希望能在这里得到一些帮助。

首先,我在这里使用这个gemfile:

https://github.com/ryanwkan/covetous

使用VALID tagID初始化后(rwk#1242)

@temp = Covetous :: Profile :: Career.new'rwk#1242'

我得到一个这样的返回对象

http://pastebin.com/CcW0aaLL

对象的顶部看起来像这样:

#<Covetous::Profile::Career:0x000001014284c8 @url="http://us.battle.net/api/d3/profile/rwk-1242/", @response=#<HTTParty::Response:0x102e69ff8 parsed_response={"heroes"=>[{"name"=>"Ziyi", "id"=>10692899, "level"=>60, "hardcore"=>false, "paragonLevel"=>87, "gender"=>1, "dead"=>false, "class"=>"wizard", "last-updated"=>1354337248}, {"name"=>"Aerendil", etc ...

如果我使用INVALID tagID初始化(无效#9999)

@temp = Covetous :: Profile :: Career.new'无效#9999'

我得到一个这样的返回对象:

#<Covetous::Profile::Career:0x00000100dfbcf8 @url="http://us.battle.net/api/d3/profile/invalid-9999/", @response=#<HTTParty::Response:0x100d96a88 parsed_response={"code"=>"OOPS", "reason"=>"There was a problem processing the request."}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"date"=>["Tue, 18 Dec 2012 02:30:00 GMT"], "server"=>["Apache"], "x-frame-options"=>["SAMEORIGIN"], "content-language"=>["en-US"], "vary"=>["Accept-Encoding"], "connection"=>["close"], "transfer-encoding"=>["chunked"], "content-type"=>["application/json;charset=UTF-8"]}>>

每当给出无效标记时,我都需要闪烁错误消息。

问题是,如果我收到有效回复或回复无效,我应该怎么知道?

我已经尝试检查键'代码'或'英雄'的响应是否只有在键可用时才有效,否则我会得到NoM​​ethodError。有没有办法测试是否存在密钥,返回true / false?

1 个答案:

答案 0 :(得分:0)

如果知道要求codeheroes在您要查找的情况下为您提供NoMethodError,请将该调用包装在begin / end块中并使用rescue捕获错误。

相关问题