Class: Feed::ReviewsIo::ProductCatalogGenerator

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/feed/reviews_io/product_catalog_generator.rb

Overview

Generates Reviews.io product catalog feed
https://dash.reviews.io/products/setup
Product ID / SKU, Product Name, Product Image URL and Product Page URL

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.grouping_mapObject

Product grouping configuration loaded from config/reviews_io/product_groupings.yml



8
9
10
# File 'app/services/feed/reviews_io/product_catalog_generator.rb', line 8

def self.grouping_map
  Rails.application.config.reviews_io_product_groupings
end

Instance Method Details

#process(limit: nil) ⇒ Object

Main method.

  • limit: optional limit to number of records to process


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
# File 'app/services/feed/reviews_io/product_catalog_generator.rb', line 13

def process(limit: nil)
  res = { products: 0 }
  data = []

  # First, get unique product IDs using DISTINCT ON (fast, ID-only query)
  # Then batch process them for memory efficiency
  product_ids = load_product_ids(limit: limit)

  # Process in batches to avoid loading all records into memory at once
  product_ids.each_slice(1000) do |batch_ids|
    # Load products for this batch with associations
    products = ViewProductCatalog.where(id: batch_ids)
                                 .includes(item: [:primary_image, :primary_product_line])
                                 .to_a

    items = products.filter_map(&:item)
    pls = items.filter_map(&:primary_product_line).uniq(&:id)
    if pls.any?
      paths = ProductLine.canonical_paths_for(pls)
      pls.each { |pl| pl.instance_variable_set(:@canonical_path, paths[pl.id]) }
    end

    products.each do |product|
      product_data = build_product_hash(product)
      data << product_data
      res[:products] += 1
    end
  end

  res[:data] = data
  res[:xml] = data.to_xml(root: 'products')
  res
end