Restclient error handling (newbie question)

279 views
Skip to first unread message

Lindsey Mysse

unread,
Dec 26, 2010, 3:44:40 PM12/26/10
to boston-r...@googlegroups.com
Well, happy SNOWMG 2010-11, Rubyists.

So I just wrote a little command line Ruby script that solves a
problem I have-- how to upload a directory of .jpgs on your computer
into your tumblr queue (every other ruby tumblr tool can’t handle
images or queuing, apparently).

It’s been a fun to play around with rest-client for the first time and
write some ruby.

So I got it to work (except when I hit Tumblr’s undocumented daily
upload limit) -- but I would like to add proper error correction.

RestClient is great at sending errors when something goes wrong-- I
just want to capture that output and make it into something cleaner
and not have my program error out. I have tried lots of things in my
little program, but I’m at a newbie impasse on how to properly apply
rescue to something that looks like this:

RestClient.post weburl,{
:email => email,
:password => password,
:type => "photo",
:data => File.new(a),
:group => blog,
:state => "queue",
}

The Rest-Client docs suggest:

# Manage a specific error code
RestClient.get('http://my-rest-service.com/resource'){ |response,
request, result, &block|
case response.code
when 200
p "It worked !"
response
when 423
raise SomeCustomExceptionIfYouWant
else
response.return!(request, result, &block)
end
}

But that doesn't factor into the info I am sending to Tumblr first--
when I try to add something like this to by RestClient.post statement
I get syntax error dumps left and right.

This is one of those things that I can stare at the restclient
documentation all day and kind of KNOW what they are saying to me, but
don't understand it enough to apply it to a different situation.

How do I capture the output data from this RestClient.post statement?

Any other critiques of my little script/random flame mails would be
appreciated. Stay warm.

(Full code Follows)
Lindsey

#!/usr/bin/env ruby
require "restclient"

# check to see if there are enough inputs
unless ARGV.length == 3
puts 'Usage: ruby addToQ.rb email password your_blog_address_on_tumblr'
exit
end


#assign the variables
weburl = "http://www.tumblr.com/api/write"
email = ARGV[0]
password = ARGV[1]
blog = ARGV[2]

#upload the files
Dir.glob("*.jpg").each do|a|

puts "uploading #{a}"

#the post code

RestClient.post weburl,{
:email => email,
:password => password,
:type => "photo",
:data => File.new(a),
:group => blog,
:state => "queue",
}


end

Reply all
Reply to author
Forward
0 new messages