Class: Aspera::Cli::Result
- Inherits:
-
Object
- Object
- Aspera::Cli::Result
- Defined in:
- lib/aspera/cli/result.rb,
lib/aspera/cli/poll.rb,
lib/aspera/cli/result.rb
Overview
Special result types - each has its own class
Defined Under Namespace
Classes: Empty, Image, Nothing, Null, ObjectList, Poll, SingleObject, Special, Status, Success, Text, ValueList
Constant Summary collapse
- STRUCTURED_FORMATS =
Formats rendering the data structure itself
%i[ruby json jsonpp yaml].freeze
- SCALAR_TYPES =
Types of a single value result (Text), or of items of a value list
[String, Integer, Float, Symbol, TrueClass, FalseClass].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
-
.auto(data) ⇒ Result
Class method to automatically determine result type from data.
-
.bulk(items, is_bulk:, command:, id_result: 'id', fields: :default, bfail: true) {|item| ... } ⇒ Result::ObjectList, Result::SingleObject
Execute a bulk operation over a list of already-resolved items and return a CLI result.
Instance Method Summary collapse
-
#format(formatter) ⇒ nil
Format this result using the provided formatter This method implements the Visitor pattern, allowing each Result subclass to define how it should be formatted without the Formatter needing to know about all Result types.
-
#initialize(data: nil, fields: nil) ⇒ Result
constructor
A new instance of Result.
-
#with_data(data) ⇒ Result
Copy of this result, with other data (e.g. with secrets hidden).
Constructor Details
#initialize(data: nil, fields: nil) ⇒ Result
Returns a new instance of Result.
29 30 31 32 33 34 |
# File 'lib/aspera/cli/result.rb', line 29 def initialize(data: nil, fields: nil) Aspera.assert_type(fields, NilClass, Array, Proc) Aspera.assert_array_all(fields, String) if fields.is_a?(Array) @data = data @fields = fields end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
25 26 27 |
# File 'lib/aspera/cli/result.rb', line 25 def data @data end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
25 26 27 |
# File 'lib/aspera/cli/result.rb', line 25 def fields @fields end |
Class Method Details
.auto(data) ⇒ Result
Class method to automatically determine result type from data
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/aspera/cli/result.rb', line 362 def auto(data) case data when NilClass Null.new when Hash SingleObject.new(data) when Array all_types = data.map(&:class).uniq return ObjectList.new(data) if all_types.eql?([Hash]) unsupported_types = all_types - SCALAR_TYPES return ValueList.new(data, name: 'list') if unsupported_types.empty? Aspera.error_unexpected_value(unsupported_types) { 'list item types' } when *SCALAR_TYPES Text.new(data) else Aspera.error_unexpected_value(data.class.name) { 'result type' } end end |
.bulk(items, is_bulk:, command:, id_result: 'id', fields: :default, bfail: true) {|item| ... } ⇒ Result::ObjectList, Result::SingleObject
Execute a bulk operation over a list of already-resolved items and return a CLI result. This is a pure helper: it reads nothing from the CLI.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/aspera/cli/result.rb', line 329 def bulk(items, is_bulk:, command:, id_result: 'id', fields: :default, bfail: true) Aspera.assert(block_given?, 'missing block') Log.log.warn('Empty list given for bulk operation') if items.empty? Log.dump(:bulk_operation, items) result_list = [] items.each do |item| result = {id_result => item} begin res = yield(item) result = res if res.is_a?(Hash) # TODO: remove when faspio gw api fixes this result = res.first if res.is_a?(Array) && res.first.is_a?(Hash) # Ensure the id_result key is always present even when the API response uses a different key name result[id_result] = item unless result.key?(id_result) result['status'] = "#{command}#{'e' unless command.to_s.end_with?('e')}d".gsub(/yed$/, 'ied') rescue StandardError => e raise e if bfail result['status'] = e.to_s end result_list.push(result) end display_fields = [id_result, 'status'] if is_bulk return ObjectList.new(result_list, fields: display_fields) else display_fields = fields unless fields.eql?(:default) return SingleObject.new(result_list.first, fields: display_fields) end end |
Instance Method Details
#format(formatter) ⇒ nil
Format this result using the provided formatter This method implements the Visitor pattern, allowing each Result subclass to define how it should be formatted without the Formatter needing to know about all Result types. Base implementation handles common formats: text, nagios, ruby, json, jsonpp, yaml Subclasses should call super first, then handle their specific formats
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/aspera/cli/result.rb', line 50 def format(formatter) # Apply field filtering once for formats that need it filtered_data = formatter.filter_list_on_fields(@data) case formatter.format_type when :nagios then Nagios.process(@data) when :text then formatter.(:data, @data.to_s) when *STRUCTURED_FORMATS then display_structured(formatter, filtered_data) when :image if @data.nil? formatter.(:data, formatter.special_format('null (no image)')) return end Aspera.assert_type(@data, String) { 'image: URL or blob' } # Check if URL data = begin # just validate URI.parse(@data) if Environment.instance.url_method.eql?(:text) UriReader.read(@data) else Environment.instance.open_uri(@data) formatter.(:info, "Opened URL in browser: #{@data}") :done end rescue URI::InvalidURIError @data end unless data.eql?(:done) # try base64 data = begin Base64.strict_decode64(data) rescue ArgumentError data end # here, data is the image blob formatter.(:data, TerminalImage.build(data, **formatter.)) end else Aspera.error_unexpected_value(formatter.format_type) { 'format' } end end |
#with_data(data) ⇒ Result
Returns Copy of this result, with other data (e.g. with secrets hidden).
38 39 40 |
# File 'lib/aspera/cli/result.rb', line 38 def with_data(data) dup.tap { |r| r.data = data } end |