Module: Launchy

Defined in:
lib/launchy.rb,
lib/launchy/cli.rb,
lib/launchy/argv.rb,
lib/launchy/error.rb,
lib/launchy/detect.rb,
lib/launchy/version.rb,
lib/launchy/os_family.rb,
lib/launchy/deprecated.rb,
lib/launchy/application.rb,
lib/launchy/descendant_tracker.rb

Overview

The entry point into Launchy. This is the sole supported public API.

Launchy.open( uri, options = {} )

The currently defined global options are:

:debug        Turn on debugging output
:application  Explicitly state what application class is going to be used
:host_os      Explicitly state what host operating system to pretend to be
:ruby_engine  Explicitly state what ruby engine to pretend to be under
:dry_run      Do nothing and print the command that would be executed on $stdout

Other options may be used, and those will be passed directly to the application class

Defined Under Namespace

Modules: DescendantTracker, Detect, Version Classes: Application, ApplicationNotFoundError, ArgumentError, Argv, Browser, Cli, CommandNotFoundError, Error, OSFamily

Constant Summary collapse

VERSION =
"2.4.3"

Class Method Summary collapse

Class Method Details

.app_for_uri(uri) ⇒ Object



42
43
44
# File 'lib/launchy.rb', line 42

def app_for_uri( uri )
  Launchy::Application.handling( uri )
end

.app_for_uri_string(s) ⇒ Object



46
47
48
# File 'lib/launchy.rb', line 46

def app_for_uri_string( s )
  app_for_uri( string_to_uri( s ) )
end

.applicationObject



92
93
94
# File 'lib/launchy.rb', line 92

def application
  @application || ENV['LAUNCHY_APPLICATION']
end

.application=(app) ⇒ Object



88
89
90
# File 'lib/launchy.rb', line 88

def application=( app )
  @application = app
end

.bug_report_messageObject



120
121
122
# File 'lib/launchy.rb', line 120

def bug_report_message
  "Please rerun with environment variable LAUNCHY_DEBUG=true or the '-d' commandline option and file a bug at https://github.com/copiousfreetime/launchy/issues/new"
end

.debug=(d) ⇒ Object



78
79
80
# File 'lib/launchy.rb', line 78

def debug=( d )
  @debug = to_bool( d )
end

.debug?Boolean

we may do logging before a call to ‘open’, hence the need to check LAUNCHY_DEBUG here

Returns:

  • (Boolean)


84
85
86
# File 'lib/launchy.rb', line 84

def debug?
  @debug || to_bool( ENV['LAUNCHY_DEBUG'] )
end

.dry_run=(dry_run) ⇒ Object



112
113
114
# File 'lib/launchy.rb', line 112

def dry_run=( dry_run )
  @dry_run = to_bool( dry_run )
end

.dry_run?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/launchy.rb', line 116

def dry_run?
  @dry_run || to_bool( ENV['LAUNCHY_DRY_RUN'] )
end

.extract_global_options(options) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/launchy.rb', line 69

def extract_global_options( options )
  leftover = options.dup
  Launchy.debug        = leftover.delete( :debug       ) || ENV['LAUNCHY_DEBUG']
  Launchy.application  = leftover.delete( :application ) || ENV['LAUNCHY_APPLICATION']
  Launchy.host_os      = leftover.delete( :host_os     ) || ENV['LAUNCHY_HOST_OS']
  Launchy.ruby_engine  = leftover.delete( :ruby_engine ) || ENV['LAUNCHY_RUBY_ENGINE']
  Launchy.dry_run      = leftover.delete( :dry_run     ) || ENV['LAUNCHY_DRY_RUN']
end

.host_osObject



100
101
102
# File 'lib/launchy.rb', line 100

def host_os
  @host_os || ENV['LAUNCHY_HOST_OS']
end

.host_os=(host_os) ⇒ Object



96
97
98
# File 'lib/launchy.rb', line 96

def host_os=( host_os )
  @host_os = host_os
end

.log(msg) ⇒ Object



124
125
126
# File 'lib/launchy.rb', line 124

def log(msg)
  $stderr.puts "LAUNCHY_DEBUG: #{msg}" if Launchy.debug?
end

.open(uri_s, options = {}, &error_block) ⇒ Object

Launch an application for the given uri string



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/launchy.rb', line 25

def open(uri_s, options = {}, &error_block )
  leftover = extract_global_options( options )
  uri = string_to_uri( uri_s )
  app = app_for_uri( uri )
  app.new.open( uri, leftover )
rescue Launchy::Error => le
  raise le
rescue Exception => e
  msg = "Failure in opening uri #{uri_s.inspect} with options #{options.inspect}: #{e}"
  raise Launchy::Error, msg
ensure
  if $! and block_given? then
    yield $!
    return # explicitly swallow the errors
  end
end

.reset_global_optionsObject



61
62
63
64
65
66
67
# File 'lib/launchy.rb', line 61

def reset_global_options
  Launchy.debug       = false
  Launchy.application = nil
  Launchy.host_os     = nil
  Launchy.ruby_engine = nil
  Launchy.dry_run     = false
end

.ruby_engineObject



108
109
110
# File 'lib/launchy.rb', line 108

def ruby_engine
  @ruby_engine || ENV['LAUNCHY_RUBY_ENGINE']
end

.ruby_engine=(ruby_engine) ⇒ Object



104
105
106
# File 'lib/launchy.rb', line 104

def ruby_engine=( ruby_engine )
  @ruby_engine = ruby_engine
end

.string_to_uri(s) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/launchy.rb', line 50

def string_to_uri( s )
  uri = Addressable::URI.parse( s )
  Launchy.log "URI parsing pass 1 : #{s} -> #{uri.to_hash}"
  if not uri.scheme then
    uri = Addressable::URI.heuristic_parse( s )
    Launchy.log "URI parsing pass 2 : #{s} -> #{uri.to_hash}"
  end
  raise Launchy::ArgumentError, "Invalid URI given: #{s.inspect}" unless uri
  return uri
end