Class: SpreeSquare::CatalogImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_square/catalog_importer.rb

Overview

Full catalog import: pulls every ITEM + CATEGORY + MODIFIER_LIST from Square (with related objects — images, referenced categories — inlined via include_related_objects) and upserts them into Spree via CatalogObjectMapper. Categories and modifier lists are imported before items so an item can resolve its category_id / modifier_list_info to an already-mapped record.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.callObject



11
# File 'app/services/spree_square/catalog_importer.rb', line 11

def self.call = new.call

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/spree_square/catalog_importer.rb', line 13

def call
  client = SpreeSquare::Client.instance
  objects, related_by_id = fetch_all(client)

  by_type = objects.group_by(&:type)
  categories = by_type.fetch('CATEGORY', [])
  modifier_lists = by_type.fetch('MODIFIER_LIST', [])
  items = by_type.fetch('ITEM', [])

  mapper = CatalogObjectMapper.new(related_objects_by_id: related_by_id)
  categories.each { |category| mapper.map_category(category) }
  modifier_lists.each { |list| mapper.map_modifier_list(list) }
  items.each { |item| mapper.map_item(item) }

  Result.new(categories_count: categories.size, modifier_lists_count: modifier_lists.size, items_count: items.size)
end