Class: SpreeSquare::Revalidator

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

Overview

Tells the Next.js storefront which Cache Components tags to drop after a sync write, so a price/menu/inventory change from Square shows up immediately instead of waiting out the storefront's cacheLife("tenMinutes") window.

Deliberately not routed through Spree's own outbound webhook system (Spree::WebhookEndpoint): that system's product.updated event is suppressed on touch-only cascades, which is exactly how a price or stock change reaches the product (variant/price/stock_item touch the product, they don't update it directly) — so it would silently miss most of what this extension syncs. This service is called directly from the sync code, which already knows the exact product with no ambiguity.

Failure here is logged, never raised — a stale storefront cache for up to 10 minutes is a much smaller problem than a failed revalidation call breaking catalog or inventory sync.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.callObject



21
# File 'app/services/spree_square/revalidator.rb', line 21

def self.call(...) = new.call(...)

Instance Method Details

#call(tags) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/spree_square/revalidator.rb', line 23

def call(tags)
  return if url.blank? || secret.blank? || tags.blank?

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.open_timeout = 2
  http.read_timeout = 2

  request = Net::HTTP::Post.new(uri.request_uri)
  request['Content-Type'] = 'application/json'
  request['x-revalidate-secret'] = secret
  request.body = { tags: Array(tags) }.to_json

  response = http.request(request)
  unless response.is_a?(Net::HTTPSuccess)
    Rails.logger.warn("[SpreeSquare] revalidate call failed (#{response.code}): #{response.body}")
  end
rescue StandardError => e
  Rails.logger.warn("[SpreeSquare] revalidate call errored: #{e.message}")
end