dmao-generic-ingesters 0.5.0 → 0.6.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: 14515f70a6f8d85f46287efdda0215af1bc1dda9
4
- data.tar.gz: 4b358a91f560c57d6e44918cd15bcad63cb26147
3
+ metadata.gz: 9638acab4b7d6a95530831b63d34343c12c4be18
4
+ data.tar.gz: cc6cdc638125bb133ea3f0df7f8576acdf6f6b87
5
5
  SHA512:
6
- metadata.gz: fa75f4a64f4b4e86003162acdb220327ee33140b05c3beccf03784a376d3651d5198bd860fc1d3267414031dd6c3a9bba0475daa41a782b1d5bdc2690735e8a5
7
- data.tar.gz: 2f75e695c3b0d19ce70cbb754a40e1a656d687ec36c9360663e5296dc547c197cea30a1b493c6f94766610338767cb7147058022179eec7444f6e99dc2fbfa25
6
+ metadata.gz: 5d94d17e706c4e19f20247b0d89c4a7406ae08fb31cdc45af2eb692db922e0bff151155449199be49620a1d371ae02b312c9cdf56817adf9eba3c1a6841a4ef5
7
+ data.tar.gz: 96941dcc369fa5830d532ddb3f4881e4e8fbc476734bc6e97e32fcbba474ceacd99d03a13fa72df82600ec734145b5fd5ecd435a869231872d21625ccb390e5f
@@ -0,0 +1,17 @@
1
+ require 'dmao/ingesters/errors/ingest_entity_error'
2
+
3
+ module DMAO
4
+ module Ingesters
5
+ module Errors
6
+
7
+ class IngestProjectError < IngestEntityError
8
+
9
+ def initialize(msg="Error ingesting project.")
10
+ super(msg)
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -14,6 +14,7 @@ module DMAO
14
14
  ENTITY_ERROR = nil
15
15
  INVALID_ENTITY_ERROR = nil
16
16
  ENTITY_ERROR_MESSAGE = nil
17
+ ERROR_HANDLING = nil
17
18
 
18
19
  def initialize(api_url=nil, api_token=nil, institution_id=nil)
19
20
 
@@ -34,13 +35,19 @@ module DMAO
34
35
  self.class.send(:define_method, add_method_name, Proc.new {|attributes| add_entity(attributes)}) unless self.respond_to?(add_method_name, include_all: true)
35
36
  self.class.send(:define_method, update_method_name, Proc.new {|id, attributes| update_entity(id, attributes)}) unless self.respond_to?(update_method_name, include_all: true)
36
37
  self.class.send(:define_method, ingest_method_name, Proc.new {|attributes={}| ingest_entity(attributes)}) unless self.respond_to?(ingest_method_name, include_all: true)
37
-
38
38
  end
39
39
 
40
40
  def self.entity_name
41
41
  self::ENTITY.to_s.split('::')[-1].gsub(/[A-Z]/, " \\0").downcase.strip
42
42
  end
43
43
 
44
+ def generic_errors
45
+ {
46
+ "DMAO::API::Errors::InstitutionNotFound" => "Institution not found, cannot ingest #{self.class.entity_name} to non-existent institution",
47
+ "DMAO::API::Errors::EntityNotFound" => "#{self.class.entity_name.capitalize} not found, cannot update #{self.class.entity_name} that does not exist"
48
+ }
49
+ end
50
+
44
51
  def ingest
45
52
  raise DMAO::Ingesters::Errors::GenericIngester.new("Calling ingest on generic ingester is not allowed.")
46
53
  end
@@ -86,27 +93,40 @@ module DMAO
86
93
 
87
94
  private
88
95
 
89
- def add_entity attributes={}
96
+ def perform_entity_action action, arguments
97
+
98
+ errors_to_handle = self.class::ERROR_HANDLING.nil? ? generic_errors : self.class::ERROR_HANDLING
90
99
 
91
100
  begin
92
- self.class::ENTITY.create attributes
93
- rescue DMAO::API::Errors::InstitutionNotFound
94
- raise self.class::ENTITY_ERROR.new("Institution not found, cannot ingest #{self.class.entity_name} to non-existent institution")
95
- rescue self.class::INVALID_ENTITY_ERROR => e
101
+ self.class::ENTITY.send(action, *arguments)
102
+ rescue ingest_errors(errors_to_handle) => e
103
+ error_message = errors_to_handle[e.class.to_s].nil? ? errors_to_handle[(e.class.ancestors.map {|a| a.to_s} & errors_to_handle.keys).first] : errors_to_handle[e.class.to_s]
104
+ raise self.class::ENTITY_ERROR.new(error_message)
105
+ rescue self.class::ENTITY::INVALID_ENTITY_CLASS => e
96
106
  parse_unprocessable_errors(e.errors)
97
107
  end
98
108
 
99
109
  end
100
110
 
111
+ def ingest_errors(errors={})
112
+ Class.new do
113
+ def self.===(other)
114
+ !(other.class.ancestors.map {|a| a.to_s} & @errors.keys).empty?
115
+ end
116
+ end.tap do |c|
117
+ c.instance_variable_set(:@errors, errors)
118
+ end
119
+ end
120
+
121
+ def add_entity attributes={}
122
+
123
+ perform_entity_action "create", [attributes]
124
+
125
+ end
126
+
101
127
  def update_entity entity_id, attributes={}
102
128
 
