require 'rest-client' require 'dmao/api/base' require 'dmao/api/errors/institution_not_found' require 'dmao/api/errors/invalid_parent_id' require 'dmao/api/errors/invalid_organisation_unit' require 'dmao/api/errors/organisation_unit_not_found' require 'dmao/api/errors/invalid_system_uuid' require 'dmao/api/errors/invalid_response_length' module DMAO module API class OrganisationUnit < Base VALID_ATTRIBUTES = [:id, :institution_id, :name, :description, :url, :system_uuid, :system_modified_at, :isni, :unit_type, :parent_id] attr_reader(*VALID_ATTRIBUTES) def initialize(attributes) @id = attributes[:id] @institution_id = attributes[:institution_id] @name = attributes[:name] @description = attributes[:description] @url = attributes[:url] @system_uuid = attributes[:system_uuid] @system_modified_at = attributes[:system_modified_at] @isni = attributes[:isni] @unit_type = attributes[:unit_type] if attributes[:parent_id] @parent_id = attributes[:parent_id] end end def self.all begin response = self.api['organisation_units'].get rescue RestClient::NotFound raise DMAO::API::Errors::InstitutionNotFound.new end org_units = [] response_data = JSON.parse(response)["data"] return org_units if response_data.length == 0 response_data.each do |data| org_units.push instance_from_api_data(data) end org_units end def self.get id validate_organisation_unit_id id begin response = self.api["organisation_units/#{id}"].get rescue RestClient::NotFound raise DMAO::API::Errors::OrganisationUnitNotFound.new end instance_from_response response end def self.find_by_system_uuid system_uuid validate_system_uuid system_uuid response = self.api["organisation_units?system_uuid=#{system_uuid}"].get response_data = JSON.parse(response)["data"] raise DMAO::API::Errors::OrganisationUnitNotFound.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 def self.create attributes validate_attributes attributes begin response = self.api['organisation_units'].post attributes.to_json rescue RestClient::NotFound raise DMAO::API::Errors::InstitutionNotFound.new rescue RestClient::UnprocessableEntity => e handle_unprocessable_entity e end instance_from_response response end def self.update id, attributes validate_organisation_unit_id id validate_attributes attributes begin response = self.api["organisation_units/#{id}"].patch attributes.to_json rescue RestClient::NotFound raise DMAO::API::Errors::OrganisationUnitNotFound.new rescue RestClient::UnprocessableEntity => e handle_unprocessable_entity e end instance_from_response response end def self.delete id validate_organisation_unit_id id begin self.api["organisation_units/#{id}"].delete rescue RestClient::NotFound raise DMAO::API::Errors::OrganisationUnitNotFound.new rescue RestClient::UnprocessableEntity => e handle_unprocessable_entity e end true end def self.instance_from_response response_body data = JSON.parse(response_body)["data"] instance_from_api_data data end def self.instance_from_api_data data parent_id = data["relationships"]["parent"]["data"].nil? ? nil : data["relationships"]["parent"]["data"]["id"] attributes = { id: data["id"], institution_id: data["relationships"]["institution"]["data"]["id"], name: data["attributes"]["name"], description: data["attributes"]["description"], url: data["attributes"]["url"], system_uuid: data["attributes"]["system-uuid"], system_modified_at: data["attributes"]["system-modified-at"], isni: data["attributes"]["isni"], unit_type: data["attributes"]["unit-type"], parent_id: parent_id } new(attributes) end def self.validate_attributes attributes attributes.keep_if { |k, _v| VALID_ATTRIBUTES.include? k } end def self.validate_organisation_unit_id id if id.nil? || id.to_s.empty? raise DMAO::API::Errors::InvalidOrganisationUnitID.new end end def self.validate_system_uuid system_uuid if system_uuid.nil? || system_uuid.to_s.empty? raise DMAO::API::Errors::InvalidSystemUUID.new end end def self.handle_unprocessable_entity error_response errors = JSON.parse(error_response.response.body)["errors"] raise DMAO::API::Errors::InvalidParentId.new if errors.keys.include? "parent" raise DMAO::API::Errors::InvalidOrganisationUnit.new("Invalid organisation unit details, please see errors.", errors) end end end end