class Apipie::Client::CliCommand

Public Instance Methods

client() click to toggle source
# File lib/apipie/client/cli_command.rb, line 4
def client
  resource_class = apipie_options[:client_module]::Resources.const_get(self.class.name[%r[^:]*$/])
  @client        ||= resource_class.new(apipie_options[:config])
end
normalize_item_data(item) click to toggle source

unifies the data for further processing. e.g.

{ "user" => {"username" => "test", "password" => "changeme" }

becomes:

{ "username" => "test", "password" => "changeme" }
# File lib/apipie/client/cli_command.rb, line 41
def normalize_item_data(item)
  if item.size == 1 && item.values.first.is_a?(Hash)
    item.values.first
  else
    item
  end
end
print_big_table(table, options={ }) click to toggle source
print_data(data) click to toggle source
print_unknown(data) click to toggle source
table_from_array(data) click to toggle source
# File lib/apipie/client/cli_command.rb, line 49
def table_from_array(data)
  return [] if data.empty?
  table   = []
  items   = data.map { |item| normalize_item_data(item) }
  columns = items.first.keys
  table << columns
  items.each do |item|
    row = columns.map { |c| item[c] }
    table << row.map(&:to_s)
  end
  return table
end
table_from_hash(data) click to toggle source
# File lib/apipie/client/cli_command.rb, line 62
def table_from_hash(data)
  return [] if data.empty?
  table = []
  normalize_item_data(data).each do |k, v|
    table << ["#{k}:", v].map(&:to_s)
  end
  table
end
transform_options(inline_params, transform_hash = { }) click to toggle source
# File lib/apipie/client/cli_command.rb, line 9
def transform_options(inline_params, transform_hash = { })
  # we use not mentioned params without change
  transformed_options = (options.keys - transform_hash.values.flatten - inline_params).reduce({ }) { |h, k| h.update(k => options[k]) }

  inline_params.each { |p| transformed_options[p] = options[p] }

  transform_hash.each do |sub_key, params|
    transformed_options[sub_key] = { }
    params.each { |p| transformed_options[sub_key][p] = options[p] if options.has_key?(p) }
  end

  return transformed_options
end

Public Class Methods

banner(task, namespace = nil, subcommand = false) click to toggle source
help(shell, subcommand = true) click to toggle source
# File lib/apipie/client/cli_command.rb, line 110
def help(shell, subcommand = true)
  list = self.printable_tasks(true, subcommand)
  Thor::Util.thor_classes_in(self).each do |klass|
    list += printable_tasks(false)
  end
  list.sort! { |a, b| a[0] <=> b[0] }

  shell.say
  shell.print_table(list, :indent => 2, :truncate => true)
  shell.say
  Thor.send(:class_options_help, shell)
end