Class: Edi::Wayfair::Sender

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

Overview

Sends pending EDI communication log entries to Wayfair over a transport,
marking each entry complete or errored based on the HTTP result.

Constant Summary

Constants included from RequestIdentifiable

RequestIdentifiable::REQUEST_ID_HEADERS

Constants included from AddressAbbreviator

AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseEdiService

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

Methods included from RequestIdentifiable

#partner_request_id

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, #tagged_logger

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Transport::HttpGraphqlApiConnection

Builds the transport connection for the given adapter key.

Parameters:

  • transporter (Symbol)

    transport adapter key

  • transporter_profile (String, nil) (defaults to: nil)

    connection profile name

Returns:

Raises:

  • (RuntimeError)

    when the transporter key is unknown



47
48
49
50
51
52
53
54
# File 'app/services/edi/wayfair/sender.rb', line 47

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

#process(transporter: :http_graphql_api, transporter_profile: nil, partner:, category:, edi_communication_logs: nil) ⇒ Array<EdiCommunicationLog>, ActiveRecord::Relation

Sends each matching log entry and records the outcome on it.

Parameters:

  • transporter (Symbol) (defaults to: :http_graphql_api)

    transport adapter key (currently only :http_graphql_api)

  • transporter_profile (String, nil) (defaults to: nil)

    connection profile name (see secrets.yml)

  • partner (String)

    partner used to filter the communication log

  • category (String)

    category to send, e.g. order_confirm

  • edi_communication_logs (Array<EdiCommunicationLog>, ActiveRecord::Relation, nil) (defaults to: nil)

    entries to send; defaults to unprocessed entries for the partner/category

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/edi/wayfair/sender.rb', line 19

def process(transporter: :http_graphql_api,
            transporter_profile: nil, # What connection profile to use? see secrets.yml for options
            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} data #{ecl.data} to using transporter: #{transporter} using profile #{transporter_profile}"
    res = transport.send_data(ecl.data)
    ecl.transmit_datetime = Time.current
    ecl.notes = "HTTP CODE: #{res[:http_result]&.status}, ATTEMPT NUMBER: #{res[:attempt_number_reached]}, HTTP BODY: #{res[:http_result]&.body}"
    logger.info "Result: HTTP CODE: #{res[:http_result]&.status}, HTTP BODY: #{res[:http_result]&.body}"
    if res[:success]
      ecl.complete!
    else
      ecl.error
    end
  end
  edi_communication_logs
end