Class: Connectors::Base::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/connectors/base/adapter.rb

Direct Known Subclasses

GitLab::Adapter

Class Method Summary collapse

Class Method Details

.es_document_from_configured_object_base(object_type:, object:, fields:) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/connectors/base/adapter.rb', line 96

def self.es_document_from_configured_object_base(object_type:, object:, fields:)
  object_as_json = object.as_json

  adapted_object = {
    :type => normalize_enum(object_type)
  }

  fields.each do |field_data|
    remote_field_name = field_data.fetch(:remote)

    value = object_as_json[remote_field_name]
    value = object_as_json.dig(*remote_field_name.split('.')) if value.blank?
    next if value.nil?

    adapted_object[field_data.fetch(:target)] = value
  end

  adapted_object.symbolize_keys
end

.extension_for_file(file_name) ⇒ Object



53
54
55
# File 'lib/connectors/base/adapter.rb', line 53

def self.extension_for_file(file_name)
  File.extname(file_name.downcase).delete_prefix!('.')
end

.fields_to_preserveObject



20
21
22
23
24
25
26
# File 'lib/connectors/base/adapter.rb', line 20

def self.fields_to_preserve
  @fields_to_preserve ||= ['body']
    .concat(Utility::Constants::THUMBNAIL_FIELDS)
    .concat(Utility::Constants::SUBEXTRACTOR_RESERVED_FIELDS)
    .map(&:freeze)
    .freeze
end

.generate_id_helpers(method_prefix, id_prefix) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/connectors/base/adapter.rb', line 28

def self.generate_id_helpers(method_prefix, id_prefix)
  define_singleton_method("#{method_prefix}_id_to_es_id") do |id|
    "#{id_prefix}_#{id}"
  end

  define_singleton_method("es_id_is_#{method_prefix}_id?") do |es_id|
    regex_match = /#{id_prefix}_(.+)$/.match(es_id)
    regex_match.present? && regex_match.size == 2
  end

  define_singleton_method("es_id_to_#{method_prefix}_id") do |es_id|
    regex_match = /#{id_prefix}_(.+)$/.match(es_id)

    raise ArgumentError, "Invalid id #{es_id} for source with method prefix #{method_prefix}." if regex_match.nil? || regex_match.length != 2
    regex_match[1]
  end
end

.mime_type_for_file(file_name) ⇒ Object



46
47
48
49
50
51
# File 'lib/connectors/base/adapter.rb', line 46

def self.mime_type_for_file(file_name)
  ruby_detected_type = MIME::Types.type_for(file_name)
  return ruby_detected_type.first.simplified if ruby_detected_type.present?
  extension = extension_for_file(file_name)
  Utility::ExtensionMappingUtil.get_mime_types(extension)&.first
end

.normalize_date(date) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/connectors/base/adapter.rb', line 65

def self.normalize_date(date)
  return nil if date.blank?

  case date
  when Date, Time, DateTime, ActiveSupport::TimeWithZone
    date.to_datetime.rfc3339
  else
    begin
      Time.zone.parse(date).to_datetime.rfc3339
    rescue ArgumentError, TypeError => e
      Utility::ExceptionTracking.capture_exception(e)
      nil
    end
  end
end

.normalize_enum(enum) ⇒ Object



61
62
63
# File 'lib/connectors/base/adapter.rb', line 61

def self.normalize_enum(enum)
  enum&.to_s&.downcase
end

.normalize_path(path) ⇒ Object



81
82
83
84
85
# File 'lib/connectors/base/adapter.rb', line 81

def self.normalize_path(path)
  return nil if path.blank?
  return path if path.start_with?('/')
  "/#{path}"
end

.strip_file_extension(file_name) ⇒ Object



57
58
59
# File 'lib/connectors/base/adapter.rb', line 57

def self.strip_file_extension(file_name)
  File.basename(file_name, File.extname(file_name))
end

.url_to_path(url) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/connectors/base/adapter.rb', line 87

def self.url_to_path(url)
  return nil if url.blank?
  uri = URI(url)
  return nil if uri.scheme.blank?
  normalize_path(uri.path)
rescue URI::InvalidURIError, ArgumentError
  nil
end