lib/dmao/ingesters/generic/ingester.rb in dmao-generic-ingesters-0.4.0 vs lib/dmao/ingesters/generic/ingester.rb in dmao-generic-ingesters-0.5.0

- old
+ new

@@ -14,24 +14,42 @@ 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 config.api_token = api_token config.institution_id = institution_id end + + if self.class::ENTITY.nil? + raise "Entity not set" + end + + add_method_name = "add_#{self.class.entity_name.tr(' ', '_')}" + update_method_name = "update_#{self.class.entity_name.tr(' ', '_')}" + 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 ingest raise DMAO::Ingesters::Errors::GenericIngester.new("Calling ingest on generic ingester is not allowed.") end def ingest_entity attributes = {} - raise DMAO::Ingesters::Errors::EmptyAttributes.new("Cannot ingest #{entity_name} without attributes.") if attributes.nil? || attributes.empty? + raise DMAO::Ingesters::Errors::EmptyAttributes.new("Cannot ingest #{self.class.entity_name} without attributes.") if attributes.nil? || attributes.empty? begin entity = self.class::ENTITY.find_by_system_uuid attributes[:system_uuid] @@ -43,11 +61,11 @@ update_entity entity.id, attributes rescue DMAO::API::Errors::EntityNotFound add_entity attributes rescue DMAO::API::Errors::InvalidResponseLength - raise self.class::ENTITY_ERROR.new("More than one #{entity_name} returned for system uuid #{attributes[:system_uuid]}.") + raise self.class::ENTITY_ERROR.new("More than one #{self.class.entity_name} returned for system uuid #{attributes[:system_uuid]}.") end end def parse_unprocessable_errors errors @@ -64,16 +82,18 @@ raise self.class::ENTITY_ERROR.new("#{self.class::ENTITY_ERROR_MESSAGE}, #{error_messages}") end + private + 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") + 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 parse_unprocessable_errors(e.errors) end end @@ -81,18 +101,14 @@ 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") + 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 - 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