class ConsoleWriter

Public Class Methods

new(options) click to toggle source
# File lib/mc/writer.rb, line 5
def initialize(options)
  @options = options
  puts "*** DEBUG ON ***" if @options[:debug]
end

Public Instance Methods

as_formatted(results, options={}) click to toggle source
# File lib/mc/writer.rb, line 48
def as_formatted(results, options={})
  redirect_output? results, options

  # set default options
  options[:show_header] ||= true
  options[:show_index]  ||= false
  options[:debug]       ||= false

  fields = options[:fields]

  # find the correct staring level in the returned json
  if results.kind_of? Hash
    # array of hashes or just a single hash?
    if results["data"].nil?
      results = [results]
    else
      results = results["data"]
    end
  end

  if fields == :all
    fields = []
    results.first.each_key {|key| fields << key}
  end

  results.reverse! if options[:reverse]
  results = results[0..options[:limit]] if options[:limit]

  puts "Fields: #{[*fields].join(', ')}" unless fields.nil? || !options[:show_header]

  results.to_enum.with_index(1) do |item, index|
    if options[:debug] || fields.nil?
      puts "Number of items: #{results.count}"
      puts "Fields:"
      item.each do |k,v|
        if v.kind_of? Hash
          puts "#{k} ="
          v.each do |k,v|
            puts "     #{k} = #{v}"
          end
        elsif v.kind_of? Array
          puts "#{k} ="
          v.each do |i|
            if i.kind_of? Hash
              i.each do |k,v|
                puts "     #{k} = #{v}"
              end
            else
              puts "     #{i}"
            end
          end
        else
          puts "#{k} = #{v}"
        end
      end
      return
    else
      values_to_print = []
      [*fields].each do |field|
        if field.class == Hash
          values_to_print << item[field.keys.first.to_s][field.values.first.to_s]
        else
          values_to_print << item[field.to_s]
        end
      end

      print "#{pad(index, results.count)} - " if options[:show_index]
      puts  "#{values_to_print.join(' ')}"
    end
  end
end
as_hash(results, options={}) click to toggle source
# File lib/mc/writer.rb, line 15
def as_hash(results, options={})
  redirect_output? results, options

  if results.is_a? Hash and results.has_key? 'data'
    ap results['data'].first, options
  else
    ap results, options
  end
end
as_raw(results) click to toggle source
# File lib/mc/writer.rb, line 32
def as_raw(results)
  puts results
end
as_table(results, options={}) click to toggle source
# File lib/mc/writer.rb, line 25
def as_table(results, options={})
  redirect_output? results, options

  results.reverse! if options[:reverse]
  tp results, options[:fields]
end
errors(results) click to toggle source
# File lib/mc/writer.rb, line 36
def errors(results)
  puts "Error count: #{results['error_count']}"
  results['errors'].each do |error|
    puts error['error']
  end
end
flat_hash(hash, k = []) click to toggle source
# File lib/mc/writer.rb, line 43
def flat_hash(hash, k = [])
  return {k => hash} unless hash.is_a?(Hash)
  hash.inject({}){ |h, v| h.merge! flat_hash(v[-1], k + [v[0]]) }
end
standard(results, options={}) click to toggle source
# File lib/mc/writer.rb, line 10
def standard(results, options={})
  redirect_output? results, options
  as_table results, options
end

Private Instance Methods

debug(results) click to toggle source
# File lib/mc/writer.rb, line 184
def debug(results)
  puts "#{'*'*50}"
  puts results
  puts results.class
  puts "#{'*'*50}"
end
find_max(a, b) click to toggle source
# File lib/mc/writer.rb, line 180
def find_max(a, b)
  a > b ? a : b
end
pad(value, padding, max_length=25) click to toggle source
# File lib/mc/writer.rb, line 175
def pad(value, padding, max_length=25)
  value = value.to_s
  "#{value[0..max_length]}#{' '*(padding - value[0..max_length].size)}"
end
redirect_output?(results, options={}) click to toggle source
# File lib/mc/writer.rb, line 196
def redirect_output?(results, options={})
  if @options[:output]
    case @options[:output].to_sym
    when :table
      as_table results, options
      exit_now!('')
    when :formatted
      as_formatted results, options
      exit_now!('')
    when :raw
      as_raw results
      exit_now!('')
    when :awesome, :hash
      ap results
      exit_now!('')
    end
  end
end
show_member(m) click to toggle source
# File lib/mc/writer.rb, line 192
def show_member(m)
  puts "#{m['id']} - #{m['email']}: Rating=#{m['member_rating']}, List=#{m['list_name']}, VIP?=#{m['is_gmonkey']}"
end