Created
February 24, 2011 15:17
-
-
Save micahalles/842276 to your computer and use it in GitHub Desktop.
fire poll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FirePoll | |
# @param [String] msg a custom message raised when polling fails | |
# @param [Numeric] seconds number of seconds to poll | |
# @yield a block that determines whether polling should continue | |
# @yieldreturn false if polling should continue | |
# @yieldreturn true if polling is complete | |
# @raise [RuntimeError] when polling fails | |
# @return [void] | |
# @since 1.0.0 | |
def poll(msg=nil, seconds=2.0) | |
(seconds * 10).to_i.times do | |
return if yield | |
sleep 0.1 | |
end | |
msg ||= "polling failed after #{seconds} seconds" | |
raise msg | |
end | |
module_function :poll | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment