Skip to content

yegor256/online

Repository files navigation

Ruby Function to Check If We Are Currently Online

DevOps By Rultor.com We recommend RubyMine

rake PDD status Gem Version Test Coverage Yard Docs Hits-of-Code License

First, install it:

gem install online

Then, you can use the online? global function:

require 'online'
if online?
  # do some LIVE testing
end

You can also check a particular URL:

if online?(uri: 'https://api.github.com')
  # GitHub API is accessible
end

The 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!
end

You 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
end

That'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 rake

If it's clean and you don't see any error messages, submit your pull request.

About

Ruby function to check whether we are online now (useful for unit tests)

Topics

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •  

Languages