Class: Retailer::CostcoCredentials

Inherits:
Object
  • Object
show all
Defined in:
app/services/retailer/costco_credentials.rb

Overview

Discovers and caches the Costco.ca catalog-API credentials.

Costco's product pages embed a machine-readable API config blob
(priceDetailsServiceConfig) that publishes the price endpoint, the
required Client-Identifier header credential, the per-region clientId
parameter, and the warehouse number. These values are static across
visits but Costco can rotate them on a site deploy — so rather than
hardcoding, we scrape them once (via Oxylabs), cache them, and re-scrape
when an API call rejects them.

See Also:

Defined Under Namespace

Classes: Config, ConfigError

Constant Summary collapse

REGION =

Region key inside the config blob. cabc = Costco Canada consumer
(the only live Costco catalog — COSTCO_USA is discontinued).

'cabc'
CACHE_KEY =
'retailer:costco:credentials:cabc'
CACHE_TTL =
30.days
CONFIG_SOURCE_URL =

A stable Costco.ca product page used to (re)discover the API config.
Any product page carries the same global blob; this one is a
long-lived WarmlyYours listing.

'https://www.costco.ca/p/-/warmlyyours-cable-floor-heating-kit-for-under-tile-and-hardwood-floor/100328844'

Class Method Summary collapse

Class Method Details

.fetchConfig

Cached credentials, scraping on a cold cache.

Returns:



48
49
50
# File 'app/services/retailer/costco_credentials.rb', line 48

def fetch
  Rails.cache.read(CACHE_KEY) || refresh!
end

.refresh!(source_url: CONFIG_SOURCE_URL) ⇒ Config

Force a fresh scrape and overwrite the cache. Called on a cold cache
and whenever Retailer::CostcoApi sees an auth rejection.

Parameters:

  • source_url (String) (defaults to: CONFIG_SOURCE_URL)

    Costco.ca page to scrape the config from

Returns:

Raises:

  • (ConfigError)

    if the page yields no usable config



58
59
60
61
62
63
# File 'app/services/retailer/costco_credentials.rb', line 58

def refresh!(source_url: CONFIG_SOURCE_URL)
  config = scrape_config(source_url)
  Rails.cache.write(CACHE_KEY, config, expires_in: CACHE_TTL)
  Rails.logger.info "[CostcoCredentials] Refreshed: endpoint=#{config.price_endpoint} warehouse=#{config.warehouse}"
  config
end