Class: DMAO::API::OrganisationUnit

Inherits:
Base
  • Object
show all
Defined in:
lib/dmao/api/organisation_unit.rb

Constant Summary collapse

VALID_ATTRIBUTES =
[:id, :institution_id, :name, :description, :url, :system_uuid, :system_modified_at, :isni, :unit_type, :parent_id]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api

Constructor Details

#initialize(attributes) ⇒ OrganisationUnit

Returns a new instance of OrganisationUnit.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dmao/api/organisation_unit.rb', line 18

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

Class Method Details

.allObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dmao/api/organisation_unit.rb', line 36

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

.create(attributes) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dmao/api/organisation_unit.rb', line 91

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

.delete(id) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dmao/api/organisation_unit.rb', line 125

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

.find_by_system_uuid(system_uuid) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dmao/api/organisation_unit.rb', line 74

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

.get(id) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dmao/api/organisation_unit.rb', line 60

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

.handle_unprocessable_entity(error_response) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/dmao/api/organisation_unit.rb', line 186

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

.instance_from_api_data(data) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dmao/api/organisation_unit.rb', line 149

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

.instance_from_response(response_body) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/dmao/api/organisation_unit.rb', line 141

def self.instance_from_response response_body

  data = JSON.parse(response_body)["data"]

  instance_from_api_data data

end

.update(id, attributes) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dmao/api/organisation_unit.rb', line 107

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

.validate_attributes(attributes) ⇒ Object



170
171
172
# File 'lib/dmao/api/organisation_unit.rb', line 170

def self.validate_attributes attributes
  attributes.keep_if { |k, _v| VALID_ATTRIBUTES.include? k }
end

.validate_organisation_unit_id(id) ⇒ Object



174
175
176
177
178
# File 'lib/dmao/api/organisation_unit.rb', line 174

def self.validate_organisation_unit_id id
  if id.nil? || id.to_s.empty?
    raise DMAO::API::Errors::InvalidOrganisationUnitID.new
  end
end

.validate_system_uuid(system_uuid) ⇒ Object



180
181
182
183
184
# File 'lib/dmao/api/organisation_unit.rb', line 180

def self.validate_system_uuid system_uuid
  if system_uuid.nil? || system_uuid.to_s.empty?
    raise DMAO::API::Errors::InvalidSystemUUID.new
  end
end