Class: Edi::MiraklSeller::Sender

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

Constant Summary

Constants included from AddressAbbreviator

AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

Instance Method Summary collapse

Methods inherited from BaseEdiService

#duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #report_order_creation_issues, #safe_process_edi_communication_log

Methods included from AddressAbbreviator

#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes

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 Edi::BaseEdiService

Instance Method Details

#instantiate_transporter(_transporter, transporter_profile = nil) ⇒ Object



52
53
54
55
# File 'app/services/edi/mirakl_seller/sender.rb', line 52

def instantiate_transporter(_transporter, transporter_profile = nil)
  # It doesn't matter what we get in the params, mirakl only has 1 transporter
  Transport::MiraklTransporter.new({ profile: transporter_profile })
end

#process(remote_path:, partner:, category:, transporter: :http_api, transporter_send_method: 'post', transporter_profile: nil, data_type: 'xml', edi_communication_logs: nil) ⇒ 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
# File 'app/services/edi/mirakl_seller/sender.rb', line 6

def process(remote_path:, partner:, category:, transporter: :http_api,
            transporter_send_method: 'post',
            transporter_profile: nil,
            data_type: 'xml',
            edi_communication_logs: nil) # Which category to pull, e.g order_confirm
  transport = instantiate_transporter(transporter, transporter_profile)
  options = {}

  options[:import_mode] = 'NORMAL' if category.present? && category == :price_advice
  edi_communication_logs ||= EdiCommunicationLog.requiring_processing.where(partner: partner).where(category: category).order(:created_at)
  [edi_communication_logs].flatten.each do |ecl|
    if remote_path.include?('{order_id}')
      order = ecl.edi_documents.where.not(order_id: nil).first.order
      remote_path = remote_path.gsub! '{order_id}', order.edi_transaction_id
    end
    logger.info "Sending #{category} data #{ecl.data} to #{remote_path} using transporter method: #{transporter_send_method} using profile #{transporter_profile}"
    res = if valid_json?(ecl.data)
            transport.send_data(ecl.data, remote_path)
          else
            transport.send_mirakl_data(ecl.data, remote_path, ecl.file_name, options)
          end
    ecl.transmit_datetime = Time.current
    ecl.notes = "HTTP CODE: #{res[:http_result].try(:code)}, HTTP BODY: #{res[:http_result].try(:body)}"
    logger.info "Result: HTTP CODE: #{res[:http_result].try(:code)}, HTTP BODY: #{res[:http_result].try(:body)}"
    if res[:success]
      if (data = res[:http_result]&.body.to_s).present?
        json_hash = JSON.parse(data).with_indifferent_access
        import_id = json_hash.dig('import_id')
        # store it
        ecl.transaction_id = import_id
      end
      ecl.transmit_datetime = Time.current
      ecl.start_process!
    else
      ecl.error
    end
  end
  edi_communication_logs
end

#valid_json?(string) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'app/services/edi/mirakl_seller/sender.rb', line 46

def valid_json?(string)
  !!(JSON.parse(string) || true)
rescue StandardError
  false
end