showroom 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c08f4e39bf588bda99e07cb1310c9e56628914a6cdb79d4a0b6e89b92a048e3
4
- data.tar.gz: 0de4e4be0d0a10eaa891c512daa622b80724b022d320709e021453b213e98647
3
+ metadata.gz: 7805a1b540c985ccaa80393080ce970f09764990bd9a7a8471c3e3cd092827f2
4
+ data.tar.gz: 261bcdbd50855a4761ff0b18f5b517c7acd1c533a835d1121914b7b23fc2db73
5
5
  SHA512:
6
- metadata.gz: 118d905ece1c93e675e63ce0efcc6f553a22fbca8b09a255057ea3a5b55a8f36fae9b4d93855962d621f879aa5841729ee69b88bb197420aec85b847e08027b8
7
- data.tar.gz: 98659d1aa63513b1c2e22eb8f54dca17ee70d27b1ec9d02feb6b6f4aaa3800e9e386c002e83ca460782807fb5ea92e35a0439873df2715c28fe2b910d59f866f
6
+ metadata.gz: 45cd85604c8244fb98f7d4c2d9950b486f2e05fb95ce72e334ef1b927c0578496f69745b5651ca32426cc2fe168548072e38de2c02cf27c3b8fa134561ab145e
7
+ data.tar.gz: 1c2be840665a8c1dac605529d09218b7eb4f8663a07b0854548ae5dcf113296dc5e3921cf4154075e526431dc14b7a71ca708f0c4aec6486ece25c9a0c4ebbba
data/CHANGELOG.md CHANGED
@@ -7,6 +7,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - `Product#similar` — searches for products similar to the current one via the search suggest endpoint; strips variant SKU fragments from the handle before querying, and falls back to a title search when the stripped-handle query returns no results
13
+ - `Search::Result#products` now accepts an `order:` keyword (`:id`, `:title`, `:handle`, or `:price`) to sort suggestions client-side; defaults to API response order when omitted
14
+
15
+ ### Fixed
16
+
17
+ - Rakefile: corrected gem-build task so `rake build` works correctly
18
+
19
+ ## [0.2.0] - 2026-04-20
20
+
21
+ ### Added
22
+
23
+ - `Product#url` — returns the canonical storefront URL for a product (`/products/:handle`)
24
+ - `Product#prices` — deduplicated list of raw price strings across all variants
25
+ - `Product#main_image` — returns the image with `position: 1`, distinct from `featured_image` (which returns `images.first`)
26
+ - `Collection#url` — returns the canonical storefront URL for a collection (`/collections/:handle`)
27
+ - `Client#base_url` — exposes the resolved HTTPS base URL for the configured store
28
+ - `Resource#client` accessor — models now carry a reference to the client that fetched them
29
+ - Ruby 4.0 added to CI matrix
30
+ - Dependabot configuration for automated dependency updates
31
+
32
+ ### Changed
33
+
34
+ - All models returned by `Client` methods (`products`, `product`, `collections`, `collection`) now have their `client` attribute set automatically, enabling instance-scoped operations without relying on the global `Showroom.client`
35
+ - `Collection#products` uses the instance client when available, falling back to the global client
36
+
10
37
  ## [0.1.0] - 2026-04-11
11
38
 
12
39
  ### Added
data/README.md CHANGED
@@ -55,6 +55,13 @@ globo = Showroom::Client.new(store: 'globo.myshopify.com')
55
55
 
56
56
  acme.products(limit: 5).map(&:title)
57
57
  globo.product('road-bike').price
58
+
59
+ # Models remember which client fetched them
60
+ product = acme.product('lorem-road-bike')
61
+ product.url # => "https://acme.myshopify.com/products/lorem-road-bike"
62
+
63
+ # base_url is available on any client
64
+ acme.base_url # => "https://acme.myshopify.com"
58
65
  ```
59
66
 
60
67
  ## Products
@@ -71,11 +78,19 @@ product.vendor # => "Lorem Cycles"
71
78
  product.available? # => true (any variant in stock)
72
79
  product.price # => "749.00" (lowest variant price)
73
80
  product.price_range # => "749.00–899.00"
74
- product.featured_image # => #<Showroom::ProductImage ...>
81
+ product.prices # => ["749.00", "899.00"] (unique prices from all variants)
82
+ product.url # => "https://acme.myshopify.com/products/lorem-road-bike"
83
+ product.featured_image # => #<Showroom::ProductImage ...> (first image)
84
+ product.main_image # => #<Showroom::ProductImage ...> (image with position 1)
85
+
86
+ # Similar products — searches by handle (SKU fragments stripped), falls back to title
87
+ product.similar # => Array<Search::ProductSuggestion>, sorted by price
88
+ product.similar(limit: 3) # forward any Client#search keyword
89
+ product.similar.first.load # => Showroom::Product (full record)
75
90
 
76
91
  # Variants
77
92
  product.variants.each do |v|
78
- puts "#{v.title} #{v.price} on_sale=#{v.on_sale?} available=#{v.available?}"
93
+ puts "#{v.title} #{v.price} #{v.compare_at_price} on_sale=#{v.on_sale?} available=#{v.available?}"
79
94
  end
80
95
 
81
96
  # All pages — returns an Enumerator
@@ -94,6 +109,30 @@ Showroom.products(limit: 5) # => Array<Product>
94
109
  Showroom.product('lorem-road-bike') # => Product
95
110
  ```
