Class: RViki::Printer
- Inherits:
-
Object
- Object
- RViki::Printer
- Defined in:
- lib/rviki/printer.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#object ⇒ Object
Returns the value of attribute object.
-
#targets ⇒ Object
Returns the value of attribute targets.
Instance Method Summary collapse
- #do_print ⇒ Object
-
#initialize(print_targets = [:stdout]) ⇒ Printer
constructor
A new instance of Printer.
- #pretty_json ⇒ Object
- #pretty_ruby ⇒ Object
- #tabular ⇒ Object
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
#format ⇒ Object
Returns the value of attribute format.
4 5 6 |
# File 'lib/rviki/printer.rb', line 4 def format @format end |
#object ⇒ Object
Returns the value of attribute object.
4 5 6 |
# File 'lib/rviki/printer.rb', line 4 def object @object end |
#targets ⇒ Object
Returns the value of attribute targets.
4 5 6 |
# File 'lib/rviki/printer.rb', line 4 def targets @targets end |
Instance Method Details
#do_print ⇒ Object
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_json ⇒ Object
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_ruby ⇒ Object
31 32 33 34 |
# File 'lib/rviki/printer.rb', line 31 def pretty_ruby return unless inputs_valid? do_output object.pretty_inspect end |
#tabular ⇒ Object
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 |