Class: PactBroker::Api::Resources::Pact

Inherits:
BaseResource
  • Object
show all
Includes:
PacticipantResourceMethods, WebhookExecutionMethods
Defined in:
lib/pact_broker/api/resources/pact.rb

Direct Known Subclasses

PactVersion

Instance Method Summary collapse

Methods included from WebhookExecutionMethods

#webhook_execution_configuration

Methods included from PacticipantResourceMethods

#potential_duplicate_pacticipants?

Instance Method Details

#allowed_methodsObject



32
33
34
# File 'lib/pact_broker/api/resources/pact.rb', line 32

def allowed_methods
  ["GET", "PUT", "DELETE", "PATCH", "OPTIONS"]
end

#content_types_acceptedObject



28
29
30
# File 'lib/pact_broker/api/resources/pact.rb', line 28

def content_types_accepted
  [["application/json", :from_json]]
end

#content_types_providedObject



19
20
21
22
23
24
25
26
# File 'lib/pact_broker/api/resources/pact.rb', line 19

def content_types_provided
  [
    ["application/hal+json", :to_json],
    ["application/json", :to_json],
    ["text/html", :to_html],
    ["application/vnd.pactbrokerextended.v1+json", :to_extended_json]
  ]
end

#delete_resourceObject



85
86
87
88
89
# File 'lib/pact_broker/api/resources/pact.rb', line 85

def delete_resource
  pact_service.delete(pact_params)
  set_post_deletion_response
  true
end

#from_jsonObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pact_broker/api/resources/pact.rb', line 56

def from_json
  response_code = pact ? 200 : 201

  if request.patch? && resource_exists?
    @pact = pact_service.merge_pact(pact_params, webhook_options)
  else
    @pact = pact_service.create_or_update_pact(pact_params, webhook_options)
  end

  response.body = to_json
  response_code
end

#is_conflict?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/pact_broker/api/resources/pact.rb', line 36

def is_conflict?
  merge_conflict = request.patch? && resource_exists? &&
    Pacts::Merger.conflict?(pact.json_content, pact_params.json_content)

  potential_duplicate_pacticipants?(pact_params.pacticipant_names) || merge_conflict
end

#malformed_request?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'lib/pact_broker/api/resources/pact.rb', line 43

def malformed_request?
  if request.patch? || request.put?
    invalid_json? ||
      contract_validation_errors?(Contracts::PutPactParamsContract.new(pact_params), pact_params)
  else
    false
  end
end

#policy_nameObject



91
92
93
# File 'lib/pact_broker/api/resources/pact.rb', line 91

def policy_name
  :'pacts::pact'
end

#resource_exists?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/pact_broker/api/resources/pact.rb', line 52

def resource_exists?
  !!pact
end

#to_extended_jsonObject



73
74
75
# File 'lib/pact_broker/api/resources/pact.rb', line 73

def to_extended_json
  PactBroker::Api::Decorators::ExtendedPactDecorator.new(pact).to_json(user_options: decorator_context(metadata: identifier_from_path[:metadata]))
end

#to_htmlObject



77
78
79
80
81
82
83
# File 'lib/pact_broker/api/resources/pact.rb', line 77

def to_html
  PactBroker.configuration.html_pact_renderer.call(
    pact, {
      base_url: ui_base_url,
      badge_url: badge_url_for_latest_pact(pact, ui_base_url)
  })
end

#to_jsonObject



69
70
71
# File 'lib/pact_broker/api/resources/pact.rb', line 69

def to_json
  PactBroker::Api::Decorators::PactDecorator.new(pact).to_json(user_options: decorator_context(metadata: identifier_from_path[:metadata]))
end