Class: Apt::Spy2::Writer

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

Overview

abstracted puts or json

Instance Method Summary collapse

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

Returns:

  • (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