103
- begin
104
- self.class::ENTITY.update entity_id, attributes
105
- rescue DMAO::API::Errors::EntityNotFound
106
- raise self.class::ENTITY_ERROR.new("#{self.class.entity_name.capitalize} not found, cannot update #{self.class.entity_name} that does not exist")
107
- rescue self.class::INVALID_ENTITY_ERROR => e
108
- parse_unprocessable_errors(e.errors)
109
- end
129
+ perform_entity_action "update", [entity_id, attributes]
110
130
 
111
131
  end
112
132
 
@@ -15,6 +15,11 @@ module DMAO
15
15
  ENTITY = DMAO::API::OrganisationUnit
16
16
  ENTITY_ERROR = DMAO::Ingesters::Errors::IngestOrganisationUnitError
17
17
  ENTITY_ERROR_MESSAGE = "Invalid organisation unit details"
18
+ ERROR_HANDLING = {
19
+ "DMAO::API::Errors::OrganisationUnitNotFound" => "Organisation unit not found, cannot update organisation unit that does not exist",
20
+ "DMAO::API::Errors::InstitutionNotFound" => "Institution not found, cannot ingest organisation unit to non-existent institution",
21
+ "DMAO::API::Errors::InvalidParentId" => "Parent ID not found in DMA Online, cannot link organisation unit to non-existent parent"
22
+ }
18
23
 
19
24
  def link_child_to_parent child_system_uuid, parent_system_uuid
20
25
 
@@ -29,46 +34,8 @@ module DMAO
29
34
 
30
35
  end
31
36
 
32
- def add_organisation_unit attributes
33
-
34
- begin
35
- DMAO::API::OrganisationUnit.create attributes
36
- rescue DMAO::API::Errors::InstitutionNotFound
37
- raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Institution not found, cannot ingest organisation unit to non-existent institution")
38
- rescue DMAO::API::Errors::InvalidParentId
39
- raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Parent ID not found in DMA Online, cannot link organisation unit to non-existent parent")
40
- rescue DMAO::API::Errors::InvalidOrganisationUnit => e
41
- parse_unprocessable_errors(e.errors)
42
- end
43
-
44
- end
45
-
46
- def update_organisation_unit id, attributes
47
-
48
- begin
49
-
50
- DMAO::API::OrganisationUnit.update id, attributes
51
-
52
- rescue DMAO::API::Errors::OrganisationUnitNotFound
53
- raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Organisation unit not found, cannot update organisation unit that does not exist")
54
- rescue DMAO::API::Errors::InvalidParentId
55
- raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Parent ID not found in DMA Online, cannot link organisation unit to non-existent parent")
56
- rescue DMAO::API::Errors::InvalidOrganisationUnit => e
57
- parse_unprocessable_errors(e.errors)
58
- end
59
-
60
- end
61
-
62
37
  protected
63
38
 
64
- def add_entity attributes
65
- add_organisation_unit attributes
66
- end
67
-
68
- def update_entity id, attributes
69
- update_organisation_unit id, attributes
70
- end
71
-
72
39
  def find_unit_or_raise_link_error unit_system_uuid, error_message
73
40
 
74
41
  begin
@@ -0,0 +1,23 @@
1
+ require 'dmao/ingesters/generic/ingester'
2
+ require 'dmao/ingesters/errors/ingest_project_error'
3
+
4
+ module DMAO
5
+ module Ingesters
6
+ module Generic
7
+
8
+ class ProjectsIngester < Ingester
9
+
10
+ ENTITY = DMAO::API::Project
11
+ ENTITY_ERROR = DMAO::Ingesters::Errors::IngestProjectError
12
+ ENTITY_ERROR_MESSAGE = "Invalid project details"
13
+ ERROR_HANDLING = {
14
+ "DMAO::API::Errors::ProjectNotFound" => "Project not found, cannot update project that does not exist",
15
+ "DMAO::API::Errors::InstitutionNotFound" => "Institution not found, cannot ingest project to non-existent institution",
16
+ "DMAO::API::Errors::InvalidOrganisationUnitID" => "Organisation unit not found, cannot ingest project linked to non-existent organisation unit"
17
+ }
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -1,7 +1,7 @@
1
1
  module DMAO
2
2
  module Ingesters
3
3
  module Generic
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dmao-generic-ingesters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.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-04-19 00:00:00.000000000 Z
11
+ date: 2017-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dmao_api
@@ -129,12 +129,14 @@ files:
129
129
  - lib/dmao/ingesters/errors/ingest_funder_error.rb
130
130
  - lib/dmao/ingesters/errors/ingest_organisation_unit_error.rb
131
131
  - lib/dmao/ingesters/errors/ingest_person_error.rb
132
+ - lib/dmao/ingesters/errors/ingest_project_error.rb
132
133
  - lib/dmao/ingesters/errors/link_organisation_unit_error.rb
133
134
  - lib/dmao/ingesters/generic.rb
134
135
  - lib/dmao/ingesters/generic/funders_ingester.rb
135
136
  - lib/dmao/ingesters/generic/ingester.rb
136
137
  - lib/dmao/ingesters/generic/organisation_ingester.rb
137
138
  - lib/dmao/ingesters/generic/people_ingester.rb
139
+ - lib/dmao/ingesters/generic/projects_ingester.rb
138
140
  - lib/dmao/ingesters/generic/version.rb
139
141
  homepage: https://github.com/lulibrary/DMAO-Generic-Ingesters
140
142
  licenses: