0

I think this is pretty straightforward but I've been trying for days without a solution. Everything I've tried returned errors. Hope to get some help here.

First of all, I am using this gemfile here :

https://github.com/ryanwkan/covetous

After initializing with a VALID tagID(rwk#1242)

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

I get a return object like this

http://pastebin.com/CcW0aaLL

Top portion of the object looks like this:

#<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 ...

And if I initialize with an INVALID tagID(invalid#9999)

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

I get a return object like this:

#<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"]}>>

I need to flash an error message whenever an invalid tag is given.

Question is, how should I know if I got a valid response or an invalid response?

I've tried checking if the response for the key 'code' or 'heroes' but it only works when the key is available, else I get a NoMethodError. Is there any way to test for the presence of a key, returning true/ false?

mnel
  • 113,303
  • 27
  • 265
  • 254
Ryan W Kan
  • 380
  • 3
  • 16

1 Answers1

0

If asking for code or heroes is known to give you a NoMethodError on the occasions you are looking for, just wrap that call in a begin/end block and catch the error with rescue.

matt
  • 515,959
  • 87
  • 875
  • 1,141