96
111
 
112
+ ## Collections
113
+
114
+ ```ruby
115
+ # List
116
+ Showroom::Collection.where(limit: 10)
117
+
118
+ # Single
119
+ collection = Showroom::Collection.find('lorem-helmets')
120
+ collection.title # => "Lorem Helmets"
121
+ collection.handle # => "lorem-helmets"
122
+ collection.products_count # => 12
123
+ collection.url # => "https://acme.myshopify.com/collections/lorem-helmets"
124
+
125
+ # Products in a collection
126
+ collection.products(limit: 5).each { |p| puts p.title }
127
+ ```
128
+
129
+ ### Module-level shortcuts
130
+
131
+ ```ruby
132
+ Showroom.collections(limit: 5) # => Array<Collection>
133
+ Showroom.collection('lorem-helmets') # => Collection
134
+ ```
135
+
97
136
  ## Search
98
137
 
99
138
  Showroom wraps Shopify's `/search/suggest.json` endpoint via `Showroom.search`.
@@ -117,7 +156,7 @@ Available `types:` values: `:product`, `:collection`, `:page`, `:article`, `:que
117
156
  ```ruby
118
157
  result = Showroom.search('lorem', types: [:product, :collection, :page, :article, :query])
119
158
 
120
- result.products # => Array<Search::ProductSuggestion>
159
+ result.products # => Array<Search::ProductSuggestion> (API order)
121
160
  result.collections # => Array<Search::CollectionSuggestion>
122
161
  result.pages # => Array<Search::PageSuggestion>
123
162
  result.articles # => Array<Search::ArticleSuggestion>
@@ -125,6 +164,13 @@ result.queries # => Array<Search::QuerySuggestion>
125
164
 
126
165
  result.products.first.title # => "Lorem Road Bike"
127
166
  result.queries.first.text # => "lorem road bike"
