Class: RViki::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/rviki/printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(print_targets = [:stdout]) ⇒ Printer

Returns a new instance of Printer.



6
7
8
# File 'lib/rviki/printer.rb', line 6

def initialize(print_targets=[:stdout])
  self.targets = print_targets
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



4
5
6
# File 'lib/rviki/printer.rb', line 4

def format
  @format
end

#objectObject

Returns the value of attribute object.



4
5
6
# File 'lib/rviki/printer.rb', line 4

def object
  @object
end

#targetsObject

Returns the value of attribute targets.



4
5
6
# File 'lib/rviki/printer.rb', line 4

def targets
  @targets
end

Instance Method Details

#do_printObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rviki/printer.rb', line 10

def do_print
  if self.format
    begin
      self.send(self.format)
    rescue Exception
      self.pretty_json
    end
  else
    self.pretty_json
  end
end

#pretty_jsonObject



36
37
38
39
# File 'lib/rviki/printer.rb', line 36

def pretty_json
  return unless inputs_valid?
  do_output JSON.pretty_generate(object)
end

#pretty_rubyObject



31
32
33
34
# File 'lib/rviki/printer.rb', line 31

def pretty_ruby
  return unless inputs_valid?
  do_output object.pretty_inspect
end

#tabularObject



22
23
24
25
26
27
28
29
# File 'lib/rviki/printer.rb', line 22

def tabular
  return unless inputs_valid?
  raise "Cannot print tabular data because parsed response is not an array" unless object.is_a?(Array)

  out = object.first.keys.join("\t") + "\n"
  object.each { |item| out += (item.values.join("\t") + "\n") }
  do_output out
end