Class: PactBroker::Pacts::Content

Inherits:
Object
  • Object
show all
Includes:
GenerateInteractionSha
Defined in:
lib/pact_broker/pacts/content.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GenerateInteractionSha

call, #generate_interaction_sha

Methods included from OrderHashKeys

call, order_child_array, order_hash, #order_hash_keys

Constructor Details

#initialize(pact_hash) ⇒ Content

Returns a new instance of Content.



15
16
17
# File 'lib/pact_broker/pacts/content.rb', line 15

def initialize pact_hash
  @pact_hash = pact_hash
end

Class Method Details

.from_hash(pact_hash) ⇒ Object



23
24
25
# File 'lib/pact_broker/pacts/content.rb', line 23

def self.from_hash pact_hash
  new(pact_hash)
end

.from_json(json_content) ⇒ Object



19
20
21
# File 'lib/pact_broker/pacts/content.rb', line 19

def self.from_json json_content
  new(Parse.call(json_content))
end

Instance Method Details

#content_that_affects_verification_resultsObject

Half thinking this belongs in GenerateSha



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pact_broker/pacts/content.rb', line 114

def content_that_affects_verification_results
  if interactions || messages
    cont = {}
    cont["interactions"] = interactions if interactions
    cont["messages"] = messages if messages
    cont["pact_specification_version"] = pact_specification_version if pact_specification_version
    cont
  else
    pact_hash
  end
end

#interaction_idsObject



107
108
109
110
111
# File 'lib/pact_broker/pacts/content.rb', line 107

def interaction_ids
  messages_and_or_interactions_or_empty_array.collect do | interaction |
    interaction["_id"]
  end.compact
end

#interactionsObject



130
131
132
# File 'lib/pact_broker/pacts/content.rb', line 130

def interactions
  pact_hash.is_a?(Hash) && pact_hash["interactions"].is_a?(Array) ? pact_hash["interactions"] : nil
end

#interactions_missing_test_resultsObject



51
52
53
54
55
56
# File 'lib/pact_broker/pacts/content.rb', line 51

def interactions_missing_test_results
  return [] unless messages_and_or_interactions
  messages_and_or_interactions.reject do | interaction |
    interaction["tests"]&.any?
  end
end

#messagesObject



126
127
128
# File 'lib/pact_broker/pacts/content.rb', line 126

def messages
  pact_hash.is_a?(Hash) && pact_hash["messages"].is_a?(Array) ? pact_hash["messages"] : nil
end

#messages_and_or_interactionsObject



134
135
136
137
138
# File 'lib/pact_broker/pacts/content.rb', line 134

def messages_and_or_interactions
  if messages || interactions
    (messages || []) + (interactions || [])
  end
end

#messages_and_or_interactions_or_empty_arrayObject



140
141
142
# File 'lib/pact_broker/pacts/content.rb', line 140

def messages_and_or_interactions_or_empty_array
  messages_and_or_interactions || []
end

#pact_specification_versionObject



144
145
146
147
148
149
# File 'lib/pact_broker/pacts/content.rb', line 144

def pact_specification_version
  maybe_pact_specification_version_1 = pact_hash["metadata"]["pactSpecification"]["version"] rescue nil
  maybe_pact_specification_version_2 = pact_hash["metadata"]["pact-specification"]["version"] rescue nil
  maybe_pact_specification_version_3 = pact_hash["metadata"] && pact_hash["metadata"]["pactSpecificationVersion"] rescue nil
  maybe_pact_specification_version_1 || maybe_pact_specification_version_2 || maybe_pact_specification_version_3
end

#provider_statesObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pact_broker/pacts/content.rb', line 39

def provider_states
  messages_and_or_interactions_or_empty_array.flat_map do | interaction |
    if interaction["providerState"].is_a?(String)
      [ProviderState.new(interaction["providerState"])]
    elsif interaction["providerStates"].is_a?(Array)
      interaction["providerStates"].collect do | provider_state |
        ProviderState.new(provider_state["name"], provider_state["params"])
      end
    end
  end.compact
end

#sortObject



35
36
37
# File 'lib/pact_broker/pacts/content.rb', line 35

def sort
  Content.from_hash(SortContent.call(pact_hash))
end

#to_hashObject



27
28
29
# File 'lib/pact_broker/pacts/content.rb', line 27

def to_hash
  pact_hash
end

#to_jsonObject



31
32
33
# File 'lib/pact_broker/pacts/content.rb', line 31

def to_json
  pact_hash.to_json
end

#with_ids(overwrite_existing_id = true) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pact_broker/pacts/content.rb', line 83

def with_ids(overwrite_existing_id = true)
  new_pact_hash = pact_hash.dup
  if interactions && interactions.is_a?(Array)
    new_pact_hash["interactions"] = add_ids(interactions, overwrite_existing_id)
  end

  if messages && messages.is_a?(Array)
    new_pact_hash["messages"] = add_ids(messages, overwrite_existing_id)
  end
  Content.from_hash(new_pact_hash)
end

#with_test_results(test_results) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pact_broker/pacts/content.rb', line 59

def with_test_results(test_results)
  # new format
  if test_results.is_a?(Array)
    tests = test_results
  else
    # old format
    tests = test_results && test_results["tests"]
    if tests.nil? || !tests.is_a?(Array) || tests.empty?
      tests = []
    end
  end

  new_pact_hash = pact_hash.dup
  if interactions && interactions.is_a?(Array)
    new_pact_hash["interactions"] = merge_verification_results(interactions, tests)
  end

  if messages && messages.is_a?(Array)
    new_pact_hash["messages"] = merge_verification_results(messages, tests)
  end
  Content.from_hash(new_pact_hash)
end

#without_idsObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pact_broker/pacts/content.rb', line 95

def without_ids
  new_pact_hash = pact_hash.dup
  if interactions && interactions.is_a?(Array)
    new_pact_hash["interactions"] = remove_ids(interactions)
  end

  if messages && messages.is_a?(Array)
    new_pact_hash["messages"] = remove_ids(messages)
  end
  Content.from_hash(new_pact_hash)
end