Class: PactBroker::Client::Matrix::TextFormatter

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

Defined Under Namespace

Classes: Line

Class Method Summary collapse

Class Method Details

.call(matrix) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 14

def self.call(matrix)
  matrix_rows = sort_matrix_rows(matrix[:matrix] || [])
  return "" if matrix_rows.size == 0
  data = prepare_data(matrix_rows)
  printer = TablePrint::Printer.new(data, tp_options(data))
  printer.table_print + verification_result_urls_text(matrix_rows)
end

.lookup(line, default, *keys) ⇒ Object



52
53
54
55
56
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 52

def self.lookup line, default, *keys
  keys.reduce(line) { | line, key | line[key] }
rescue NoMethodError
  default
end

.max_width(data, column, title) ⇒ Object



83
84
85
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 83

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

.prepare_data(matrix_rows) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 22

def self.prepare_data(matrix_rows)
  verification_result_number = 0
  matrix_rows.each_with_index.collect do | line |
    has_verification_result_url = lookup(line, nil, :verificationResult, :_links, :self, :href)
    if has_verification_result_url
      verification_result_number += 1
    end
    Line.new(
      lookup(line, "???", :consumer, :name),
      AbbreviateVersionNumber.call(lookup(line, "???", :consumer, :version, :number)),
      lookup(line, "???", :provider, :name) ,
      AbbreviateVersionNumber.call(lookup(line, "???", :provider, :version, :number)),
      (lookup(line, "???", :verificationResult, :success)).to_s + ( line[:ignored] ? " [ignored]" : ""),
      has_verification_result_url ? verification_result_number : "",
      lookup(line, nil, :ignored)
    )
  end
end

.sort_matrix_rows(matrix_rows) ⇒ Object



87
88
89
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 87

def self.sort_matrix_rows(matrix_rows)
  matrix_rows&.sort { |row_1, row_2| sortable_attributes(row_1) <=> sortable_attributes(row_2) }
end

.sortable_attributes(matrix_row) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 91

def self.sortable_attributes(matrix_row)
  [
    matrix_row.dig(:consumer, :name)&.downcase || "",
    matrix_row.dig(:provider, :name)&.downcase || "",
    matrix_row.dig(:consumer, :version, :number) || "",
    matrix_row.dig(:provider, :version, :number) || ""
  ]
end

.tp_options(data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 41

def self.tp_options(data)
  [
    { consumer: { width: max_width(data, :consumer, 'CONSUMER') } },
    { consumer_version: { display_name: 'C.VERSION', width: max_width(data, :consumer_version, 'C.VERSION') } },
    { provider: { width: max_width(data, :provider, 'PROVIDER') } },
    { provider_version: { display_name: 'P.VERSION', width: max_width(data, :provider_version, 'P.VERSION') } },
    { success: { display_name: 'SUCCESS?' } },
    { ref: { display_name: 'RESULT#' } }
  ]
end

.verification_result_urls_text(matrix_rows) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 70

def self.verification_result_urls_text(matrix_rows)
  text = self.verification_results_urls_and_successes(matrix_rows).each_with_index.collect do |(url, success), i|
    status = success ? 'success' : 'failure'
    "#{i+1}. #{url} (#{status})"
  end.join("\n")

  if text.size > 0
    "\n\nVERIFICATION RESULTS\n--------------------\n#{text}"
  else
    text
  end
end

.verification_results_urls_and_successes(matrix_rows) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 58

def self.verification_results_urls_and_successes(matrix_rows)
  matrix_rows.collect do | row |
    url = row.dig(:verificationResult, :_links, :self, :href)
    if url
      success = row.dig(:verificationResult, :success)
      [url, success]
    else
      nil
    end
  end.compact
end