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

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

Direct Known Subclasses

PactVersion

Constant Summary

Constants included from Logging

Logging::LOG_DIR, Logging::LOG_FILE_NAME

Instance Attribute Summary

Attributes inherited from BaseResource

#user

Instance Method Summary collapse

Methods included from PacticipantResourceMethods

#potential_duplicate_pacticipants?

Methods inherited from BaseResource

#base_url, #charsets_provided, #consumer_name, #contract_validation_errors?, #decorator_context, #encode, #finish_request, #forbidden?, #handle_exception, #identifier_from_path, #initialize, #invalid_json?, #is_authorized?, #pacticipant_name, #params, #params_with_string_keys, #provider_name, #request_body, #resource_url, #set_json_error_message, #set_json_validation_error_messages, #validation_errors?, #with_matrix_refresh

Methods included from Logging

included, #log_error, #logger, #logger=

Methods included from Authentication

#authenticated?

Methods included from PactBrokerUrls

#badge_url_for_latest_pact, #hal_browser_url, #label_url, #labels_url, #latest_pact_url, #latest_pacts_url, #latest_untagged_pact_url, #latest_verifications_for_consumer_version_url, #latest_version_url, #matrix_url_from_params, #new_verification_url, #pact_url, #pact_url_from_params, #pact_version_url, #pact_versions_url, #pacticipant_url, #pacticipant_url_from_params, #pacticipants_url, #previous_distinct_diff_url, #previous_distinct_pact_version_url, #tag_url, #tags_url, #templated_tag_url_for_pacticipant, #triggered_webhook_logs_url, #url_encode, #verification_publication_url, #verification_url, #verification_url_from_params, #version_url, #version_url_from_params, #versions_url, #webhook_execution_url, #webhook_url, #webhooks_for_pact_url, #webhooks_status_url, #webhooks_url

Methods included from Services

#badge_service, #certificate_service, #group_service, #index_service, #label_service, #matrix_service, #pact_service, #pacticipant_service, #tag_service, #verification_service, #version_service, #webhook_service

Constructor Details

This class inherits a constructor from PactBroker::Api::Resources::BaseResource

Instance Method Details

#allowed_methodsObject



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

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

#content_types_acceptedObject



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

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

#content_types_providedObject



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

def content_types_provided
  [["application/hal+json", :to_json],
   ["application/json", :to_json],
   ["text/html", :to_html]]
end

#delete_resourceObject



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

def delete_resource
  with_matrix_refresh do
    pact_service.delete(pact_params)
  end
  true
end

#from_jsonObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pact_broker/api/resources/pact.rb', line 64

def from_json
  response_code = pact ? 200 : 201

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

  response.body = to_json
  response_code
end

#is_conflict?Boolean

Returns:

  • (Boolean)


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

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

#known_methodsObject



40
41
42
# File 'lib/pact_broker/api/resources/pact.rb', line 40

def known_methods
  super + ['PATCH']
end

#malformed_request?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
# File 'lib/pact_broker/api/resources/pact.rb', line 51

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

#resource_exists?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pact_broker/api/resources/pact.rb', line 60

def resource_exists?
  !!pact
end

#to_htmlObject



81
82
83
84
85
86
87
# File 'lib/pact_broker/api/resources/pact.rb', line 81

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

#to_jsonObject



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

def to_json
  PactBroker::Api::Decorators::PactDecorator.new(pact).to_json(user_options: { base_url: base_url })
end