require 'dmao/api/entity' require 'dmao/api/errors/invalid_project' require 'dmao/api/errors/invalid_project_id' require 'dmao/api/errors/invalid_organisation_unit_id' require 'dmao/api/errors/project_not_found' module DMAO module API class Project < Entity ENDPOINT = 'projects' NOT_FOUND_ERROR = DMAO::API::Errors::ProjectNotFound INVALID_ID_ERROR = DMAO::API::Errors::InvalidProjectID INVALID_ENTITY_CLASS = DMAO::API::Errors::InvalidProject INVALID_ENTITY_ERROR_MESSAGE = "Invalid project details, please see errors." VALID_ATTRIBUTES = [ :id, :institution_id, :title, :acronym, :description, :status, :project_type, :classification, :internal_project_code, :url, :start_date, :end_date, :system_uuid, :system_modified_at, :owning_unit_id, :funder_id ] attr_reader(*VALID_ATTRIBUTES) def initialize(attributes) @id = attributes[:id] @institution_id = attributes[:institution_id] @title = attributes[:title] @acronym = attributes[:acronym] @description = attributes[:description] @status = attributes[:status] @project_type = attributes[:project_type] @classification = attributes[:classification] @internal_project_code = attributes[:internal_project_code] @url = attributes[:url] @start_date = attributes[:start_date] @end_date = attributes[:end_date] @system_uuid = attributes[:system_uuid] @system_modified_at = attributes[:system_modified_at] @owning_unit_id = attributes[:owning_unit_id] @funder_id = attributes[:funder_id] end def self.instance_from_api_data data funder_id = set_id_attribute_if_not_nil data["relationships"]["funder"]["data"] attributes = { id: data["id"], institution_id: data["relationships"]["institution"]["data"]["id"], title: data["attributes"]["title"], acronym: data["attributes"]["acronym"], description: data["attributes"]["description"], status: data["attributes"]["status"], project_type: data["attributes"]["project-type"], classification: data["attributes"]["classification"], internal_project_code: data["attributes"]["internal-project-code"], url: data["attributes"]["url"], start_date: data["attributes"]["start-date"], end_date: data["attributes"]["end-date"], system_uuid: data["attributes"]["system-uuid"], system_modified_at: data["attributes"]["system-modified-at"], owning_unit_id: data["relationships"]["owning-unit"]["data"]["id"], funder_id: funder_id } new(attributes) end def self.handle_unprocessable_entity error_response errors = parse_error_response error_response raise_error_if_key DMAO::API::Errors::InvalidOrganisationUnitID, errors, "owning_unit" raise DMAO::API::Errors::InvalidProject.new("Invalid project details, please see errors.", errors) end end end end