Class: Aspera::OpenApplication

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/open_application.rb

Overview

Allows a user to open a Url if method is “text”, then URL is displayed on terminal if method is “graphical”, then the URL will be opened with the default browser.

Constant Summary collapse

USER_INTERFACES =
%i[text graphical].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpenApplication

Returns a new instance of OpenApplication.



48
49
50
# File 'lib/aspera/open_application.rb', line 48

def initialize
  @url_method = self.class.default_gui_mode
end

Instance Attribute Details

#url_methodObject

Returns the value of attribute url_method.



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

def url_method
  @url_method
end

Class Method Details

.default_gui_modeObject



16
17
18
19
20
21
22
# File 'lib/aspera/open_application.rb', line 16

def default_gui_mode
  # assume not remotely connected on macos and windows
  return :graphical if [Environment::OS_WINDOWS, Environment::OS_X].include?(Environment.os)
  # unix family
  return :graphical if ENV.key?('DISPLAY') && !ENV['DISPLAY'].empty?
  return :text
end

.editor(file_path) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/aspera/open_application.rb', line 35

def editor(file_path)
  if ENV.key?('EDITOR')
    system(ENV['EDITOR'], file_path.to_s)
  elsif Environment.os.eql?(Environment::OS_WINDOWS)
    system('notepad.exe', %Q{"#{file_path}"})
  else
    uri_graphical(file_path.to_s)
  end
end

.uri_graphical(uri) ⇒ Object

command must be non blocking



25
26
27
28
29
30
31
32
33
# File 'lib/aspera/open_application.rb', line 25

def uri_graphical(uri)
  case Environment.os
  when Environment::OS_X       then return system('open', uri.to_s)
  when Environment::OS_WINDOWS then return system('start', 'explorer', %Q{"#{uri}"})
  when Environment::OS_LINUX   then return system('xdg-open', uri.to_s)
  else
    raise "no graphical open method for #{Environment.os}"
  end
end

Instance Method Details

#uri(the_url) ⇒ Object

this is non blocking



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aspera/open_application.rb', line 53

def uri(the_url)
  case @url_method
  when :graphical
    self.class.uri_graphical(the_url)
  when :text
    case the_url.to_s
    when /^http/
      puts "USER ACTION: please enter this url in a browser:\n#{the_url.to_s.red}\n"
    else
      puts "USER ACTION: open this:\n#{the_url.to_s.red}\n"
    end
  else
    raise StandardError, "unsupported url open method: #{@url_method}"
  end
end