Class: DMAO::Ingesters::Generic::Ingester

Inherits:
Object
  • Object
show all
Defined in:
lib/dmao/ingesters/generic/ingester.rb

Constant Summary collapse

ENTITY =
nil
ENTITY_ERROR =
nil
INVALID_ENTITY_ERROR =
nil
ENTITY_ERROR_MESSAGE =
nil
ERROR_HANDLING =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url = nil, api_token = nil, institution_id = nil) ⇒ Ingester

Returns a new instance of Ingester.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dmao/ingesters/generic/ingester.rb', line 19

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

Class Method Details

.entity_nameObject



40
41
42
# File 'lib/dmao/ingesters/generic/ingester.rb', line 40

def self.entity_name
  self::ENTITY.to_s.split('::')[-1].gsub(/[A-Z]/, " \\0").downcase.strip
end

Instance Method Details

#generic_errorsObject



44
45
46
47
48
49
# File 'lib/dmao/ingesters/generic/ingester.rb', line 44

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

#ingestObject



51
52
53
# File 'lib/dmao/ingesters/generic/ingester.rb', line 51

def ingest
  raise DMAO::Ingesters::Errors::GenericIngester.new("Calling ingest on generic ingester is not allowed.")
end

#ingest_entity(attributes = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dmao/ingesters/generic/ingester.rb', line 55

def ingest_entity attributes = {}

  validate_attributes_present attributes

  begin

    entity = self.class::ENTITY.find_by_system_uuid attributes[:system_uuid]

    current_modified_at = Time.parse(entity.system_modified_at)
    new_modified_at = Time.parse(attributes[:system_modified_at])

    return false if current_modified_at >= new_modified_at

    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 #{self.class.entity_name} returned for system uuid #{attributes[:system_uuid]}.")
  end

end

#parse_unprocessable_errors(errors) ⇒ Object

Raises:

  • (self.class::ENTITY_ERROR)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dmao/ingesters/generic/ingester.rb', line 78

def parse_unprocessable_errors errors

  error_messages = ""

  errors.each_with_index do |(k, v), index|
    v.each_with_index do |msg, msg_index|
      error_messages += "#{k} - #{msg}"
      error_messages += ", " unless msg_index == v.size - 1
    end
    error_messages += ", " unless index == errors.size - 1
  end

  raise self.class::ENTITY_ERROR.new("#{self.class::ENTITY_ERROR_MESSAGE}, #{error_messages}")

end