Class: TermuxRubyApi::Base
- Inherits:
-
Object
- Object
- TermuxRubyApi::Base
- Defined in:
- lib/termux_ruby_api/base.rb
Instance Method Summary collapse
-
#api_command(command, stdin = nil, *args) ⇒ String
General executor of Termux API commands.
-
#call_log ⇒ TermuxRubyApi::SubSystems::CallLog
Returns the ‘call_log` subsystem.
-
#clipboard ⇒ TermuxRubyApi::SubSystems::Clipboard
Returns the ‘clipboard` subsystem.
-
#dialog ⇒ TermuxRubyApi::SubSystems::Dialog
Returns the ‘dialog` subsystem.
-
#generate_args(args) ⇒ Array?
Utility method to generate the arguments for a single command option If the last argument is blank, the whole list is ignored and returns nil.
-
#generate_args_list(args_list) ⇒ Array <String>
Utility method to generate args lists to be passed to ‘api_command` and the like The array is expected to contain Arrays as elements.
-
#json_api_command(command, stdin = nil, *args) ⇒ Object
Executes a Termux API command returning the results parsed as JSON.
-
#json_streamed_api_command(command, stdin = nil, *args, &block) ⇒ nil
Executes the Termux API command and streams its results as parsed JSON, as soon as JSON structures are complete.
-
#location ⇒ TermuxRubyApi::SubSystems::Location
Returns the ‘location` subsystem.
-
#sensor ⇒ TermuxRubyApi::SubSystems::Sensor
Returns the ‘sensor` subsystem.
-
#sms ⇒ TermuxRubyApi::SubSystems::Sms
Returns the ‘sms` subsystem.
-
#streamed_api_command(command, stdin = nil, *args, &block) ⇒ nil, [Io,Thread]
Executes a Termux API command and streams its results, line by line to the provided block If a block is given, each new line output by the executed command is yielded to the block If a block is not given, it returns the output stream and the wait thread.
-
#tts ⇒ TermuxRubyApi::SubSystems::Tts
Returns the ‘tts` subsystem.
Instance Method Details
#api_command(command, stdin = nil, *args) ⇒ String
General executor of Termux API commands
70 71 72 73 74 75 |
# File 'lib/termux_ruby_api/base.rb', line 70 def api_command(command, stdin = nil, *args) command, args = prepare_command_args(command, args) stdout, stderr, status = Open3.capture3(command, *args, stdin_data: stdin.to_s) raise CommandError.new(status: status, stderr: stderr) unless status.success? stdout end |
#call_log ⇒ TermuxRubyApi::SubSystems::CallLog
Returns the ‘call_log` subsystem
37 38 39 |
# File 'lib/termux_ruby_api/base.rb', line 37 def call_log @call_log ||= TermuxRubyApi::SubSystems::CallLog.new(self) end |
#clipboard ⇒ TermuxRubyApi::SubSystems::Clipboard
Returns the ‘clipboard` subsystem
25 26 27 |
# File 'lib/termux_ruby_api/base.rb', line 25 def clipboard @clipboard ||= TermuxRubyApi::SubSystems::Clipboard.new(self) end |
#dialog ⇒ TermuxRubyApi::SubSystems::Dialog
Returns the ‘dialog` subsystem
61 62 63 |
# File 'lib/termux_ruby_api/base.rb', line 61 def dialog @dialog ||= TermuxRubyApi::SubSystems::Dialog.new(self) end |
#generate_args(args) ⇒ Array?
Utility method to generate the arguments for a single command option If the last argument is blank, the whole list is ignored and returns nil
146 147 148 |
# File 'lib/termux_ruby_api/base.rb', line 146 def generate_args(args) args.empty? || args.last.blank? ? nil : args end |
#generate_args_list(args_list) ⇒ Array <String>
Utility method to generate args lists to be passed to ‘api_command` and the like The array is expected to contain Arrays as elements. Each of them is passed to #generate_args. Returns a flattened array with the resulting arguments.
132 133 134 |
# File 'lib/termux_ruby_api/base.rb', line 132 def generate_args_list(args_list) args = args_list.map { |args| generate_args(args) }.flatten.compact end |
#json_api_command(command, stdin = nil, *args) ⇒ Object
JSON objects are converted to ActiveSupport::HashWithIndifferentAccess
Executes a Termux API command returning the results parsed as JSON
81 82 83 84 85 |
# File 'lib/termux_ruby_api/base.rb', line 81 def json_api_command(command, stdin = nil, *args) res = api_command(command, stdin, *args) return res if res.nil? || res == '' JSON.parse(res, symbolize_names: true, object_class: ActiveSupport::HashWithIndifferentAccess) end |
#json_streamed_api_command(command, stdin = nil, *args, &block) ⇒ nil
Executes the Termux API command and streams its results as parsed JSON, as soon as JSON structures are complete. Only works if a block is given.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/termux_ruby_api/base.rb', line 112 def json_streamed_api_command(command, stdin = nil, *args, &block) partial_out = '' streamed_api_command(command, stdin, *args) do |line| partial_out << line begin parsed_json = JSON.parse(partial_out, symbolize_names: true, object_class: ActiveSupport::HashWithIndifferentAccess) partial_out = '' yield parsed_json rescue end end end |
#location ⇒ TermuxRubyApi::SubSystems::Location
Returns the ‘location` subsystem
43 44 45 |
# File 'lib/termux_ruby_api/base.rb', line 43 def location @location ||= TermuxRubyApi::SubSystems::Location.new(self) end |
#sensor ⇒ TermuxRubyApi::SubSystems::Sensor
Returns the ‘sensor` subsystem
55 56 57 |
# File 'lib/termux_ruby_api/base.rb', line 55 def sensor @sensor ||= TermuxRubyApi::SubSystems::Sensor.new(self) end |
#sms ⇒ TermuxRubyApi::SubSystems::Sms
Returns the ‘sms` subsystem
49 50 51 |
# File 'lib/termux_ruby_api/base.rb', line 49 def sms @sms ||= TermuxRubyApi::SubSystems::Sms.new(self) end |
#streamed_api_command(command, stdin = nil, *args, &block) ⇒ nil, [Io,Thread]
Executes a Termux API command and streams its results, line by line to the provided block If a block is given, each new line output by the executed command is yielded to the block If a block is not given, it returns the output stream and the wait thread.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/termux_ruby_api/base.rb', line 92 def streamed_api_command(command, stdin = nil, *args, &block) command, args = prepare_command_args(command, args) i, o, t = Open3.popen2(command, *args) i.puts(stdin) unless stdin.blank? # If we have any input, send it to the child i.close # Afterwards we can close child's stdin if block_given? o.each_line do |line| yield line end o.close raise "#{command} failed" unless t.value.success? else return o, t # The caller has to close o and wait for t end end |
#tts ⇒ TermuxRubyApi::SubSystems::Tts
Returns the ‘tts` subsystem
31 32 33 |
# File 'lib/termux_ruby_api/base.rb', line 31 def tts @tts ||= TermuxRubyApi::SubSystems::Tts.new(self) end |