Class: Feed::Google::LocalInventoryGenerator

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/feed/google/local_inventory_generator.rb

Overview

Pre-generates the google merchant product feed and store it in the database for fast retrieval

Instance Method Summary collapse

Instance Method Details

#append_xml_catalog_item(xml, cip) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/feed/google/local_inventory_generator.rb', line 46

def append_xml_catalog_item(xml, cip)
  xml.entry do
    xml.send :'g:id', cip.id
    xml.send :'g:store_code', cip.store_code
    xml.send :'g:quantity', cip.store_item_qty_available
    xml.send :'g:price', cip.price_with_currency
    if cip.sale_price_in_effect?
      xml.send :'g:sale_price', cip.sale_price_with_currency
      xml.send :'g:sale_price_effective_date', cip.sale_price_date_range if cip.sale_price_date_range.present?
    end
    xml.send :'g:availability', cip.availability
    xml.send :'g:pickup_method', cip.pickup_method
    xml.send :'g:pickup_sla', cip.pickup_sla
    xml.send :'g:link_template', "#{cip.url}?g_store_code={store_code}"
  end
end

#load_products(catalog, limit: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/feed/google/local_inventory_generator.rb', line 27

def load_products(catalog, limit: nil)
  # All filtering done in SQL via valid_for_google_local_inventory scope:
  # - shipping dimensions > 0
  # - item_condition = 'new'
  # - excludes dealer-channel-only products (cosmopolitan towel warmers)
  products = catalog.view_product_catalogs
                    .valid_for_google_local_inventory
                    .includes({ store_item: :store },
                              :item,
                              { catalog_item: :catalog },
                              :sales_portal_product_line,
                              :primary_product_line)
  products = products.joins(:catalog_item).merge(CatalogItem.for_google_feed)
  products = products.limit(limit) if limit.present?
  products.map { |ci| Feed::Google::GoogleProductPresenter.new(ci) }
end

#process(catalogs: nil, limit: nil) ⇒ Object

Main method.

  • catalogs: specify catalogs otherwise will be main catalogs (US and CA)
  • limit: optional limit to number of records to process


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/feed/google/local_inventory_generator.rb', line 6

def process(catalogs: nil, limit: nil)
  res = { products: 0 }
  builder = Nokogiri::XML::Builder.new do |xml|
    catalogs ||= Catalog.main_catalogs
    xml.feed(xmlns: 'http://www.w3.org/2005/Atom', 'xmlns:g': 'http://base.google.com/ns/1.0') do
      catalogs.each do |catalog|
        I18n.with_locale catalog.locale_for_catalog do
          products = load_products(catalog, limit: limit)
          products.each do |p|
            append_xml_catalog_item(xml, p)
            res[:products] += 1
          end
        end
      end
    end
  end
  res[:xml] = builder.to_xml # save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION

  res
end