Class: Edi::MiraklSeller::ProductDataConfigurator

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

Defined Under Namespace

Classes: ProductDataResult

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

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#append_items(xml, items) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/edi/mirakl_seller/product_data_configurator.rb', line 8

def append_items(xml, items)
  items.find_each do |item|
    data_as_hash = orchestrator.product_data_presenter(item).to_h
    if data_as_hash.nil?
      # Presenter declined to map this item (e.g. no Best Buy product
      # category matches its family — accessories, unmapped lines). This is
      # expected, not an error: the item is excluded from the feed and the
      # run proceeds. Recorded as a skip (logged, not emailed) so a genuine
      # data error doesn't get buried under the same notification.
      @skipped_list << "#{item.sku} - no category mapping; excluded from feed."
      next
    end
    xml.send(:product) do
      # Generic product data regardless of the category. All products need this set of attributes.
      product_data(xml, item, data_as_hash)
    end
  end
  xml
end

#build_xml(items: Item.none, _catalog_items: nil, _states: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/edi/mirakl_seller/product_data_configurator.rb', line 39

def build_xml(items: Item.none, _catalog_items: nil, _states: nil)
  @error_list = []
  @skipped_list = []
  logger.info "#{items.size} items in product data payload"
  b = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.import('xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsd': 'http://www.w3.org/2001/XMLSchema') do
      xml.send(:products) do
        append_items(xml, items)
      end
    end
  end
  ProductDataResult.new(xml: b.to_xml, errors: @error_list, skipped: @skipped_list)
end

#create_attribute(xml, key, value, item, required: false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/edi/mirakl_seller/product_data_configurator.rb', line 58

def create_attribute(xml, key, value, item, required: false)
  # Convert boolean values Yes/No to marketplace value. Yes LOV_000001 - No LOV_000002.
  value_filtered = value_pre_filter(value)
  ####### PROCEED
  if required && value_filtered.blank?
    # Better handle for missing mandatory values?
    @error_list << "#{item.sku} - Missing value for #{key}."
  elsif value_filtered.present? # optional do not need to be included
    xml.send(:attribute) do
      xml.send(:code, key)
      xml.send(:value, value_filtered)
    end
  end
end

#product_data(xml, item, data_as_hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'app/services/edi/mirakl_seller/product_data_configurator.rb', line 28

def product_data(xml, item, data_as_hash)
  required_data = data_as_hash[:required]
  optional_data = data_as_hash[:optional]
  required_data&.each do |key, value|
    create_attribute xml, key, value, item, required: true
  end
  optional_data&.each do |key, value|
    create_attribute xml, key, value, item, required: false
  end
end

#value_pre_filter(value) ⇒ Object

Converts value yes/no to the mapping of the marketplace, for most it is 1 or 0 but for some it is LOV_000001 or LOV_000002 (leroymerlin)



54
55
56
# File 'app/services/edi/mirakl_seller/product_data_configurator.rb', line 54

def value_pre_filter(value)
  orchestrator.try(:value_pre_filter).try(:[], value).presence || value
end