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.



50
51
52
# File 'lib/aspera/open_application.rb', line 50

def initialize
  @url_method = self.class.default_gui_mode
end

Instance Attribute Details

#url_methodObject

self



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

def url_method
  @url_method
end

Class Method Details

.default_gui_modeObject



19
20
21
22
23
24
# File 'lib/aspera/open_application.rb', line 19

def default_gui_mode
  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



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

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



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

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

.user_interfacesObject

User Interfaces



17
# File 'lib/aspera/open_application.rb', line 17

def user_interfaces; USER_INTERFACES; end

Instance Method Details

#uri(the_url) ⇒ Object

this is non blocking



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

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