167
+
168
+ # Sort product suggestions client-side
169
+ result.products(order: :price) # numeric ascending
170
+ result.products(order: :title) # alphabetical
171
+ result.products(order: :handle) # alphabetical
172
+ result.products(order: :id) # numeric ascending
173
+ # Accepted values: :id, :title, :handle, :price — raises ArgumentError otherwise
128
174
  ```
129
175
 
130
176
  ### Loading full models
@@ -193,7 +239,7 @@ end
193
239
  - **Rate limits** — Shopify's public endpoints allow roughly 2 req/s per IP. Showroom raises `TooManyRequests` (429) but does not retry automatically. Add your own back-off logic.
194
240
  - **`/products.json` may be disabled** on some stores. You'll receive a `NotFound` or `ServerError`.
195
241
  - **User-Agent** — some stores block the default Faraday UA. Showroom sets its own identifying header by default; override via `c.user_agent` if needed.
196
- - **Search result ordering is not stable** — `/search/suggest.json` does not guarantee a consistent order across requests. Results with equal relevance scores may alternate non-deterministically. Do not rely on `result.products.first` being the same between calls.
242
+ - **Search result ordering is not stable** — `/search/suggest.json` does not guarantee a consistent order across requests. Use `result.products(order: :price)` (or another attribute) for deterministic client-side sorting.
197
243
 
198
244
  ## License
199
245
 
@@ -44,7 +44,7 @@ module Showroom
44
44
  # @param params [Hash] Shopify query parameters
45
45
  # @return [Array<Product>]
46
46
  def products(**params)
47
- get('/products.json', params).fetch('products', []).map { |h| Product.new(h) }
47
+ get('/products.json', params).fetch('products', []).map { |h| Product.new(h).tap { |r| r.client = self } }
48
48
  end
49
49
 
50
50
  # Fetches a single product by handle.
@@ -55,7 +55,7 @@ module Showroom
55
55
  def product(handle)
56
56
  get("/products/#{handle}.json")
57
57
  .fetch('product') { raise Showroom::NotFound, handle }
58
- .then { |h| Product.new(h) }
58
+ .then { |h| Product.new(h).tap { |r| r.client = self } }
59
59
  end
60
60
 
61
61
  # Fetches collections from the store.
@@ -63,7 +63,11 @@ module Showroom
63
63
  # @param params [Hash] Shopify query parameters
64
64
  # @return [Array<Collection>]
65
65
  def collections(**params)
66
- get('/collections.json', params).fetch('collections', []).map { |h| Collection.new(h) }
66
+ get('/collections.json', params).fetch('collections', []).map do |h|
67
+ Collection.new(h).tap do |r|
68
+ r.client = self
69
+ end
70
+ end
67
71
  end
68
72
 
69
73
  # Fetches a single collection by handle.
@@ -74,7 +78,7 @@ module Showroom
74
78
  def collection(handle)
75
79
  get("/collections/#{handle}.json")
76
80
  .fetch('collection') { raise Showroom::NotFound, handle }
77
- .then { |h| Collection.new(h) }
81
+ .then { |h| Collection.new(h).tap { |r| r.client = self } }
78
82
  end
79
83
 
80
84
  # Calls the Shopify search suggest endpoint and returns a {Search::Result}.
@@ -13,6 +13,13 @@ module Showroom
13
13
  # @return [Faraday::Response, nil] the last HTTP response object
14
14
  attr_reader :last_response
15
15
 
16
+ # Returns the canonical HTTPS base URL for the configured store.
17
+ #
18
+ # @return [String]
19
+ def base_url
20
+ StoreUrl.resolve(store)
21
+ end
22
+
16
23
  # Memoized Faraday connection built from the current configuration.
17
24
  #
18
25
  # @return [Faraday::Connection]
@@ -4,6 +4,6 @@ module Showroom
4
4
  # Internal gem infrastructure (configuration, connection, errors, versioning).
5
5
  module Core
6
6
  # Current gem version.
7
- VERSION = '0.1.0'
7
+ VERSION = '0.3.0'
8
8
  end
9
9
  end
@@ -59,9 +59,18 @@ module Showroom
59
59
  # @param params [Hash] Shopify query parameters (e.g. +limit:+, +page:+)
60
60
  # @return [Array<Product>]
61
61
  def products(**params)
62
- Showroom.client.get("/collections/#{handle}/products.json", params)
63
- .fetch('products', [])
64
- .map { |h| Product.new(h) }
62
+ conn = client || Showroom.client
63
+ conn.get("/collections/#{handle}/products.json", params)
64
+ .fetch('products', [])
65
+ .map { |h| Product.new(h).tap { |r| r.client = conn } }
66
+ end
67
+
68
+ # Returns the canonical storefront URL for this collection.
69
+ #
70
+ # @return [String]
71
+ def url
72
+ conn = client || Showroom.client
73
+ "#{conn.base_url}/collections/#{handle}"
65
74
  end
66
75
  end
67
76
  end
@@ -138,5 +138,79 @@ module Showroom
138
138
  def featured_image
139
139
  images.first
140
140
  end
141
+
142
+ # Returns the canonical storefront URL for this product.
143
+ #
144
+ # @return [String]
145
+ def url
146
+ conn = client || Showroom.client
147
+ "#{conn.base_url}/products/#{handle}"
148
+ end
149
+
150
+ # Returns unique prices across all variants as an Array of Strings.
151
+ #
152
+ # Unlike {#price} (lowest single price) or {#price_range} (formatted string),
153
+ # this returns the raw deduplicated list useful for custom rendering.
154
+ #
155
+ # @return [Array<String>]
156
+ def prices
157
+ variants.map do |variant|
158
+ variant['price']
159
+ end.uniq
160
+ end
161
+
162
+ # Returns the image whose +position+ field equals 1, or nil if none match.
163
+ #
164
+ # Unlike {#featured_image} (which returns +images.first+ regardless of
165
+ # position), this explicitly matches on the +position+ attribute.
166
+ #
167
+ # @return [ProductImage, nil]
168
+ def main_image
169
+ images.find do |img|
170
+ img['position'] == 1
171
+ end
172
+ end
173
+
174
+ # Searches for products similar to this one using the product handle as
175
+ # the query, with any variant SKU fragments stripped out first.
176
+ #
177
+ # SKU fragments are matched case-insensitively as hyphen-delimited segments
178
+ # within the handle. For example, a handle of +"o2feel-equo-42-rr2px5"+ on
179
+ # a product whose variant carries SKU +"RR2PX5"+ yields the query
180
+ # +"o2feel-equo-42"+.
181
+ #
182
+ # @param kwargs [Hash] additional options forwarded to +Client#search+
183
+ # @return [Array<Search::ProductSuggestion>]
184
+ def similar(**kwargs)
185
+ search_args = kwargs.merge(types: %i[product])
186
+
187
+ handle_similars = similar_by_stripped_handle(**search_args)
188
+ return handle_similars unless handle_similars.empty?
189
+
190
+ similar_by_title(**search_args)
191
+ end
192
+
193
+ private
194
+
195
+ # @api private
196
+ def similar_by_stripped_handle(**)
197
+ (client || Showroom.client).search(stripped_handle_search_query, **).products(order: 'price')
198
+ end
199
+
200
+ # @api private
201
+ def similar_by_title(**)
202
+ (client || Showroom.client).search(title, **).products(order: 'price')
203
+ end
204
+
205
+ # Strips variant SKU fragments from the handle to build the search query.
206
+ #
207
+ # @return [String]
208
+ def stripped_handle_search_query
209
+ skus = variants.filter_map { |v| v.sku.to_s.strip.downcase }.reject(&:empty?).uniq
210
+ skus.inject(handle.to_s) do |h, sku|
211
+ h.sub(/(^|-)#{Regexp.escape(sku)}((?=-)|$)/i, '')
212
+ .gsub(/^-|-$/, '')
213
+ end
214
+ end
141
215
  end
142
216
  end
@@ -8,7 +8,7 @@ module Showroom
8
8
  # variant.available? # => true
9
9
  # variant.on_sale? # => false
10
10
  class ProductVariant < Resource
11
- main_attrs :id, :title, :price, :sku
11
+ main_attrs :id, :title, :price, :compare_at_price, :sku
12
12
 
13
13
  # Returns true when the variant is available for purchase.
14
14
  #
@@ -15,6 +15,9 @@ module Showroom
15
15
  # @return [Hash] the normalized attribute hash
16
16
  attr_reader :attrs
17
17
 
18
+ # @return [Showroom::Client, nil] the client that fetched this resource
19
+ attr_accessor :client
20
+
18
21
  # Initializes a Resource from a Hash, normalizing all keys to strings.
19
22
  #
20
23
  # @param hash [Hash] attribute data
@@ -16,11 +16,22 @@ module Showroom
16
16
  @data = results_hash
17
17
  end
18
18
 
19
- # Returns product suggestions from the search result.
19
+ PRODUCT_ORDER_ATTRS = %w[id title handle price].freeze
20
+ private_constant :PRODUCT_ORDER_ATTRS
21
+
22
+ # Returns product suggestions from the search result, optionally sorted.
20
23
  #
24
+ # @param order [Symbol, nil] attribute to sort by: +:id+, +:title+, +:handle+, or +:price+.
25
+ # When +nil+ (default) the API response order is preserved.
26
+ # +price+ is sorted numerically; all others alphabetically/numerically by natural value.
21
27
  # @return [Array<ProductSuggestion>]
22
- def products
23
- @data.fetch('products', []).map { |h| ProductSuggestion.new(h) }
28
+ # @raise [ArgumentError] when an unsupported order attribute is given
29
+ def products(order: nil)
30
+ validate_order!(order)
31
+ suggestions = @data.fetch('products', []).map { |h| ProductSuggestion.new(h) }
32
+ return suggestions unless order
33
+
34
+ sort_product_suggestions(suggestions, order)
24
35
  end
25
36
 
26
37
  # Returns collection suggestions from the search result.
@@ -50,6 +61,23 @@ module Showroom
50
61
  def queries
51
62
  @data.fetch('queries', []).map { |h| QuerySuggestion.new(h) }
52
63
  end
64
+
65
+ private
66
+
67
+ def validate_order!(order)
68
+ return unless order
69
+ return if PRODUCT_ORDER_ATTRS.include?(order.to_s)
70
+
71
+ raise ArgumentError, "order must be one of #{PRODUCT_ORDER_ATTRS.join(', ')}"
72
+ end
73
+
74
+ def sort_product_suggestions(suggestions, order)
75
+ if order.to_s == 'price'
76
+ suggestions.sort_by { |p| p.price.to_f }
77
+ else
78
+ suggestions.sort_by(&order.to_sym)
79
+ end
80
+ end
53
81
  end
54
82
  end
55
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: showroom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 01max