Class: Showroom::Client
- Inherits:
-
Object
- Object
- Showroom::Client
- 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).
Constant Summary
Constants included from Showroom::Core::Configurable
Showroom::Core::Configurable::KEYS
Instance Attribute Summary
Attributes included from Showroom::Core::Connection
Instance Method Summary collapse
-
#collection(handle) ⇒ Collection
Fetches a single collection by handle.
-
#collections(**params) ⇒ Array<Collection>
Fetches collections from the store.
-
#initialize(**opts) ⇒ Client
constructor
Initializes a new Client with the given options.
-
#product(handle) ⇒ Product
Fetches a single product by handle.
-
#products(**params) ⇒ Array<Product>
Fetches products from the store.
-
#search(query_str, types: %i[product collection],, limit: per_page, **params) ⇒ Search::Result
Calls the Shopify search suggest endpoint and returns a Search::Result.
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.
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.
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.
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.
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.
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.
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 |