Class: Showroom::Client

Inherits:
Object
  • Object
show all
Includes:
Showroom::Core::Configurable, Showroom::Core::Connection
Defined in:
lib/showroom/client.rb

Overview

A configured HTTP client for a single Showroom store.

Combines Showroom::Core::Configurable (configuration DSL) with Showroom::Core::Connection (Faraday-based HTTP).

Examples:

Single-store usage

client = Showroom::Client.new(store: 'example.myshopify.com')
client.get('/products.json', limit: 10)

Multi-store usage

eu_client = Showroom::Client.new(store: 'eu.shop.com')
us_client = Showroom::Client.new(store: 'us.shop.com')

Constant Summary

Constants included from Showroom::Core::Configurable

Showroom::Core::Configurable::KEYS

Instance Attribute Summary

Attributes included from Showroom::Core::Connection

#last_response

Instance Method Summary collapse

Methods included from Showroom::Core::Connection

#agent, #base_url, #get, #paginate

Methods included from Showroom::Core::Configurable

#configure, #options, #per_page=, #reset!, #same_options?, #store, #store=

Constructor Details

#initialize(**opts) ⇒ Client

Initializes a new Client with the given options.

Resets all keys to defaults first, then applies any provided options.

Parameters:

Options Hash (**opts):

  • :store (String)

    the Shopify store domain or URL

  • :user_agent (String)

    custom User-Agent string

  • :per_page (Integer)

    results per page (clamped to MAX_PER_PAGE)

  • :pagination_depth (Integer)

    maximum pages to fetch

  • :open_timeout (Integer)

    connection timeout in seconds

  • :timeout (Integer)

    read timeout in seconds

  • :middleware (#call, nil)

    Faraday middleware proc

  • :connection_options (Hash)

    extra Faraday connection options



37
38
39
40
# File 'lib/showroom/client.rb', line 37

def initialize(**opts)
  reset!
  opts.each { |key, value| send(:"#{key}=", value) }
end

Instance Method Details

#collection(handle) ⇒ Collection

Fetches a single collection by handle.

Parameters:

  • handle (String)

    the collection handle

Returns:

Raises:



78
79
80
81
82
# File 'lib/showroom/client.rb', line 78

def collection(handle)
  get("/collections/#{handle}.json")
    .fetch('collection') { raise Showroom::NotFound, handle }
    .then { |h| Collection.new(h).tap { |r| r.client = self } }
end

#collections(**params) ⇒ Array<Collection>

Fetches collections from the store.

Parameters:

  • params (Hash)

    Shopify query parameters

Returns:



65
66
67
68
69
70
71
# File 'lib/showroom/client.rb', line 65

def collections(**params)
  get('/collections.json', params).fetch('collections', []).map do |h|
    Collection.new(h).tap do |r|
      r.client = self
    end
  end
end

#product(handle) ⇒ Product

Fetches a single product by handle.

Parameters:

  • handle (String)

    the product handle

Returns:

Raises:



55
56
57
58
59
# File 'lib/showroom/client.rb', line 55

def product(handle)
  get("/products/#{handle}.json")
    .fetch('product') { raise Showroom::NotFound, handle }
    .then { |h| Product.new(h).tap { |r| r.client = self } }
end

#products(**params) ⇒ Array<Product>

Fetches products from the store.

Parameters:

  • params (Hash)

    Shopify query parameters

Returns:



46
47
48
# File 'lib/showroom/client.rb', line 46

def products(**params)
  get('/products.json', params).fetch('products', []).map { |h| Product.new(h).tap { |r| r.client = self } }
end

#search(query_str, types: %i[product collection],, limit: per_page, **params) ⇒ Search::Result

Calls the Shopify search suggest endpoint and returns a Search::Result.

Parameters:

  • query_str (String)

    the search query

  • types (Array<Symbol>) (defaults to: %i[product collection],)

    resource types to search (e.g. :product, :collection)

  • limit (Integer) (defaults to: per_page)

    maximum results per type (defaults to #per_page)

  • params (Hash)

    additional query parameters forwarded to the API

Returns:



91
92
93
94
95
96
97
# File 'lib/showroom/client.rb', line 91

def search(query_str, types: %i[product collection], limit: per_page, **params)
  query = { q: query_str, 'resources[limit]' => limit }
  query['resources[type]'] = types.join(',') unless types.empty?
  query.merge!(params)
  raw = get('/search/suggest.json', query)
  Search::Result.new(raw.dig('resources', 'results') || {})
end