dmao_api 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86fbfebf03c55721de45759213ac525070534b45
4
- data.tar.gz: 62d993aa9ae429585fd91b69fa6a630cc48c3741
3
+ metadata.gz: 8e7257d738325683b5729c978624513908db362c
4
+ data.tar.gz: ddf6f8f8e95bb525f749ab6000659560079673a3
5
5
  SHA512:
6
- metadata.gz: 8a630788fba8c1c44d36220d32a04ca3414ebc5bb71841f7ef9b01a0e9aad2f262aba3b1e55897cfaccd5c7c086f7c53fd38ac42addf648a1444b3e178b57f4e
7
- data.tar.gz: df765da27d14c6c0b531261add761170ab6d98598edc54e3c8ad104b97b07feb258f509ec4be48ad3ed8a13d5fb642d306495758b366fac0a71e6363755e0580
6
+ metadata.gz: 8c5765835fb5a8f277ccc48bd093878b82d5a6b477cd77e5a4a8ec0219cfbb3aca471fde00d29dfdde3bacfa74b2faaf1f44d52534b9b6e3beea81612efbc8e8
7
+ data.tar.gz: 19656c8cbdb4ccf1f731b8d3ff22688d273cf62d0f4083fb3f0dac935ae572e1ab18f015b64af20d9533329ac5c37d83b0572005b3b86f515a41f2034b428b52
@@ -0,0 +1,163 @@
1
+ require 'dmao/api/base'
2
+
3
+ require 'dmao/api/errors/institution_not_found'
4
+ require 'dmao/api/errors/invalid_system_uuid'
5
+ require 'dmao/api/errors/invalid_response_length'
6
+
7
+ module DMAO
8
+ module API
9
+
10
+ class Entity < Base
11
+
12
+ ENDPOINT = ''
13
+ NOT_FOUND_ERROR = nil
14
+ INVALID_ID_ERROR = nil
15
+ INVALID_ENTITY_CLASS = nil
16
+ INVALID_ENTITY_ERROR_MESSAGE = ""
17
+ VALID_ATTRIBUTES = []
18
+
19
+ def self.find_by_system_uuid system_uuid
20
+
21
+ validate_system_uuid system_uuid
22
+
23
+ response = self.api["#{self::ENDPOINT}?system_uuid=#{system_uuid}"].get
24
+
25
+ response_data = JSON.parse(response)["data"]
26
+
27
+ raise self::NOT_FOUND_ERROR.new if response_data.length == 0
28
+ raise DMAO::API::Errors::InvalidResponseLength.new("Expected 1 element in response there were #{response_data.length}") if response_data.length != 1
29
+
30
+ data = response_data[0]
31
+
32
+ instance_from_api_data data
33
+
34
+ end
35
+
36
+ def self.all
37
+
38
+ begin
39
+ response = self.api[self::ENDPOINT].get
40
+ rescue RestClient::NotFound
41
+ raise DMAO::API::Errors::InstitutionNotFound.new
42
+ end
43
+
44
+ entities = []
45
+
46
+ response_data = JSON.parse(response)["data"]
47
+
48
+ return entities if response_data.length == 0
49
+
50
+ response_data.each do |data|
51
+
52
+ entities.push instance_from_api_data(data)
53
+
54
+ end
55
+
56
+ entities
57
+
58
+ end
59
+
60
+ def self.get id
61
+
62
+ validate_id id
63
+
64
+ begin
65
+ response = self.api["#{self::ENDPOINT}/#{id}"].get
66
+ rescue RestClient::NotFound
67
+ raise self::NOT_FOUND_ERROR.new
68
+ end
69
+
70
+ instance_from_response response
71
+
72
+ end
73
+
74
+ def self.create attributes
75
+
76
+ validate_attributes attributes
77
+
78
+ begin
79
+ response = self.api[self::ENDPOINT].post attributes.to_json
80
+ rescue RestClient::NotFound
81
+ raise DMAO::API::Errors::InstitutionNotFound.new
82
+ rescue RestClient::UnprocessableEntity => e
83
+ handle_unprocessable_entity e
84
+ end
85
+
86
+ instance_from_response response
87
+
88
+ end
89
+
90
+ def self.update id, attributes
91
+
92
+ validate_id id
93
+
94
+ validate_attributes attributes
95
+
96
+ begin
97
+ response = self.api["#{self::ENDPOINT}/#{id}"].patch attributes.to_json
98
+ rescue RestClient::NotFound
99
+ raise self::NOT_FOUND_ERROR.new
100
+ rescue RestClient::UnprocessableEntity => e
101
+ handle_unprocessable_entity e
102
+ end
103
+
104
+ instance_from_response response
105
+
106
+ end
107
+
108
+ def self.delete id
109
+
110
+ validate_id id
111
+
112
+ begin
113
+ self.api["#{self::ENDPOINT}/#{id}"].delete
114
+ rescue RestClient::NotFound
115
+ raise self::NOT_FOUND_ERROR.new
116
+ rescue RestClient::UnprocessableEntity => e
117
+ handle_unprocessable_entity e
118
+ end
119
+
120
+ true
121
+
122
+ end
123
+
124
+ def self.instance_from_response response_body
125
+
126
+ data = JSON.parse(response_body)["data"]
127
+
128
+ instance_from_api_data data
129
+
130
+ end
131
+
132
+ def self.handle_unprocessable_entity error_response
133
+
134
+ errors = JSON.parse(error_response.response.body)["errors"]
135
+
136
+ raise self::INVALID_ENTITY_CLASS.new(self::INVALID_ENTITY_ERROR_MESSAGE, errors)
137
+
138
+ end
139
+
140
+ def self.validate_id id
141
+ if id.nil? || id.to_s.empty?
142
+ raise self::INVALID_ID_ERROR.new
143
+ end
144
+ end
145
+
146
+ def self.validate_system_uuid system_uuid
147
+ if system_uuid.nil? || system_uuid.to_s.empty?
148
+ raise DMAO::API::Errors::InvalidSystemUUID.new
149
+ end
150
+ end
151
+
152
+ def self.validate_attributes attributes
153
+ attributes.keep_if { |k, _v| self::VALID_ATTRIBUTES.include? k }
154
+ end
155
+
156
+ def self.instance_from_api_data _data
157
+ raise "Should be overridden in extending class"
158
+ end
159
+
160
+ end
161
+
162
+ end
163
+ end
@@ -0,0 +1,21 @@
1
+ module DMAO
2
+ module API
3
+ module Errors
4
+
5
+ class InvalidPerson < StandardError
6
+
7
+ attr_reader :errors
8
+
9
+ def initialize(msg="Invalid person details passed in, please see errors.", errors=nil)
10
+
11
+ @errors = errors
12
+
13
+ super(msg)
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module DMAO
2
+ module API
3
+ module Errors
4
+
5
+ class InvalidPersonID < StandardError
6
+
7
+ attr_reader :errors
8
+
9
+ def initialize(msg="Invalid person ID specified")
10
+
11
+ super(msg)
12
+
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module DMAO
2
+ module API
3
+ module Errors
4
+
5
+ class PersonNotFound < StandardError
6
+
7
+ def initialize(msg="Invalid person id specified, id does not exist for requesting institution.")
8
+
9
+ super(msg)
10
+
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -1,16 +1,20 @@
1
1
  require 'rest-client'
