Class: ConnectorsSdk::Base::Adapter

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

Class Method Summary collapse

Class Method Details

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



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

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



51
52
53
# File 'lib/connectors_sdk/base/adapter.rb', line 51

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

.fields_to_preserveObject



18
19
20
21
22
23
24
# File 'lib/connectors_sdk/base/adapter.rb', line 18

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

.generate_id_helpers(method_prefix, id_prefix) ⇒ Object



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

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



44
45
46
47
48
49
# File 'lib/connectors_sdk/base/adapter.rb', line 44

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)
  ConnectorsShared::ExtensionMappingUtil.get_mime_types(extension)&.first
end

.normalize_date(date) ⇒ Object



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

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
      ConnectorsShared::ExceptionTracking.capture_exception(e)
      nil
    end
  end
end

.normalize_enum(enum) ⇒ Object



59
60
61
# File 'lib/connectors_sdk/base/adapter.rb', line 59

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

.normalize_path(path) ⇒ Object



79
80
81
82
83
# File 'lib/connectors_sdk/base/adapter.rb', line 79

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

.strip_file_extension(file_name) ⇒ Object



55
56
57
# File 'lib/connectors_sdk/base/adapter.rb', line 55

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

.url_to_path(url) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/connectors_sdk/base/adapter.rb', line 85

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