Sha256: 3f487f27e2a2e183ebf34244b4acaf9f03fc06ee98c45a7456b313f6de8a7fa2

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'launchy/spawnable/application'
require 'uri'

module Launchy
    module Spawnable
        class Browser < Application
            APP_LIST = { 
                :windows => %w[ firefox iexplore ],
                :darwin  => %w[ open ],
                :nix     => %w[ firefox ],
                :unknown => [],
                }
            class << self
                def run(*args)
                    Browser.new.visit(args[0]) 
                end
                
                # return true if this class can handle the given parameter(s)
                def handle?(*args)
                    begin 
                        uri = URI.parse(args[0])
                        return [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
                    rescue Exception 
                        return false
                    end
                end
            end
            
            def initialize
                raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
            end
            
            # returns the list of command line application names for the current os
            def app_list
                APP_LIST[my_os_family]
            end
            
            # return the full command line path to the browser or nil
            def browser
                @browser ||= app_list.collect { |bin| find_executable(bin) }.reject { |x| x.nil? }.first
            end
            
            # launch the browser at the appointed url
            def visit(url)
                run(browser,url)
            end
            
        end
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
launchy-0.1.0 lib/launchy/spawnable/browser.rb
launchy-0.1.1 lib/launchy/spawnable/browser.rb
launchy-0.1.2 lib/launchy/spawnable/browser.rb