2
- require 'dmao/api/base'
3
- require 'dmao/api/errors/institution_not_found'
2
+ require 'dmao/api/entity'
4
3
  require 'dmao/api/errors/invalid_parent_id'
5
4
  require 'dmao/api/errors/invalid_organisation_unit'
5
+ require 'dmao/api/errors/invalid_organisation_unit_id'
6
6
  require 'dmao/api/errors/organisation_unit_not_found'
7
- require 'dmao/api/errors/invalid_system_uuid'
8
- require 'dmao/api/errors/invalid_response_length'
9
7
 
10
8
  module DMAO
11
9
  module API
12
10
 
13
- class OrganisationUnit < Base
11
+ class OrganisationUnit < Entity
12
+
13
+ ENDPOINT = 'organisation_units'
14
+ NOT_FOUND_ERROR = DMAO::API::Errors::OrganisationUnitNotFound
15
+ INVALID_ID_ERROR = DMAO::API::Errors::InvalidOrganisationUnitID
16
+ INVALID_ENTITY_CLASS = DMAO::API::Errors::InvalidOrganisationUnit
17
+ INVALID_ENTITY_ERROR_MESSAGE = "Invalid person details, please see errors."
14
18
 
15
19
  VALID_ATTRIBUTES = [:id, :institution_id, :name, :description, :url, :system_uuid, :system_modified_at, :isni, :unit_type, :parent_id]
