lib/pact_broker/api/resources/pact.rb in pact_broker-1.1.0 vs lib/pact_broker/api/resources/pact.rb in pact_broker-1.2.0

- old
+ new

@@ -1,60 +1,55 @@ require 'cgi' require 'pact_broker/api/resources/base_resource' +require 'pact_broker/api/resources/pacticipant_resource_methods' require 'pact_broker/api/decorators/pact_decorator' require 'pact_broker/json' -module PactBroker::Api +module PactBroker - module Resources + module Api + module Resources - class Pact < BaseResource + class Pact < BaseResource - def content_types_provided - [["application/json", :to_json]] - end + include PacticipantResourceMethods - def content_types_accepted - [["application/json", :from_json]] - end + def content_types_provided + [["application/json", :to_json]] + end - def allowed_methods - ["GET", "PUT"] - end + def content_types_accepted + [["application/json", :from_json]] + end - def malformed_request? - if request.put? - begin - JSON.parse(pact_content, PactBroker::PACT_PARSING_OPTIONS) #Not load! Otherwise it will try to load Ruby classes. + def allowed_methods + ["GET", "PUT"] + end + + def malformed_request? + if request.put? + return invalid_json? || + potential_duplicate_pacticipants?([identifier_from_path[:consumer_name], identifier_from_path[:provider_name]]) + else false - rescue StandardError => e - logger.error "Error parsing JSON #{e} - #{pact_content}" - response.headers['Content-Type'] = 'application/json' - response.body = {error: "Invalid JSON - #{e.message}"}.to_json - true end end - end - def resource_exists? - @pact = pact_service.find_pact(identifier_from_path) - @pact != nil - end + def resource_exists? + @pact = pact_service.find_pact(identifier_from_path) + @pact != nil + end - def from_json - @pact, created = pact_service.create_or_update_pact(identifier_from_path.merge(:json_content => pact_content)) - response.headers["Location"] = pact_url(base_url, @pact) if created - response.body = to_json - end + def from_json + @pact, created = pact_service.create_or_update_pact(identifier_from_path.merge(:json_content => request_body)) + response.headers["Location"] = pact_url(base_url, @pact) if created + response.body = to_json + end - def to_json - PactBroker::Api::Decorators::PactDecorator.new(@pact).to_json(base_url: base_url) - end + def to_json + PactBroker::Api::Decorators::PactDecorator.new(@pact).to_json(base_url: base_url) + end - def pact_content - request.body.to_s end - end end - -end +end \ No newline at end of file