Class: Oembed::ProductProvider

Inherits:
Object
  • Object
show all
Defined in:
app/services/oembed/product_provider.rb

Defined Under Namespace

Classes: ProductNotFoundError, ProductUnavailableError

Instance Method Summary collapse

Instance Method Details

#get(options = {}) ⇒ Hash

Get rendered product HTML

Parameters:

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :sku (String)

    Product SKU (required)

  • :locale (String)

    Locale for pricing ('en-US' or 'en-CA')

  • :embedded_asset_uuid (String)

    EmbeddedAsset UUID for tracking (optional)

Returns:

  • (Hash)

    oEmbed-like response with :html, :type, :provider_name, etc.

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/oembed/product_provider.rb', line 37

def get(options = {})
  sku = options[:sku].to_s.strip
  locale = options[:locale].to_s.presence || 'en-US'
  embedded_asset_uuid = options[:embedded_asset_uuid].presence

  raise ProductNotFoundError, 'No SKU provided' if sku.blank?

  # Try exact match first, then case-insensitive
  item = Item.find_by(sku: sku) || Item.where('UPPER(sku) = ?', sku.upcase).first
  raise ProductNotFoundError, "Product not found: #{sku}" if item.nil?

  # For CRM editor preview, show the product with locale-aware pricing
  # The frontend rendering will handle final availability checks
  build_response(item, sku, locale, embedded_asset_uuid: embedded_asset_uuid)
end

#render_for_locale(sku, locale = 'en-US', include_schema: true) ⇒ String?

Render product embed HTML for the frontend (blog display)
This is called during content refresh and normal rendering
Handles locale-aware pricing and availability checks

Parameters:

  • sku (String)

    Product SKU

  • locale (String, Symbol) (defaults to: 'en-US')

    Locale ('en-US' or 'en-CA')

  • include_schema (Boolean) (defaults to: true)

    Whether to include JSON-LD schema (default: true)

Returns:

  • (String, nil)

    HTML string or nil if product unavailable



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/oembed/product_provider.rb', line 61

def render_for_locale(sku, locale = 'en-US', include_schema: true)
  sku_stripped = sku.to_s.strip
  item = Item.find_by(sku: sku_stripped) || Item.where('UPPER(sku) = ?', sku_stripped.upcase).first
  return nil if item.nil?

  catalog_id = catalog_id_for_locale(locale)
  catalog_item = item.catalog_items.find_by(catalog_id: catalog_id)

  # Check availability - return nil if product shouldn't be displayed
  return nil unless product_available?(item, catalog_item)

  # Render the product card
  card_html = render_product_card(item, catalog_item, locale)
  return nil if card_html.nil?

  # Optionally include JSON-LD schema for SEO
  if include_schema
    schema_html = render_product_schema(item, catalog_item, locale)
    "#{card_html}#{schema_html}"
  else
    card_html
  end
end