class Launchy::Browser

Public Class Methods

desktop_environment_browser_launchers() click to toggle source
# File lib/launchy/browser.rb, line 9
def desktop_environment_browser_launchers
  @desktop_environment_browser_launchers ||= {
    :kde     => "kfmclient",
    :gnome   => "gnome-open",
    :xfce    => "exo-open",
    :generic => "htmlview"
  }
end
fallback_browsers() click to toggle source
# File lib/launchy/browser.rb, line 17
def fallback_browsers
  @fallback_browsers ||=  %w[ firefox seamonkey opera mozilla netscape galeon ]
end
handle?(*args) click to toggle source

return true if this class can handle the given parameter(s)

# File lib/launchy/browser.rb, line 25
def handle?(*args)
  begin
    Launchy.log "#{self.name} : testing if [#{args[0]}] (#{args[0].class}) is a url."
    uri = URI.parse(args[0])
    result =  %w[http https ftp file].include?(uri.scheme)
  rescue Exception => e
    # hmm... why does rcov not see that this is executed ?
    Launchy.log "#{self.name} : not a url, #{e}"
    return false
  end
end
new() click to toggle source
# File lib/launchy/browser.rb, line 38
def initialize
  @browser = nil
  @nix_app_list = []
  raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
end
run(*args) click to toggle source
# File lib/launchy/browser.rb, line 20
def run(*args)
  Browser.new.visit(args[0])
end

Public Instance Methods

browser() click to toggle source

return the full command line path to the browser or nil

# File lib/launchy/browser.rb, line 73
def browser
  if not @browser then
    if ENV['LAUNCHY_BROWSER'] and File.exists?(ENV['LAUNCHY_BROWSER']) then
      Launchy.log "#{self.class.name} : Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
      @browser = ENV['LAUNCHY_BROWSER']
    elsif ENV['BROWSER'] and File.exists?(ENV['BROWSER']) then
      Launchy.log "#{self.class.name} : Using BROWSER environment variable : #{ENV['BROWSER']}"
      @browser = ENV['BROWSER']
    elsif app_list.size > 0 then
      @browser = app_list.first
      Launchy.log "#{self.class.name} : Using application list : #{@browser}"
    else
      msg = "Unable to launch. No Browser application found."
      Launchy.log "#{self.class.name} : #{msg}"
      $stderr.puts msg
    end
  end
  return @browser
end
desktop_environment_browser_launchers() click to toggle source
# File lib/launchy/browser.rb, line 44
def desktop_environment_browser_launchers
  self.class.desktop_environment_browser_launchers
end
fallback_browsers() click to toggle source
# File lib/launchy/browser.rb, line 48
def fallback_browsers
  self.class.fallback_browsers
end
nix_app_list() click to toggle source

Find a list of potential browser applications to run on *nix machines. The order is:

1) What is in ENV['LAUNCHY_BROWSER'] or ENV['BROWSER']
2) xdg-open
3) desktop environment launcher program
4) a list of fallback browsers
# File lib/launchy/browser.rb, line 58
def nix_app_list
  if @nix_app_list.empty?
    browser_cmds = ['xdg-open']
    browser_cmds << desktop_environment_browser_launchers[nix_desktop_environment]
    browser_cmds << fallback_browsers
    browser_cmds.flatten!
    browser_cmds.delete_if { |b| b.nil? || (b.strip.size == 0) }
    Launchy.log "#{self.class.name} : Initial *Nix Browser List: #{browser_cmds.join(', ')}"
    @nix_app_list = browser_cmds.collect { |bin| find_executable(bin) }.find_all { |x| not x.nil? }
    Launchy.log "#{self.class.name} : Filtered *Nix Browser List: #{@nix_app_list.join(', ')}"
  end
  @nix_app_list
end
visit(url) click to toggle source

launch the browser at the appointed url

# File lib/launchy/browser.rb, line 94
def visit(url)
  run(browser,url)
end