Class: SakaiInfo::CLI::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/sakai-info/cli/lookup.rb

Class Method Summary collapse

Class Method Details

.process(args, flags) ⇒ Object



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
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
# File 'lib/sakai-info/cli/lookup.rb', line 15

def self.process(args, flags)
  object_type = args.shift
  id = args.shift
  fields = nil
  output = :yaml

  flags_to_delete = []
  flags.each do |flag|
    if flag =~ /^--fields=(.+)$/
      fields = $1.downcase.split(',')
      flags_to_delete << flag
    elsif flag == "--json"
      output = :json
      flags_to_delete << flag
    end
  end

  flags_to_delete.each do |flag|
    flags.delete(flag)
  end

  if flags.include? "--all"
    serials = CLI::LookupModes[object_type].all_serializations
  elsif flags.include? "--dbrow-only"
    serials = [:dbrow_only]
  else
    serials = [:default] + flags.collect{|flag|flag.gsub(/^--/,'').gsub("-","_").to_sym}
  end

  object = nil
  begin
    object = CLI::LookupModes[object_type].find(id)
  rescue ObjectNotFoundException => e
    STDERR.puts "ERROR: #{e}"
    exit 1
  end

  if fields.nil? or fields.empty?
    if output == :json
      puts object.to_json(serials)
    else
      puts object.to_yaml(serials)
    end

  else
    object_hash = object.serialize(serials).delete_if{|k,v| not fields.include? k.to_s}
    if object_hash.empty?
      STDERR.puts "ERROR: no requested fields were found"
      exit 1
    end
    if output == :json
      puts object_hash.to_json
    else
      puts object_hash.to_yaml
    end
  end
end