Skip to content

Instantly share code, notes, and snippets.

@mono0x
Created March 6, 2012 13:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0x/1986179 to your computer and use it in GitHub Desktop.
Save mono0x/1986179 to your computer and use it in GitHub Desktop.
Open URI with default web browser
# -*- coding: utf-8 -*-
require 'uri'
class WebBrowser
def self.open(uri)
uri = URI.parse(uri.to_s)
unless %w[ http https ftp file ].include?(uri.scheme)
raise ArgumentError
end
case RUBY_PLATFORM
when /mswin(?!ce)|mingw|bccwin/
system %!start /B #{uri}!
when /cygwin/
system %!cmd /C start /B #{uri}!
when /darwin/
system %!open '#{uri}'!
when /linux/
system %!xdg-open '#{uri}'!
when /java/
require 'java'
import 'java.awt.Desktop'
import 'java.net.URI'
Desktop.getDesktop.browse java.net.URI.new(uri)
else
raise NotImplementedError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment