0
\$\begingroup\$

I want to keep trying to get response until its code is 200 or unknown yet. In first case it should be stored in response variable. In another case I should raise any kind of exception.

 response = nil
 1.times do
 response = begin
 http.request request
 rescue Net::ReadTimeout
 puts "Net::ReadTimeout"
 retry
 end
 case response.code
 when "503"
 puts "servers are busy at #{Time.now}?"
 sleep 5
 redo
 when "200"
 "ok"
 else
 fail "#{response.code} at '#{request.path}'"
 end
 end

The 1.times thing is taken from SO.

asked Jun 15, 2015 at 22:34
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The 1.times ... redo thing is just awful. Replace that with an until loop, like this:

success = false
until success
 ...
 case response.code
 ...
 when "200"
 success = true
 "ok"
 else
 ...
 end 
end
answered Jun 16, 2015 at 0:03
\$\endgroup\$
4
  • \$\begingroup\$ Too imperative. \$\endgroup\$ Commented Jun 16, 2015 at 1:38
  • 2
    \$\begingroup\$ Ok, you didn't say anything about needing a functional pattern in your question. And besides, your original is already imperative. What exactly are you looking for? \$\endgroup\$ Commented Jun 16, 2015 at 1:49
  • \$\begingroup\$ Yes, original is imperative -- this is why I posted it to ask how to make it more Rubyish. \$\endgroup\$ Commented Jun 16, 2015 at 4:22
  • 4
    \$\begingroup\$ There's nothing inherently 'un-Ruby' about imperative solutions. \$\endgroup\$ Commented Jun 16, 2015 at 13:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.