Class: ConsoleWriter
- Inherits:
-
Object
- Object
- ConsoleWriter
- Defined in:
- lib/mc/writer.rb
Instance Method Summary collapse
- #as_formatted(results, options = {}) ⇒ Object
- #as_hash(results, options = {}) ⇒ Object
- #as_raw(results) ⇒ Object
- #as_table(results, options = {}) ⇒ Object
- #campaign_search(results) ⇒ Object
- #errors(results) ⇒ Object
- #flat_hash(hash, k = []) ⇒ Object
-
#initialize(options) ⇒ ConsoleWriter
constructor
A new instance of ConsoleWriter.
- #member_search(exact, full, options = nil) ⇒ Object
- #standard(results, options = {}) ⇒ Object
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 = puts "*** DEBUG ON ***" if @options[:debug] end |
Instance Method Details
#as_formatted(results, options = {}) ⇒ Object
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 115 116 117 118 |
# File 'lib/mc/writer.rb', line 48 def as_formatted(results, ={}) redirect_output? results, # set default options [:show_header] ||= true [:show_index] ||= false [:debug] ||= false fields = [: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 [:reverse] results = results[0..[:limit]] if [:limit] puts "Fields: #{[*fields].join(', ')}" unless fields.nil? || ![:show_header] results.to_enum.with_index(1) do |item, index| if [: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 [: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, ={}) redirect_output? results, if results.is_a? Hash and results.has_key? 'data' ap results['data'].first, else ap results, end end |
#as_raw(results) ⇒ Object
32 33 34 |
# File 'lib/mc/writer.rb', line 32 def as_raw(results) puts results end |
#as_table(results, options = {}) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/mc/writer.rb', line 25 def as_table(results, ={}) redirect_output? results, results.reverse! if [:reverse] tp results, [:fields] end |
#campaign_search(results) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mc/writer.rb', line 120 def campaign_search(results) if results['total'].to_i == 0 exit_now!("No matches found.") end results['results'].each_with_index do |result, index| puts "#{'-'*15} [#{index+1}] #{'-'*15}" puts result['snippet'].strip.gsub(/\n/, ' ') puts "From campaign \"#{result['campaign']['title']}\" (#{result['campaign']['id']}) that was sent on #{result['campaign']['send_time'].split.first}" puts "\n" end end |
#errors(results) ⇒ Object
36 37 38 39 40 41 |
# 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 = []) ⇒ Object
43 44 45 46 |
# 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 |
#member_search(exact, full, options = nil) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mc/writer.rb', line 133 def member_search(exact, full, =nil) redirect_output?(full, ) if exact.count == 0 and full.count == 0 exit_now!("No matches found.") end members = [] if exact.count > 0 puts "#{'='*20} Exact Matches (#{exact.count}) #{'='*20}" exact.each do |member| members << member end tp members, :email, :id, :list_name, :member_rating, :status, "VIP?" => {:display_method => :is_gmonkey} elsif full.count > 0 max_email = 0 max_name = 0 full.each do |member| members << member max_email = find_max(member['email'].size, max_email) max_name = find_max(member['list_name'].size, max_name) end #display the table header header = "#{pad('EMAIL', max_email, 40)} | ID | #{pad('LIST NAME', max_name, 20)} | MEMBER RATING | #{pad('STATUS', 12)} | VIP?" puts "#{'='*(header.size/2 - 7)} Matches (#{full.count}) #{'='*(header.size/2 - 7)}" header += "\n#{'-'*header.size}" puts header #display member rows members.each do |m| puts "#{pad(m['email'], max_email, 40)} | #{m['id']} | #{pad(m['list_name'], max_name, 20)} | #{pad(m['member_rating'], 13)} | #{pad(m['status'], 12)} | #{m['is_gmonkey']}" end end end |
#standard(results, options = {}) ⇒ Object
10 11 12 13 |
# File 'lib/mc/writer.rb', line 10 def standard(results, ={}) redirect_output? results, as_table results, end |