Class: Edi::MiraklSeller::Sender
- Inherits:
-
BaseEdiService
- Object
- BaseService
- BaseEdiService
- Edi::MiraklSeller::Sender
- Defined in:
- app/services/edi/mirakl_seller/sender.rb
Overview
I connect to a transport, retrieve files and save them in the Edi Communication log
Constant Summary
Constants included from RequestIdentifiable
RequestIdentifiable::REQUEST_ID_HEADERS
Constants included from AddressAbbreviator
AddressAbbreviator::MAX_LENGTH
Instance Attribute Summary
Attributes inherited from BaseEdiService
Attributes inherited from BaseService
Instance Method Summary collapse
- #call(remote_path:, partner:, category:, transporter: :http_api, transporter_send_method: 'post', transporter_profile: nil, data_type: nil, edi_communication_logs: nil) ⇒ Object
- #instantiate_transporter(_transporter, transporter_profile = nil) ⇒ Object
- #valid_json?(string) ⇒ Boolean
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
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, #process, #tagged_logger
Constructor Details
This class inherits a constructor from Edi::BaseEdiService
Instance Method Details
#call(remote_path:, partner:, category:, transporter: :http_api, transporter_send_method: 'post', transporter_profile: nil, data_type: nil, edi_communication_logs: nil) ⇒ Object
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 |
# File 'app/services/edi/mirakl_seller/sender.rb', line 8 def call(remote_path:, partner:, category:, transporter: :http_api, transporter_send_method: 'post', transporter_profile: nil, data_type: nil, edi_communication_logs: nil) # Which category to pull, e.g order_confirm transport = instantiate_transporter(transporter, transporter_profile) = {} [: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} ECL ##{ecl.id} bytes=#{ecl.data.bytesize} " \ "sha256=#{Digest::SHA256.hexdigest(ecl.data)} to #{remote_path} " \ "using transporter method: #{transporter_send_method} using profile #{transporter_profile}" send_as_json = data_type.nil? ? valid_json?(ecl.data) : data_type.to_s == 'json' res = if send_as_json transport.send_data(ecl.data, remote_path) else transport.send_mirakl_data(ecl.data, remote_path, ecl.file_name, ) end ecl.transmit_datetime = Time.current # send_data now returns a Faraday::Response (#status); send_mirakl_data # still returns an http.rb response (#code). #body works on both. http_code = res[:http_result].try(:status) || res[:http_result].try(:code) ecl.notes = "HTTP CODE: #{http_code}, HTTP BODY: #{res[:http_result].try(:body)}" logger.info "Result: HTTP CODE: #{http_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 |
#instantiate_transporter(_transporter, transporter_profile = nil) ⇒ Object
61 62 63 64 |
# File 'app/services/edi/mirakl_seller/sender.rb', line 61 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 |
#valid_json?(string) ⇒ Boolean
54 55 56 57 58 59 |
# File 'app/services/edi/mirakl_seller/sender.rb', line 54 def valid_json?(string) JSON.parse(string) true rescue StandardError false end |