views:

152

answers:

1

how can I capture response from twitter.com? To make sure that everything went ok?

I am using ruby and ruby twitter gem and the my code is basically like that

oauth = Twitter::OAuth.new('consumer token', 'consumer secret')
oauth.authorize_from_access('access token', 'access secret')

client = Twitter::Base.new(oauth)
client.update('Heeeyyyyoooo from Twitter Gem!')
+2  A: 

The update twitter api method will send back a response that will let you know if everything went okay. It can respond in either json or xml, I'm sure the twitter gem is using one or the other as a default. You need to save the return value to a variable and parse it, if you have a status id in there then it worked. Try using a token or secret to check what happens when it errors. I would suggest changing your last line to this

ret = client.update('Heeeyyyyoooo from Twitter Gem!')

and then add this line below that to check out what you got back

puts ret.inspect

or

logger.info ret.inspect

or your choice of logging method

[Edit] It looked like the twitter gem hides the twitter api's actual response from you, parses it for you and just returns you the relevant bits. in the case of the update method it just returns you the id of your new tweet. you can view the id like this

puts ret.id

If you use another library to connect to the twitter api and need to parse xml or json responses then then the rest of this answer may be what you are looking for. [/Edit]

If you are not using a gem that parses twitter api responses for you then you will need to use something to parse the twitter api's responses into data that you can do something with. There are tons of ways to do this depending on what format you want to parse (json or xml)

My preferences:

Here is more information on what the twitter api update method returns: http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses%C2%A0update

imightbeinatree at Cloudspace
@imightbeinatree: great answer!. `if you have a status id in there then it worked` is basically what I wanted to know. I have in the ret variable something like `"id"=>12555654648,` I guess that is what I am looking for. So if id exists and is not empty everything went fine, am I right?
Radek
actually twitter gem is pretty good. if I use `ret = client.update('Heeeyyyyoooo from Twitter Gem!')` then I can do `puts ret.id`. great isn't it. @imightbeinatree could you add that in your answer?
Radek
if the return you are getting from the twitter gem is just "id" => 1223123 then the gem is somewhat hiding the real response from the twitter api from you, parsing it for you and just returning you the relevant bits. i'll edit my answer above so that it says that.
imightbeinatree at Cloudspace
hi, what do you mean by `twitter api hides` ... I don't think it hides anything but parses. So instead of using Hpricot for parsing I can use only twitter gem. My opinion
Radek
I should have used a more technically correct term. That is what i meant by hides that the twitter gem does not show you the full response from the twitter api, it parses it and only gives you back the relevant parts. You do not need hpricot to parse the responses since the twitter gem is doing the parsing for you. I'll edit my answer a bit to make sure that is clear.
imightbeinatree at Cloudspace
do you think the gem doesn't pass everything?
Radek