Class: Launchy::Browser

Inherits:
Application show all
Defined in:
lib/gems/launchy-0.3.2/lib/launchy/browser.rb

Constant Summary collapse

DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS =
{
    :kde     => "kfmclient",
    :gnome   => "gnome-open",
    :xfce    => "exo-open",
    :generic => "htmlview"
}
FALLBACK_BROWSERS =
%w[ firefox seamonkey opera mozilla netscape galeon ]

Constants inherited from Application

Application::KNOWN_OS_FAMILIES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Application

#app_list, application_classes, #cygwin_app_list, #darwin_app_list, find_application_class_for, find_executable, #find_executable, inherited, #my_os, my_os, my_os_family, #my_os_family, #nix_desktop_environment, #run, #windows_app_list

Constructor Details

#initializeBrowser

Returns a new instance of Browser.



35
36
37
# File 'lib/gems/launchy-0.3.2/lib/launchy/browser.rb', line 35

def initialize
    raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
end

Class Method Details

.handle?(*args) ⇒ Boolean

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

Returns:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gems/launchy-0.3.2/lib/launchy/browser.rb', line 22

def handle?(*args)
    begin 
        Launchy.log "#{self.name} : testing if [#{args[0]}] (#{args[0].class}) is a url."
        uri = URI.parse(args[0])
        result =  [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
    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

.run(*args) ⇒ Object



17
18
19
# File 'lib/gems/launchy-0.3.2/lib/launchy/browser.rb', line 17

def run(*args)
    Browser.new.visit(args[0]) 
end

Instance Method Details

#browserObject

return the full command line path to the browser or nil



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gems/launchy-0.3.2/lib/launchy/browser.rb', line 60

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

#nix_app_listObject

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


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gems/launchy-0.3.2/lib/launchy/browser.rb', line 45

def nix_app_list
    if not @nix_app_list then
        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) ⇒ Object

launch the browser at the appointed url



81
82
83
# File 'lib/gems/launchy-0.3.2/lib/launchy/browser.rb', line 81

def visit(url)
    run(browser,url)
end