Sha256: 7f757348e57ab7c1f2b3672111699bd82a95aecb7e1012f9c45a63eac8343fcb

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

require "pact_broker/api/resources/base_resource"
require "pact_broker/api/resources/environment"
require "pact_broker/api/contracts/environment_schema"

module PactBroker
  module Api
    module Resources
      class Environments < BaseResource
        def content_types_provided
          [["application/hal+json", :to_json]]
        end

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

        def allowed_methods
          ["GET", "POST", "OPTIONS"]
        end

        def resource_exists?
          true
        end

        def post_is_create?
          true
        end

        def malformed_request?
          super || (request.post? && validation_errors_for_schema?(schema, params.merge(uuid: uuid)))
        end

        def create_path
          environment_url(OpenStruct.new(uuid: uuid), base_url)
        end

        def from_json
          response.body = decorator_class(:environment_decorator).new(create_environment).to_json(**decorator_options)
        end

        def policy_name
          :'deployments::environments'
        end

        def to_json
          decorator_class(:environments_decorator).new(environments).to_json(**decorator_options)
        end

        def parsed_environment
          @parsed_environment ||= decorator_class(:environment_decorator).new(PactBroker::Deployments::Environment.new).from_json(request_body)
        end

        def create_environment
          environment_service.create(uuid, parsed_environment)
        end

        def uuid
          @uuid ||= environment_service.next_uuid
        end

        def environments
          @environments ||= if request.query["name"]
                              [environment_service.find_by_name(request.query["name"])]
                            else
                              environment_service.find_all
                            end
        end

        def schema
          api_contract_class(:environment_schema)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pact_broker-2.107.1 lib/pact_broker/api/resources/environments.rb
pact_broker-2.107.0 lib/pact_broker/api/resources/environments.rb
pact_broker-2.107.0.beta.1 lib/pact_broker/api/resources/environments.rb