Class: Edi::Wayfair::Retriever

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

Overview

Retrieves files from a Wayfair transport and stores successful responses
in the EDI communication log for later processing.

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

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



54
55
56
57
58
59
60
61
# File 'app/services/edi/wayfair/retriever.rb', line 54

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, query: nil, partner:, category:, data_type: 'json') ⇒ Array<EdiCommunicationLog>

Runs the retrieval and persists any purchase order payloads found.

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)

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

    GraphQL query payload to send

  • partner (String)

    partner name recorded on the log entry

  • category (String)

    category recorded on the log entry

  • data_type (String) (defaults to: 'json')

    data format recorded on the log entry

Returns:



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
# File 'app/services/edi/wayfair/retriever.rb', line 19

def process(transporter: :http_graphql_api,
            transporter_profile: nil, # Transporter profile for sftp connection, see secrets.yml
            query: nil,
            partner:, # Name of partner for creating log entry
            category:, # Category for log entry
            data_type: 'json')
  transport = instantiate_transporter(transporter, transporter_profile)
  res = transport.send_data(query)
  edi_logs = []
  EdiCommunicationLog.transaction do
    if res[:success] && (data = res[:http_result]&.body.to_s).present?
      json_hash = JSON.parse(data).with_indifferent_access
      data_blob = json_hash.to_json
    end
    if json_hash && json_hash[:data] && json_hash[:data][:purchaseOrders]&.any? # just skip generaing edi communication logs if no records to process
      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
  edi_logs
end