Module: Retailer::UrlPatterns

Defined in:
app/services/retailer/url_patterns.rb

Overview

Shared URL patterns for identifying direct product pages vs search/category pages.
Used by PriceChecker (sync) and WebhookResultProcessor (async) to decide whether
a discovered or redirect URL should be stored on a catalog item.

Constant Summary collapse

PRODUCT_PATTERNS =

Product patterns.

[
  %r{/p/[^/]+/\d+},           # Home Depot USA: /p/{slug}/{id}
  %r{/p/-/[^/]+/\d+},         # Costco (new): /p/-/{slug}/{id}
  %r{/product/[^/]+},         # Generic / HD Canada: /product/{slug}
  /\.product\.\d+\.html/, # Costco (legacy): /{slug}.product.{tppn}.html
  %r{/dp/},                   # Amazon: /dp/{asin}
  %r{/pdp/[^?]+\.html},       # Wayfair / Canadian Tire: /pdp/{slug}.html
  %r{/pd/[^/]+/\d+},          # Lowe's USA: /pd/{slug}/{id}
  # Walmart: /ip/{slug}/{id}. The id is the numeric usItemId for listings whose
  # id was captured by hand, but Walmart's Marketplace API hands us the
  # ALPHANUMERIC wpid ("4KRAWZGZCAO2"), which walmart.com resolves in the same
  # position. Every wpid on the account happens to start with a digit today, so
  # a `\d+` here matched by luck; a letter-leading one would have silently
  # failed the direct-PDP gate and dropped the item from the probe run.
  %r{/ip/[^/]+/[A-Za-z0-9]+},
  %r{/en-ca/product/},        # Best Buy Canada: /en-ca/product/{slug}
  %r{/s\d+\?uid=}             # Ferguson Home (Build.com): /{slug}/s{id}?uid={uid}
].freeze
SEARCH_PATTERNS =

Search patterns.

[
  /search\?/,
  /search-results/,
  %r{/s/[^/]*$} # HD USA search: /s/{query}
].freeze

Class Method Summary collapse

Class Method Details

.direct_product_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'app/services/retailer/url_patterns.rb', line 34

def self.direct_product_url?(url)
  return false if url.blank?
  return false if SEARCH_PATTERNS.any? { |p| url.match?(p) }

  PRODUCT_PATTERNS.any? { |p| url.match?(p) }
end