showroom 0.2.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: 022d47e06d5e246f2124d3297c029aadaa39e7c9004136590e3516d27390f7d7
4
- data.tar.gz: ad735cba032078f6de522a012855aedbe1dcfdfb6f25104a62323f5428618331
3
+ metadata.gz: 7805a1b540c985ccaa80393080ce970f09764990bd9a7a8471c3e3cd092827f2
4
+ data.tar.gz: 261bcdbd50855a4761ff0b18f5b517c7acd1c533a835d1121914b7b23fc2db73
5
5
  SHA512:
6
- metadata.gz: 829309cb01db6bfbaa0a7d0134c038e266f14340259058e16139b46a4431f44993a1720f81f32a36a6b4306b96b64664eed0fc9c4ba274f3f7095d6937515634
7
- data.tar.gz: 96a9bcfcf7fe25a4950c49df8181ebaf77928e43e88f94630a914dbc3c082e4edd12e94f6db349af2723801a5b05256b0891510de14e7567bee59c4dd9d37813
6
+ metadata.gz: 45cd85604c8244fb98f7d4c2d9950b486f2e05fb95ce72e334ef1b927c0578496f69745b5651ca32426cc2fe168548072e38de2c02cf27c3b8fa134561ab145e
7
+ data.tar.gz: 1c2be840665a8c1dac605529d09218b7eb4f8663a07b0854548ae5dcf113296dc5e3921cf4154075e526431dc14b7a71ca708f0c4aec6486ece25c9a0c4ebbba
data/CHANGELOG.md CHANGED
@@ -7,6 +7,15 @@ 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
+
10
19
  ## [0.2.0] - 2026-04-20
11
20
 
12
21
  ### Added
data/README.md CHANGED
@@ -83,6 +83,11 @@ product.url # => "https://acme.myshopify.com/products/lorem-road-bike
83
83
  product.featured_image # => #<Showroom::ProductImage ...> (first image)
84
84
  product.main_image # => #<Showroom::ProductImage ...> (image with position 1)
85
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)
90
+
86
91
  # Variants
87
92
  product.variants.each do |v|
88
93
  puts "#{v.title} #{v.price} #{v.compare_at_price} on_sale=#{v.on_sale?} available=#{v.available?}"
@@ -151,7 +156,7 @@ Available `types:` values: `:product`, `:collection`, `:page`, `:article`, `:que
151
156
  ```ruby
152
157
  result = Showroom.search('lorem', types: [:product, :collection, :page, :article, :query])
153
158
 
154
- result.products # => Array<Search::ProductSuggestion>
159
+ result.products # => Array<Search::ProductSuggestion> (API order)
155
160
  result.collections # => Array<Search::CollectionSuggestion>
156
161
  result.pages # => Array<Search::PageSuggestion>
157
162
  result.articles # => Array<Search::ArticleSuggestion>
@@ -159,6 +164,13 @@ result.queries # => Array<Search::QuerySuggestion>
159
164
 
160
165
  result.products.first.title # => "Lorem Road Bike"
161
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
162
174
  ```
163
175
 
164
176
  ### Loading full models
@@ -227,7 +239,7 @@ end
227
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.
228
240
  - **`/products.json` may be disabled** on some stores. You'll receive a `NotFound` or `ServerError`.
229
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.
230
- - **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.
231
243
 
232
244
  ## License
233
245
 
@@ -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.2.0'
7
+ VERSION = '0.3.0'
8
8
  end
9
9
  end
@@ -170,5 +170,47 @@ module Showroom
170
170
  img['position'] == 1
171
171
  end
172
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
173
215
  end
174
216
  end
@@ -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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 01max