lib/dmao/ingesters/generic/ingester.rb in dmao-generic-ingesters-0.3.0 vs lib/dmao/ingesters/generic/ingester.rb in dmao-generic-ingesters-0.4.0
- old
+ new
@@ -1,18 +1,20 @@
require 'time'
require 'dmao_api'
require 'dmao/ingesters/errors/generic_ingester'
require 'dmao/ingesters/errors/empty_attributes'
+require 'dmao/ingesters/errors/ingest_entity_error'
module DMAO
module Ingesters
module Generic
class Ingester
ENTITY = nil
ENTITY_ERROR = nil
+ INVALID_ENTITY_ERROR = nil
ENTITY_ERROR_MESSAGE = nil
def initialize(api_url=nil, api_token=nil, institution_id=nil)
DMAO::API.configure do |config|
config.base_url = api_url
@@ -25,12 +27,10 @@
raise DMAO::Ingesters::Errors::GenericIngester.new("Calling ingest on generic ingester is not allowed.")
end
def ingest_entity attributes = {}
- entity_name = self.class::ENTITY.to_s.split('::')[-1].gsub(/[A-Z]/, " \\0").strip.downcase
-
raise DMAO::Ingesters::Errors::EmptyAttributes.new("Cannot ingest #{entity_name} without attributes.") if attributes.nil? || attributes.empty?
begin
entity = self.class::ENTITY.find_by_system_uuid attributes[:system_uuid]
@@ -64,15 +64,35 @@
raise self.class::ENTITY_ERROR.new("#{self.class::ENTITY_ERROR_MESSAGE}, #{error_messages}")
end
- def add_entity _attributes={}
- raise NotImplementedError
+ def add_entity attributes={}
+
+ begin
+ self.class::ENTITY.create attributes
+ rescue DMAO::API::Errors::InstitutionNotFound
+ raise self.class::ENTITY_ERROR.new("Institution not found, cannot ingest #{entity_name} to non-existent institution")
+ rescue self.class::INVALID_ENTITY_ERROR => e
+ parse_unprocessable_errors(e.errors)
+ end
+
end
- def update_entity _entity_id, _attributes={}
- raise NotImplementedError
+ def update_entity entity_id, attributes={}
+
+ begin
+ self.class::ENTITY.update entity_id, attributes
+ rescue DMAO::API::Errors::EntityNotFound
+ raise self.class::ENTITY_ERROR.new("#{entity_name.capitalize} not found, cannot update #{entity_name} that does not exist")
+ rescue self.class::INVALID_ENTITY_ERROR => e
+ parse_unprocessable_errors(e.errors)
+ end
+
+ end
+
+ def entity_name
+ self.class::ENTITY.to_s.split('::')[-1].gsub(/[A-Z]/, " \\0").strip.downcase
end
end
end
\ No newline at end of file