Class: Aspera::Cli::Result
- Inherits:
-
Object
- Object
- Aspera::Cli::Result
- 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
-
#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.
Instance Method Summary collapse
-
#format(formatter) ⇒ void
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.
Constructor Details
#initialize(data: nil, fields: nil) ⇒ Result
Returns a new instance of Result.
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/aspera/cli/result.rb', line 21 def data @data end |
#fields ⇒ Object (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
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/aspera/cli/result.rb', line 286 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 |
Instance Method Details
#format(formatter) ⇒ void
This method returns an undefined value.
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
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 |
# 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.(:data, @data.to_s) when :nagios Nagios.process(@data) when :ruby formatter.(:data, PP.pp(filtered_data, +'')) when :json formatter.(:data, JSON.generate(filtered_data)) when :jsonpp formatter.(:data, JSON.pretty_generate(filtered_data)) when :yaml formatter.(:data, YAML.dump(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 # try base64 data = begin Base64.strict_decode64(data) rescue data end # here, data is the image blob formatter.(:data, Preview::Terminal.build(data, **formatter.)) unless data.eql?(:done) else Aspera.error_unexpected_value(formatter.format_type){'format'} end end |