Ruby Function to Check If We Are Currently Online
First, install it:
gem install onlineThen, you can use the online? global function:
require 'online'
if online?
# do some LIVE testing
endYou can also check a particular URL:
if online?(uri: 'https://api.github.com')
# GitHub API is accessible
endThe online?() function doesn't make an HTTP request on each call, but
instead caches the previous result for five minutes. You can change this
interval to three seconds, for example:
if online?(ttl: 3)
# We are online, let's run integration tests!
endYou can specify HTTP timeout (in seconds) to prevent hanging on slow connections, for example:
if online?(timeout: 2)
# We are online, if the timeout has not expired
endThat's it.
How to contribute
Read these guidelines. Make sure your build is green before you contribute your pull request. You will need to have Ruby 3.0+ and Bundler installed. Then:
bundle update
bundle exec rakeIf it's clean and you don't see any error messages, submit your pull request.