Class: Latestver::CliHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/latestver/cli_helpers.rb

Class Method Summary collapse

Class Method Details

.output(options, data) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/latestver/cli_helpers.rb', line 4

def self.output(options, data)
  data_out = data

  unless data_out.is_a? Exception

    if options[:select]
      s = options[:select].split('.')
      while s.length > 0 and not data_out.nil?
        data_out = data_out[s.shift]
      end
    end

    return nil unless data_out

  end

  case options[:output]
    when 'json'
      if data_out.is_a? String
        data_out = {
            value: data_out,
            error: '',
        }
      elsif data_out.is_a? Exception
        data_out = {
            error: data_out.message,
            value: '',
        }
      end
      JSON.pretty_generate(data_out)
    else
      if data_out.is_a? Exception
        "ERROR: #{data_out.message}"
      else
        data_out
      end
  end
end