Module: Registrable::InstanceMethods

Defined in:
lib/rviki/registrable.rb

Instance Method Summary collapse

Instance Method Details

#execute(*args) ⇒ Object



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
91
# File 'lib/rviki/registrable.rb', line 60

def execute(*args)
  prepare_printer

  @cmd = args.shift
  @cmd = @cmd.to_sym if @cmd
  unless valid_command?
    puts <<-EOL

Command not found: #{@cmd}
----------------------------------------

RViki Version #{RViki::VERSION::STRING} for Viki API #{self.class.api_version}
Usage: #{self.class.binary_name} <api_endpoint> [--opt optvalue] [api_param1] [api_param2]

Available API Endpoints:
#{commands_listing}
#{print_opts_parser}

Examples:

#{self.class.examples}

EOL
    exit(1)
  end

  @cmd_args = self.class.registered_commands[@cmd]
  extract_options args
  response = client.send(@cmd, client_options_hash)
  printer.object = response.parsed_response
  printer.do_print
end

#prepare_printerObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rviki/registrable.rb', line 40

def prepare_printer
  @printer = RViki::Printer.new([:stdout])

  @print_opts_parser = OptionParser.new do |pop|
    pop.banner = "Print Options:"
    pop.on("-f", "--format FORMAT", [:tabular, :pretty_json, :pretty_ruby],
           "Print with FORMAT (tabular (only for array), pretty_json, or pretty_ruby)") do |pf|
      printer.format = pf
    end

    pop.on("-o", "--output clipboard,stdout", Array,
           "Output to OUTPUT_TARGET (clipboard, stdout)") do |o|
      o = o.map(&:to_sym)
      if (o.include?(:clipboard) || o.include?(:stdout))
        printer.targets = o
      end
    end
  end
end