Class: Outhad::Integrations::Source::Sftp::Client

Inherits:
SourceConnector
  • Object
show all
Defined in:
lib/outhad/integrations/source/sftp/client.rb

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/outhad/integrations/source/sftp/client.rb', line 7

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  create_connection(connection_config)
  if @sftp.stat!(@remote_file_path)
    success_status
  else
    failure_status(nil)
  end
rescue StandardError => e
  handle_exception(e, {
                     context: "SFTP:CHECK_CONNECTION:EXCEPTION",
                     type: "error"
                   })
  failure_status(e)
end

#discover(connection_config) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/outhad/integrations/source/sftp/client.rb', line 23

def discover(connection_config)
  connection_config = connection_config.with_indifferent_access
  db = create_connection(connection_config)
  @sftp.download!(@remote_file_path, @tempfile.path)
  query = "SELECT * FROM read_csv_auto('#{@tempfile.path}')"
  records = db.query(query).columns
  catalog = Catalog.new(streams: create_streams(records.map(&:name)))
  catalog.to_outhad_message
rescue StandardError => e
  handle_exception(e, {
                     context: "SFTP:DISCOVER:EXCEPTION",
                     type: "error"
                   })
ensure
  @tempfile&.close!
end

#read(sync_config) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/outhad/integrations/source/sftp/client.rb', line 40

def read(sync_config)
  connection_config = sync_config.source.connection_specification
  connection_config = connection_config.with_indifferent_access
  conn = create_connection(connection_config)
  query = sync_config.model.query
  query = batched_query(query, sync_config.limit, sync_config.offset) unless sync_config.limit.nil? && sync_config.offset.nil?
  query(conn, query)
rescue StandardError => e
  handle_exception(e, {
                     context: "SFTP:READ:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end