Class: DMAO::API::ProjectParticipant

Inherits:
Entity
  • Object
show all
Defined in:
lib/dmao/api/project_participant.rb

Constant Summary collapse

ENDPOINT =
'project_participants'
NOT_FOUND_ERROR =
DMAO::API::Errors::ProjectParticipantNotFound
INVALID_ID_ERROR =
DMAO::API::Errors::InvalidProjectParticipantID
INVALID_ENTITY_CLASS =
DMAO::API::Errors::InvalidProjectParticipant
INVALID_ENTITY_ERROR_MESSAGE =
"Invalid project participant details, please see errors."
VALID_ATTRIBUTES =
[:id, :institution_id, :role, :project_id, :participant_id, :person_id]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

all, create, delete, get, instance_from_response, parse_error_response, raise_error_if_key, set_id_attribute_if_not_nil, update, validate_attributes, validate_id, validate_system_uuid

Methods inherited from Base

api

Constructor Details

#initialize(attributes) ⇒ ProjectParticipant

Returns a new instance of ProjectParticipant.



24
25
26
27
28
29
30
31
32
# File 'lib/dmao/api/project_participant.rb', line 24

def initialize(attributes)

  @id = attributes[:id]
  @institution_id = attributes[:institution_id]
  @role = attributes[:role]
  @project_id = attributes[:project_id]
  @participant_id = @person_id = attributes[:participant_id]

end

Class Method Details

.find_by_system_uuid(_system_uuid) ⇒ Object



62
63
64
# File 'lib/dmao/api/project_participant.rb', line 62

def self.find_by_system_uuid _system_uuid
  raise DMAO::API::Errors::UnsupportedQueryBySystemUUID.new
end

.find_project_participant(project_id, participant_id) ⇒ Object

Raises:

  • (self::NOT_FOUND_ERROR)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dmao/api/project_participant.rb', line 66

def self.find_project_participant project_id, participant_id

  response = self.api["#{self::ENDPOINT}?project=#{project_id}&person=#{participant_id}"].get

  response_data = JSON.parse(response)["data"]

  raise self::NOT_FOUND_ERROR.new if response_data.length == 0
  raise DMAO::API::Errors::InvalidResponseLength.new("Expected 1 element in response there were #{response_data.length}") if response_data.length != 1

  data = response_data[0]

  instance_from_api_data data

end

.handle_unprocessable_entity(error_response) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dmao/api/project_participant.rb', line 48

def self.handle_unprocessable_entity error_response

  errors = parse_error_response error_response

  raise DMAO::API::Errors::InvalidProjectParticipant.new("Invalid project participant details, please see errors.", errors) if (errors.keys.include?("project") && errors.keys.include?("person"))

  raise_error_if_key DMAO::API::Errors::InvalidProjectID, errors, "project"
  raise_error_if_key DMAO::API::Errors::InvalidPersonID, errors, "person"

  raise DMAO::API::Errors::InvalidProjectParticipant.new("Invalid project participant details, please see errors.", errors)


end

.instance_from_api_data(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dmao/api/project_participant.rb', line 34

def self.instance_from_api_data data

  attributes = {
      id: data["id"],
      institution_id: data["relationships"]["institution"]["data"]["id"],
      role: data["attributes"]["role"],
      project_id: data["relationships"]["project"]["data"]["id"],
      participant_id: data["relationships"]["person"]["data"]["id"]
  }

  new(attributes)

end