Class: Aspera::Cli::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/cli/result.rb,
lib/aspera/cli/result.rb

Overview

Special result types - each has its own class

Defined Under Namespace

Classes: Empty, Image, Nothing, Null, ObjectList, SingleObject, Special, Status, Success, Text, ValueList

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data: nil, fields: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • data (Object, nil) (defaults to: nil)

    The result data

  • fields (Object) (defaults to: nil)

    Specification of fields to include



25
26
27
28
# File 'lib/aspera/cli/result.rb', line 25

def initialize(data: nil, fields: nil)
  @data = data
  @fields = fields
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/aspera/cli/result.rb', line 21

def data
  @data
end

#fieldsObject (readonly)

Returns the value of attribute fields.



21
22
23
# File 'lib/aspera/cli/result.rb', line 21

def fields
  @fields
end

Class Method Details

.auto(data) ⇒ Result

Class method to automatically determine result type from data

Parameters:

  • data (Object)

    the data to analyze and format

Returns:



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/aspera/cli/result.rb', line 336

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])

    scalar_types = [String, Integer, Symbol]
    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 String, Integer, Symbol
    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.

Parameters:

  • items (Array)

    List of items to process (one element when non-bulk)

  • is_bulk (Boolean)

    Whether the caller is in bulk mode

  • command (Symbol)

    Operation name (:create, :delete, …) used to build the past-tense status string

  • id_result (String) (defaults to: 'id')

    Key in the result Hash used as item identifier

  • fields (Object) (defaults to: :default)

    Fields hint passed to the Result constructor (non-bulk only, when not :default)

  • bfail (Boolean) (defaults to: true)

    When true, re-raise errors; when false, capture them as a status string

Yield Parameters:

  • item (Object)

    Each item in items

Yield Returns:

  • (Hash, Array, nil)

    REST response; a Hash replaces the default id-keyed result

Returns:



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/aspera/cli/result.rb', line 305

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)
      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

Parameters:

  • formatter (Formatter)

    The formatter to use

Returns:

  • (nil)


38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/aspera/cli/result.rb', line 38

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 :text
    formatter.display_message(:data, @data.to_s)
  when :nagios
    Nagios.process(@data)
  when :ruby
    formatter.display_message(:data, PP.pp(filtered_data, +''))
  when :json
    formatter.display_message(:data, JSON.generate(filtered_data))
  when :jsonpp
    formatter.display_message(:data, JSON.pretty_generate(filtered_data))
  when :yaml
    formatter.display_message(:data, YAML.dump(filtered_data))
  when :image
    if @data.nil?
      formatter.display_message(: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.display_message(: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.display_message(:data, Preview::Terminal.build(data, **formatter.image_options))
    end
  else
    Aspera.error_unexpected_value(formatter.format_type){'format'}
  end
end