Class: Amazon::MatchBrowseNode

Inherits:
BaseService show all
Includes:
Memery
Defined in:
app/services/amazon/match_browse_node.rb

Overview

Processes Amazon catalog items and associates them with the appropriate Amazon browse nodes.

This service class is responsible for processing a set of Amazon catalog items, extracting the
relevant browse node IDs, and associating the catalog items with the corresponding browse nodes.
It handles cases where the catalog item already has associated browse nodes, as well as cases
where the browse node is missing or cannot be found.

SP-API has no endpoint to dump the full browse tree, so amazon_browse_nodes (node_id + node_path)
is a fixed seed. When Amazon reports a browse node (summaries[].browseClassification) whose id is
NOT in that seed, we log a warning with the node id + Amazon's displayName so it can be added —
otherwise new nodes would be silently skipped. (Per-ASIN ancestry is also retrievable via the
legacy Catalog Items v0 listCatalogCategories endpoint if we ever auto-populate the seed.)

param catalog_items [Array] the Amazon catalog items to process, active record relation
param logger [Logger] the logger to use for logging
param overwrite [Boolean] whether to overwrite existing browse node associations

Returns:

  • (Hash)

    a hash of processing results, keyed by catalog item ID

Instance Attribute Summary

Attributes inherited from BaseService

#options

Class Method Summary collapse

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

Class Method Details

.process_amazon_catalog_items(catalog_items: nil, logger: Rails.logger, overwrite: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'app/services/amazon/match_browse_node.rb', line 23

def self.process_amazon_catalog_items(catalog_items: nil, logger: Rails.logger, overwrite: false)
  results = {}
  matcher = new(logger: logger)
  catalog_items ||= CatalogItem.amazons
  catalog_items.includes(:amazon_browse_nodes).find_each do |catalog_item|
    result = matcher.process(catalog_item, overwrite:)
    results.merge!(result)
  end
  results
end

Instance Method Details

#process(catalog_item, overwrite: false) ⇒ Object

Always returns a { catalog_item_id => result } Hash so the batch entry point
(process_amazon_catalog_items) can merge! it. process_node yields one entry
per classification keyed by the same catalog_item id; for the (rare) multi-node
item the last node wins in the returned summary — every node is still logged.



38
39
40
41
42
43
44
45
# File 'app/services/amazon/match_browse_node.rb', line 38

def process(catalog_item, overwrite: false)
  return skip_existing_nodes(catalog_item) if catalog_item.amazon_browse_nodes.present? && !overwrite

  classifications = extract_classifications(catalog_item.retailer_information)
  return handle_missing_data(catalog_item) if classifications.blank?

  process_node(catalog_item, classifications).reduce({}, :merge)
end