Class: ConsoleWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/mc/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ConsoleWriter

Returns a new instance of ConsoleWriter.



5
6
7
8
# File 'lib/mc/writer.rb', line 5

def initialize(options)
  @options = options
  puts "*** DEBUG ON ***" if @options[:debug]
end

Instance Method Details

#as_formatted(results, options = {}) ⇒ Object



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mc/writer.rb', line 44

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 = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# 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_table(results, options = {}) ⇒ Object



25
26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
# File 'lib/mc/writer.rb', line 32

def errors(results)
  puts "Error count: #{results['error_count']}"
  results['errors'].each do |error|
    puts error['error']
  end
end

#flat_hash(hash, k = []) ⇒ Object



39
40
41
42
# File 'lib/mc/writer.rb', line 39

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

#search(results) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mc/writer.rb', line 116

def search(results)
  redirect_output?(results)

  i = results
  if i['exact_matches']['total'].to_i == 0 and i['full_search']['total'].to_i == 0
    exit_now!("No matches found.")
  end

  members = []

  if i['exact_matches']['total'].to_i > 0
    puts "#{'='*20} Exact Matches (#{i['exact_matches']['total']}) #{'='*20}"

    i['exact_matches']['members'].each do |member|
      members << member
    end
  elsif i['full_search']['total'].to_i > 0
    puts "#{'='*26} Matches (#{i['full_search']['total']}) #{'='*26}"
    i['full_search']['members'].each do |member|
      members << member
    end
  end

  tp members, :email, :id, :member_rating, :status, "VIP?" => {:display_method => :is_gmonkey}
end

#standard(results, options = {}) ⇒ Object



10
11
12
13
# File 'lib/mc/writer.rb', line 10

def standard(results, options={})
  redirect_output? results, options
  as_table results, options
end