Class: PactBroker::Client::Pacticipants2::TextFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/client/pacticipants/text_formatter.rb

Class Method Summary collapse

Class Method Details

.call(pacticipants) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/pact_broker/client/pacticipants/text_formatter.rb', line 8

def self.call(pacticipants)
  return "" if pacticipants.size == 0

  data = pacticipants.collect do | pacticipant |
    # Might add a UUID in at some stage. Backwards compatible supporting code.
    OpenStruct.new({ uuid: "" }.merge(pacticipant).merge(url: pacticipant["_links"]["self"]["href"]))
  end.sort_by{ | pacticipant | pacticipant.name.downcase }

  TablePrint::Printer.new(data, tp_options(data)).table_print
end

.max_width(data, column, title) ⇒ Object



35
36
37
# File 'lib/pact_broker/client/pacticipants/text_formatter.rb', line 35

def self.max_width(data, column, title)
  (data.collect{ |row| row.send(column) } + [title]).compact.collect(&:size).max
end

.tp_options(data) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pact_broker/client/pacticipants/text_formatter.rb', line 19

def self.tp_options(data)
  uuid_width = max_width(data, :uuid, "")
  name_width = max_width(data, :name, "NAME")
  display_name_width = max_width(data, :displayName, "DISPLAY NAME")

  tp_options = [
    { name: { width: name_width} },
    { displayName: { display_name: "Display name", width: display_name_width } },
  ]

  if uuid_width > 0
    tp_options.unshift({ uuid: { width: uuid_width } })
  end
  tp_options
end