Class: Apt::Spy2::Writer

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

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ Writer

Returns a new instance of Writer.



7
8
9
10
11
12
13
14
15
# File 'lib/apt/spy2/writer.rb', line 7

def initialize(format)

  if !["json", "shell"].include?(format)
    raise "Unknown format: #{format}"
  end

  @format = format
  @complete = []
end

Instance Method Details

#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
46
47
# File 'lib/apt/spy2/writer.rb', line 41

def json?
  if @format == 'json'
    return true
  end

  return false
end

#set_complete(complete) ⇒ Object



17
18
19
# File 'lib/apt/spy2/writer.rb', line 17

def set_complete(complete)
  @complete = complete
end

#to_jsonObject



49
50
51
# File 'lib/apt/spy2/writer.rb', line 49

def to_json
  JSON.generate(@complete)
end