Sha256: b95179c983cc6edd2721351ce5ddef84e6e36661ca70fe17b6ffb0ddaaacf91f

Contents?: true

Size: 1015 Bytes

Versions: 1

Compression:

Stored size: 1015 Bytes

Contents

module Workarea
  module FlowIo
    # Fetches new import CSVs from the FTP server, ignoring any that
    # have already been processed.
    class FetchImport
      include Sidekiq::Worker

      sidekiq_options lock: :until_executing

      def perform
        username = FlowIo.credentials[:ftp_username]
        password = FlowIo.credentials[:ftp_password]
        path = Pathname.new('flow').join('from-flow', 'export')

        Net::SFTP.start(FlowIo::FTP_HOST, username, password: password) do |sftp|
          sftp.dir.glob(path.to_s, '*.csv') do |entry|
            Import.find_or_create_by!(name: entry.name) do |import|
              import.file = Tempfile.new(entry.name).tap do |tmp|
                begin
                  filepath = path.join(entry.name).to_s
                  csv = sftp.download!(filepath)

                  tmp.write(csv)
                ensure
                  tmp.close
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workarea-flow_io-1.2.1 app/workers/workarea/flow_io/fetch_import.rb