Class: Edi::Sender

Inherits:
BaseService show all
Defined in:
app/services/edi/sender.rb

Overview

I connect to a transport, retrieve files and save them in the Edi Communication log

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'app/services/edi/sender.rb', line 28

def instantiate_transporter(transporter, transporter_profile = nil)
  case transporter
  when :sftp
    'Transport::SftpConnection'.constantize.new(transporter_profile)
  when :http_api
    Transport::HttpApiConnection.new
  else
    raise "Unknown transporter: #{transporter}"
  end
end

#process(transporter:, transporter_profile: nil, remote_path:, partner:, category:, edi_communication_logs: nil) ⇒ Object

What type of transporter? sftp supported at the moment



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/edi/sender.rb', line 5

def process(transporter:, # What type of transporter? sftp supported at the moment
            transporter_profile: nil, # What connection profile to use? see secrets.yml for options
            remote_path:, # Which directory to upload to (no trailing /)
            partner:, # Which partner to filter edi communication log on ?
            category:,
            edi_communication_logs: nil) # Which category to pull, e.g order_confirm
  transport = instantiate_transporter(transporter, transporter_profile)
  edi_communication_logs ||= EdiCommunicationLog.requiring_processing.where(partner: partner).where(category: category).order(:created_at)
  [edi_communication_logs].flatten.each do |ecl|
    logger.info "Sending #{category} file #{ecl.file_name} to #{remote_path} using transporter profile #{transporter_profile}"
    # 12345678.md56816be8c9e5f6f4a27b9b8911b87a4fa.neworders
    remote_file_path = "#{remote_path}/#{ecl.file_name}"
    res = transport.send_data(ecl.data, remote_file_path)
    ecl.transmit_datetime = Time.current
    if res
      ecl.complete!
    else
      ecl.error
    end
  end
  edi_communication_logs
end