16
20
  attr_reader(*VALID_ATTRIBUTES)
@@ -33,119 +37,6 @@ module DMAO
33
37
 
34
38
  end
35
39
 
36
- def self.all
37
-
38
- begin
39
- response = self.api['organisation_units'].get
40
- rescue RestClient::NotFound
41
- raise DMAO::API::Errors::InstitutionNotFound.new
42
- end
43
-
44
- org_units = []
45
-
46
- response_data = JSON.parse(response)["data"]
47
-
48
- return org_units if response_data.length == 0
49
-
50
- response_data.each do |data|
51
-
52
- org_units.push instance_from_api_data(data)
53
-
54
- end
55
-
56
- org_units
57
-
58
- end
59
-
60
- def self.get id
61
-
62
- validate_organisation_unit_id id
63
-
64
- begin
65
- response = self.api["organisation_units/#{id}"].get
66
- rescue RestClient::NotFound
67
- raise DMAO::API::Errors::OrganisationUnitNotFound.new
68
- end
69
-
70
- instance_from_response response
71
-
72
- end
73
-
74
- def self.find_by_system_uuid system_uuid
75
-
76
- validate_system_uuid system_uuid
77
-
78
- response = self.api["organisation_units?system_uuid=#{system_uuid}"].get
79
-
80
- response_data = JSON.parse(response)["data"]
81
-
82
- raise DMAO::API::Errors::OrganisationUnitNotFound.new if response_data.length == 0
83
- raise DMAO::API::Errors::InvalidResponseLength.new("Expected 1 element in response there were #{response_data.length}") if response_data.length != 1
84
-
85
- data = response_data[0]
86
-
87
- instance_from_api_data data
88
-
89
- end
90
-
91
- def self.create attributes
92
-
93
- validate_attributes attributes
94
-
95
- begin
96
- response = self.api['organisation_units'].post attributes.to_json
97
- rescue RestClient::NotFound
98
- raise DMAO::API::Errors::InstitutionNotFound.new
99
- rescue RestClient::UnprocessableEntity => e
100
- handle_unprocessable_entity e
101
- end
102
-
103
- instance_from_response response
104
-
105
- end
106
-
107
- def self.update id, attributes
108
-
109
- validate_organisation_unit_id id
110
-
111
- validate_attributes attributes
112
-
113
- begin
114
- response = self.api["organisation_units/#{id}"].patch attributes.to_json
115
- rescue RestClient::NotFound
116
- raise DMAO::API::Errors::OrganisationUnitNotFound.new
117
- rescue RestClient::UnprocessableEntity => e
118
- handle_unprocessable_entity e
119
- end
120
-
121
- instance_from_response response
122
-
123
- end
124
-
125
- def self.delete id
126
-
127
- validate_organisation_unit_id id
128
-
129
- begin
130
- self.api["organisation_units/#{id}"].delete
131
- rescue RestClient::NotFound
132
- raise DMAO::API::Errors::OrganisationUnitNotFound.new
133
- rescue RestClient::UnprocessableEntity => e
134
- handle_unprocessable_entity e
135
- end
136
-
137
- true
138
-
139
- end
140
-
141
- def self.instance_from_response response_body
142
-
143
- data = JSON.parse(response_body)["data"]
144
-
145
- instance_from_api_data data
146
-
147
- end
148
-
149
40
  def self.instance_from_api_data data
150
41
 
151
42
  parent_id = data["relationships"]["parent"]["data"].nil? ? nil : data["relationships"]["parent"]["data"]["id"]
@@ -167,22 +58,6 @@ module DMAO
167
58
 
168
59
  end
169
60
 
