Class: AwsPublicIps::Formatters::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_public_ips/formatters/text.rb

Instance Method Summary collapse

Constructor Details

#initialize(results, options) ⇒ Text

Returns a new instance of Text.



6
7
8
9
# File 'lib/aws_public_ips/formatters/text.rb', line 6

def initialize(results, options)
  @results = results
  @options = options
end

Instance Method Details

#formatObject



11
12
13
14
# File 'lib/aws_public_ips/formatters/text.rb', line 11

def format
  lines = @options[:verbose] ? format_verbose : format_normal
  lines.uniq.join("\n")
end

#format_normalObject



16
17
18
19
20
# File 'lib/aws_public_ips/formatters/text.rb', line 16

def format_normal
  @results.values.flatten.flat_map do |hash|
    hash[:ip_addresses]
  end
end

#format_verboseObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aws_public_ips/formatters/text.rb', line 22

def format_verbose
  @results.flat_map do |service, hashes|
    next [] if hashes.empty?

    ["## #{service}"] + hashes.flat_map do |hash|
      [hash[:id], hash[:hostname]].compact.map do |line|
        "# #{line}"
      end + hash[:ip_addresses]
    end
  end
end