lib/dmao/ingesters/generic/ingester.rb in dmao-generic-ingesters-0.5.0 vs lib/dmao/ingesters/generic/ingester.rb in dmao-generic-ingesters-0.6.0
- old
+ new
@@ -12,10 +12,11 @@
ENTITY = nil
ENTITY_ERROR = nil
INVALID_ENTITY_ERROR = nil
ENTITY_ERROR_MESSAGE = nil
+ ERROR_HANDLING = nil
def initialize(api_url=nil, api_token=nil, institution_id=nil)
DMAO::API.configure do |config|
config.base_url = api_url
@@ -32,17 +33,23 @@
ingest_method_name = "ingest_#{self.class.entity_name.tr(' ', '_')}"
self.class.send(:define_method, add_method_name, Proc.new {|attributes| add_entity(attributes)}) unless self.respond_to?(add_method_name, include_all: true)
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)
self.class.send(:define_method, ingest_method_name, Proc.new {|attributes={}| ingest_entity(attributes)}) unless self.respond_to?(ingest_method_name, include_all: true)
-
end
def self.entity_name
self::ENTITY.to_s.split('::')[-1].gsub(/[A-Z]/, " \\0").downcase.strip
end
+ def generic_errors
+ {
+ "DMAO::API::Errors::InstitutionNotFound" => "Institution not found, cannot ingest #{self.class.entity_name} to non-existent institution",
+ "DMAO::API::Errors::EntityNotFound" => "#{self.class.entity_name.capitalize} not found, cannot update #{self.class.entity_name} that does not exist"
+ }
+ end
+
def ingest
raise DMAO::Ingesters::Errors::GenericIngester.new("Calling ingest on generic ingester is not allowed.")
end
def ingest_entity attributes = {}
@@ -84,30 +91,43 @@
end
private
- def add_entity attributes={}
+ def perform_entity_action action, arguments
+ errors_to_handle = self.class::ERROR_HANDLING.nil? ? generic_errors : self.class::ERROR_HANDLING
+
begin
- self.class::ENTITY.create attributes
- rescue DMAO::API::Errors::InstitutionNotFound
- raise self.class::ENTITY_ERROR.new("Institution not found, cannot ingest #{self.class.entity_name} to non-existent institution")
- rescue self.class::INVALID_ENTITY_ERROR => e
+ self.class::ENTITY.send(action, *arguments)
+ rescue ingest_errors(errors_to_handle) => e
+ 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]
+ raise self.class::ENTITY_ERROR.new(error_message)
+ rescue self.class::ENTITY::INVALID_ENTITY_CLASS => e
parse_unprocessable_errors(e.errors)
end
end
+ def ingest_errors(errors={})
+ Class.new do
+ def self.===(other)
+ !(other.class.ancestors.map {|a| a.to_s} & @errors.keys).empty?
+ end
+ end.tap do |c|
+ c.instance_variable_set(:@errors, errors)
+ end
+ end
+
+ def add_entity attributes={}
+
+ perform_entity_action "create", [attributes]
+
+ end
+
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("#{self.class.entity_name.capitalize} not found, cannot update #{self.class.entity_name} that does not exist")
- rescue self.class::INVALID_ENTITY_ERROR => e
- parse_unprocessable_errors(e.errors)
- end
+ perform_entity_action "update", [entity_id, attributes]
end
end
\ No newline at end of file