Class: Edi::AmazonVc::OldRetriever

Inherits:
BaseService show all
Defined in:
app/services/edi/amazon_vc/old_retriever.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



68
69
70
71
72
73
74
75
# File 'app/services/edi/amazon_vc/old_retriever.rb', line 68

def instantiate_transporter(transporter, transporter_profile = nil)
  case transporter
  when :http_seller_api
    Transport::HttpSellerApiConnection.new({profile: transporter_profile})
  else
    raise "Unknown transporter: #{transporter}"
  end
end

#process(transporter: :http_seller_api, transporter_profile: nil, remote_path: nil, partner:, category:, data_type: 'json', store_to_upload: false, http_method: 'GET', data_to_send: '', file_info: {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/edi/amazon_vc/old_retriever.rb', line 6

def process(transporter: :http_seller_api,
            transporter_profile: nil, # Transporter profile for sftp connection, see secrets.yml
            remote_path: nil,
            partner:, # Name of partner for creating log entry
            category:, # Category for log entry
            data_type: 'json',
            store_to_upload: false,
            http_method: 'GET', # we are patching this to allow a PATCH with mode=VALIDATION_PREVIEW
            data_to_send: '', # we are patching this to allow a PATCH with data a simple hash to update sku to itself
            file_info: {}) # By default the file content is stored to the log directly, if true then an upload is created )
  Rails.logger.debug("Edi::AmazonVc::Retriever#Retriever, remote_path: #{remote_path}")
  transport = instantiate_transporter(transporter, transporter_profile)
  res = transport.send_data(data_to_send, remote_path, http_method)
  Rails.logger.debug("Edi::AmazonVc::Retriever#Retriever, res: #{res}")
  edi_logs = []
  EdiCommunicationLog.transaction do
    json_hash = nil
    if res[:success] && (data = res[:http_result]&.body.to_s).present?
      json_hash = JSON.parse(data).with_indifferent_access
      Rails.logger.debug("Edi::AmazonVc::Retriever#Retriever, json_hash: #{json_hash}")
      data_blob = nil
      data_blob = json_hash.to_json unless store_to_upload
    end
    if json_hash && json_hash[:payload] && json_hash[:payload].any?{|k,v| v.any?} # just skip generating edi communication logs if no records to process
      if store_to_upload
        json_hash[:payload][:packingSlips].each do |psh|
          edi_log = EdiCommunicationLog.new(partner: partner,
                                            category: category,
                                            data: psh.to_json,
                                            data_type: data_type,
                                            transmit_datetime: Time.current)
          edi_log.save!
          upload = Upload.uploadify_from_data(file_name: "#{psh[:purchaseOrderNumber]}_packing_slip.pdf", data: Base64.decode64(psh[:content]), category: 'custom_packing_slip_pdf', resource: edi_log)
          raise "Could not uploadify file #{file_name}" unless upload.persisted?
          logger.info "saved to edi log #{edi_log.id}"
          edi_logs << edi_log
        end
      else
        edi_log = EdiCommunicationLog.new(partner: partner,
                                          category: category,
                                          data: data_blob,
                                          data_type: data_type,
                                          transmit_datetime: Time.current)
        edi_log.save!
        logger.info "saved to edi log #{edi_log.id}"
        edi_logs << edi_log
      end
    elsif category == :catalog_item_information && !data_blob.nil?
      edi_log = EdiCommunicationLog.new(partner: partner,
                                        category: category,
                                        data: data_blob,
                                        data_type: data_type,
                                        transmit_datetime: Time.current)
      edi_log.save!
      logger.info "saved to edi log #{edi_log.id}"
      edi_logs << edi_log
    end
  end
  Rails.logger.debug("Edi::Amazon::Retriever#Retriever, edi_logs: #{edi_logs}")
  edi_logs
end