Class: Apt::Spy2::Writer
- Inherits:
-
Object
- Object
- Apt::Spy2::Writer
- Defined in:
- lib/apt/spy2/writer.rb
Overview
abstracted puts or json
Instance Method Summary collapse
- #complete(complete) ⇒ Object
- #echo(data) ⇒ Object
-
#initialize(format) ⇒ Writer
constructor
A new instance of Writer.
- #json? ⇒ Boolean
- #to_json(*_args) ⇒ Object
Constructor Details
#initialize(format) ⇒ Writer
Returns a new instance of Writer.
10 11 12 13 14 15 |
# File 'lib/apt/spy2/writer.rb', line 10 def initialize(format) raise "Unknown format: #{format}" unless %w[json shell].include?(format) @format = format @complete = [] end |
Instance Method Details
#complete(complete) ⇒ Object
17 18 19 |
# File 'lib/apt/spy2/writer.rb', line 17 def complete(complete) @complete = complete end |
#echo(data) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/apt/spy2/writer.rb', line 21 def echo(data) if @format == 'json' @complete.push(data) return end print "Mirror: #{data['mirror']} - " case data['status'] when 'up' puts data['status'].upcase.green when 'down' puts data['status'].upcase.red when 'broken' puts data['status'].upcase.yellow else puts "Unknown status: #{data['status']}".white_on_red end end |
#json? ⇒ Boolean
41 42 43 44 45 |
# File 'lib/apt/spy2/writer.rb', line 41 def json? return true if @format == 'json' false end |
#to_json(*_args) ⇒ Object
47 48 49 |
# File 'lib/apt/spy2/writer.rb', line 47 def to_json(*_args) JSON.generate(@complete) end |