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

Constant Summary collapse

OPTIONS =
[
  { consumer: {} },
  { consumer_version: {display_name: 'C.VERSION'} },
  { provider: {} },
  { provider_version: {display_name: 'P.VERSION'} },
  { success: {display_name: 'SUCCESS?'} },
  { ref: { display_name: 'RESULT#' }}
]

Class Method Summary collapse

Class Method Details

.call(matrix) ⇒ Object



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

def self.call(matrix)
  matrix_rows = matrix[:matrix]
  return "" if matrix_rows.size == 0
  verification_result_number = 0
  data = 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),
      lookup(line, "???", :consumer, :version, :number),
      lookup(line, "???", :provider, :name) ,
      lookup(line, "???", :provider, :version, :number),
      (lookup(line, "???", :verificationResult, :success)).to_s,
      has_verification_result_url ? verification_result_number : ""
    )
  end

  printer = TablePrint::Printer.new(data, OPTIONS)
  printer.table_print + verification_result_urls_text(matrix)
end

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



45
46
47
48
49
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 45

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

.verification_result_urls_text(matrix) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 63

def self.verification_result_urls_text(matrix)
  text = self.verification_results_urls_and_successes(matrix).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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pact_broker/client/matrix/text_formatter.rb', line 51

def self.verification_results_urls_and_successes(matrix)
  (matrix[:matrix] || []).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