170
- def self.validate_attributes attributes
171
- attributes.keep_if { |k, _v| VALID_ATTRIBUTES.include? k }
172
- end
173
-
174
- def self.validate_organisation_unit_id id
175
- if id.nil? || id.to_s.empty?
176
- raise DMAO::API::Errors::InvalidOrganisationUnitID.new
177
- end
178
- end
179
-
180
- def self.validate_system_uuid system_uuid
181
- if system_uuid.nil? || system_uuid.to_s.empty?
182
- raise DMAO::API::Errors::InvalidSystemUUID.new
183
- end
184
- end
185
-
186
61
  def self.handle_unprocessable_entity error_response
187
62
 
188
63
  errors = JSON.parse(error_response.response.body)["errors"]
@@ -0,0 +1,56 @@
1
+ require 'dmao/api/entity'
2
+ require 'dmao/api/errors/invalid_person'
3
+ require 'dmao/api/errors/invalid_person_id'
4
+ require 'dmao/api/errors/person_not_found'
5
+
6
+ module DMAO
7
+ module API
8
+
9
+ class Person < Entity
10
+
11
+ ENDPOINT = 'people'
12
+ NOT_FOUND_ERROR = DMAO::API::Errors::PersonNotFound
13
+ INVALID_ID_ERROR = DMAO::API::Errors::InvalidPersonID
14
+ INVALID_ENTITY_CLASS = DMAO::API::Errors::InvalidPerson
15
+ INVALID_ENTITY_ERROR_MESSAGE = "Invalid person details, please see errors."
16
+
17
+ VALID_ATTRIBUTES = [:id, :institution_id, :first_name, :last_name, :email, :image_link, :orcid, :system_uuid, :system_modified_at]
18
+
19
+ attr_reader(*VALID_ATTRIBUTES)
20
+
21
+ def initialize(attributes)
22
+
23
+ @id = attributes[:id]
24
+ @institution_id = attributes[:institution_id]
25
+ @first_name = attributes[:first_name]
26
+ @last_name = attributes[:last_name]
27
+ @email = attributes[:email]
28
+ @image_link = attributes[:image_link]
29
+ @orcid = attributes[:orcid]
30
+ @system_uuid = attributes[:system_uuid]
31
+ @system_modified_at = attributes[:system_modified_at]
32
+
33
+ end
34
+
35
+ def self.instance_from_api_data data
36
+
37
+ attributes = {
38
+ id: data["id"],
39
+ institution_id: data["relationships"]["institution"]["data"]["id"],
40
+ first_name: data["attributes"]["first-name"],
41
+ last_name: data["attributes"]["last-name"],
42
+ email: data["attributes"]["email"],
43
+ image_link: data["attributes"]["image-link"],
44
+ orcid: data["attributes"]["orcid"],
45
+ system_uuid: data["attributes"]["system-uuid"],
46
+ system_modified_at: data["attributes"]["system-modified-at"]
47
+ }
48
+
49
+ new(attributes)
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  module DMAO
2
2
  module API
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dmao_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Robinson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-07 00:00:00.000000000 Z
11
+ date: 2017-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -153,16 +153,21 @@ files:
153
153
  - dmao_api.gemspec
154
154
  - lib/dmao/api/base.rb
155
155
  - lib/dmao/api/configuration.rb
156
+ - lib/dmao/api/entity.rb
156
157
  - lib/dmao/api/errors/institution_not_found.rb
157
158
  - lib/dmao/api/errors/invalid_api_base_url.rb
158
159
  - lib/dmao/api/errors/invalid_api_token.rb
159
160
  - lib/dmao/api/errors/invalid_organisation_unit.rb
160
161
  - lib/dmao/api/errors/invalid_organisation_unit_id.rb
161
162
  - lib/dmao/api/errors/invalid_parent_id.rb
163
+ - lib/dmao/api/errors/invalid_person.rb
164
+ - lib/dmao/api/errors/invalid_person_id.rb
162
165
  - lib/dmao/api/errors/invalid_response_length.rb
163
166
  - lib/dmao/api/errors/invalid_system_uuid.rb
164
167
  - lib/dmao/api/errors/organisation_unit_not_found.rb
168
+ - lib/dmao/api/errors/person_not_found.rb
165
169
  - lib/dmao/api/organisation_unit.rb
170
+ - lib/dmao/api/person.rb
166
171
  - lib/dmao/api/version.rb
167
172
  - lib/dmao_api.rb
168
173
  homepage: https://github.com/lulibrary/DMAO-API-rb