showroom 0.2.0 → 0.3.1

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: 0a879aafd609e03c490716579f3051e97ca92108252ac53cf63256db2ccb8ae5
4
+ data.tar.gz: fbfea4d3ce7580bfbb131a1643e0000f6d7c4a8a7865289337cfabcf8ee0a7fe
5
5
  SHA512:
6
- metadata.gz: 829309cb01db6bfbaa0a7d0134c038e266f14340259058e16139b46a4431f44993a1720f81f32a36a6b4306b96b64664eed0fc9c4ba274f3f7095d6937515634
7
- data.tar.gz: 96a9bcfcf7fe25a4950c49df8181ebaf77928e43e88f94630a914dbc3c082e4edd12e94f6db349af2723801a5b05256b0891510de14e7567bee59c4dd9d37813
6
+ metadata.gz: c523a5ff93f4931a403c9b59a6104e0c014d17742977a7c3c786f8276f7892f2ff751e96d18643463f9efca5fae3acbb0c36213325675896c7a24a67ea45f7f3
7
+ data.tar.gz: 858be74b2a6d8fa7429cd014e04a332699cf5c99edd7afc2485617d95b5e3e43d897a8809bf7f419297d93b55b41cdd3a150348d8ae1805134a3ceec89836df8
data/CHANGELOG.md CHANGED
@@ -7,6 +7,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.1] - 2026-04-25
11
+
12
+ ### Changed
13
+
14
+ - `ProductVariant#available?` now returns `nil` (instead of `false`) when the source payload omits the `available` key — some Shopify storefronts do not expose it on `/products/{handle}.json`, so distinguishing "unknown" from "unavailable" is necessary to avoid silently reporting every variant as out of stock
15
+ - `Product#available?` now returns `true | false | nil`: it honours a top-level `available` key when present, otherwise aggregates variants — any `true` → `true`, all `false` → `false`, otherwise `nil`
16
+
17
+ ### Added
18
+
19
+ - `ProductVariant#availability_known?` — `true` when the payload included an `available` key
20
+ - `Product#availability_known?` — `true` when product-level availability can be determined from a top-level key or any variant
21
+
22
+ ## [0.3.0] - 2026-04-24
23
+
24
+ ### Added
25
+
26
+ - `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
27
+ - `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
28
+
29
+ ### Fixed
30
+
31
+ - Rakefile: corrected gem-build task so `rake build` works correctly
32
+
10
33
  ## [0.2.0] - 2026-04-20
11
34
 
12
35
  ### Added
data/README.md CHANGED
@@ -75,17 +75,23 @@ product = Showroom::Product.find('lorem-road-bike')
75
75
  product.title # => "Lorem Road Bike"
76
76
  product.handle # => "lorem-road-bike"
77
77
  product.vendor # => "Lorem Cycles"
78
- product.available? # => true (any variant in stock)
79
- product.price # => "749.00" (lowest variant price)
78
+ product.available? # => true | false | nil (nil when no variant exposes availability)
79
+ product.availability_known? # => true when availability can be determined
80
+ product.price # => "749.00" (lowest variant price)
80
81
  product.price_range # => "749.00–899.00"
81
82
  product.prices # => ["749.00", "899.00"] (unique prices from all variants)
82
83
  product.url # => "https://acme.myshopify.com/products/lorem-road-bike"
83
84
  product.featured_image # => #<Showroom::ProductImage ...> (first image)
84
85
  product.main_image # => #<Showroom::ProductImage ...> (image with position 1)
85
86
 
87
+ # Similar products — searches by handle (SKU fragments stripped), falls back to title
88
+ product.similar # => Array<Search::ProductSuggestion>, sorted by price
89
+ product.similar(limit: 3) # forward any Client#search keyword
90
+ product.similar.first.load # => Showroom::Product (full record)
91
+
86
92
  # Variants
87
93
  product.variants.each do |v|
88
- puts "#{v.title} #{v.price} #{v.compare_at_price} on_sale=#{v.on_sale?} available=#{v.available?}"
94
+ puts "#{v.title} #{v.price} #{v.compare_at_price} on_sale=#{v.on_sale?} available=#{v.available?} availability_known=#{v.availability_known?}"
89
95
  end
90
96
 
91
97
  # All pages — returns an Enumerator
@@ -151,7 +157,7 @@ Available `types:` values: `:product`, `:collection`, `:page`, `:article`, `:que
151
157
  ```ruby
152
158
  result = Showroom.search('lorem', types: [:product, :collection, :page, :article, :query])
153
159
 
154
- result.products # => Array<Search::ProductSuggestion>
160
+ result.products # => Array<Search::ProductSuggestion> (API order)
155
161
  result.collections # => Array<Search::CollectionSuggestion>
156
162
  result.pages # => Array<Search::PageSuggestion>
157
163
  result.articles # => Array<Search::ArticleSuggestion>
@@ -159,6 +165,13 @@ result.queries # => Array<Search::QuerySuggestion>
159
165
 
160
166
  result.products.first.title # => "Lorem Road Bike"
161
167
  result.queries.first.text # => "lorem road bike"
168
+
169
+ # Sort product suggestions client-side
170
+ result.products(order: :price) # numeric ascending
171
+ result.products(order: :title) # alphabetical
172
+ result.products(order: :handle) # alphabetical
173
+ result.products(order: :id) # numeric ascending
174
+ # Accepted values: :id, :title, :handle, :price — raises ArgumentError otherwise
162
175
  ```
163
176
 
164
177
  ### Loading full models
@@ -227,7 +240,7 @@ end
227
240
  - **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
241
  - **`/products.json` may be disabled** on some stores. You'll receive a `NotFound` or `ServerError`.
229
242
  - **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.
243
+ - **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
244
 
232
245
  ## License
233
246
 
@@ -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.1'
8
8
  end
9
9
  end
@@ -125,11 +125,32 @@ module Showroom
125
125
  prices.length == 1 ? prices.first : "#{prices.min} - #{prices.max}"
126
126
  end
127
127
 
128
- # Returns true when at least one variant is available for purchase.
128
+ # Returns true when the product is available for purchase, false when it
129
+ # is not, or nil when availability cannot be determined from the payload.
129
130
  #
130
- # @return [Boolean]
131
+ # If the response carries a top-level +available+ key, that value wins.
132
+ # Otherwise the result is aggregated from variants:
133
+ # any variant +true+ → +true+; all variants +false+ → +false+;
134
+ # otherwise (mix of +false+ and unknowns, or all unknown) → +nil+.
135
+ #
136
+ # @return [Boolean, nil]
131
137
  def available?
132
- variants.any?(&:available?)
138
+ return @attrs['available'] == true if @attrs.key?('available')
139
+
140
+ states = variants.map(&:available?)
141
+ return true if states.include?(true)
142
+ return false if states.all? { |s| s == false }
143
+
144
+ nil # rubocop:disable Style/ReturnNilInPredicateMethodDefinition
145
+ end
146
+
147
+ # Returns true when product-level availability can be determined,
148
+ # either from a top-level +available+ key or from at least one variant
149
+ # whose own availability is known.
150
+ #
151
+ # @return [Boolean]
152
+ def availability_known?
153
+ @attrs.key?('available') || variants.any?(&:availability_known?)
133
154
  end
134
155
 
135
156
  # Returns the first image, or nil if there are no images.
@@ -170,5 +191,47 @@ module Showroom
170
191
  img['position'] == 1
171
192
  end
172
193
  end
194
+
195
+ # Searches for products similar to this one using the product handle as
196
+ # the query, with any variant SKU fragments stripped out first.
197
+ #
198
+ # SKU fragments are matched case-insensitively as hyphen-delimited segments
199
+ # within the handle. For example, a handle of +"o2feel-equo-42-rr2px5"+ on
200
+ # a product whose variant carries SKU +"RR2PX5"+ yields the query
201
+ # +"o2feel-equo-42"+.
202
+ #
203
+ # @param kwargs [Hash] additional options forwarded to +Client#search+
204
+ # @return [Array<Search::ProductSuggestion>]
205
+ def similar(**kwargs)
206
+ search_args = kwargs.merge(types: %i[product])
207
+
208
+ handle_similars = similar_by_stripped_handle(**search_args)
209
+ return handle_similars unless handle_similars.empty?
210
+
211
+ similar_by_title(**search_args)
212
+ end
213
+
214
+ private
215
+
216
+ # @api private
217
+ def similar_by_stripped_handle(**)
218
+ (client || Showroom.client).search(stripped_handle_search_query, **).products(order: 'price')
219
+ end
220
+
221
+ # @api private
222
+ def similar_by_title(**)
223
+ (client || Showroom.client).search(title, **).products(order: 'price')
224
+ end
225
+
226
+ # Strips variant SKU fragments from the handle to build the search query.
227
+ #
228
+ # @return [String]
229
+ def stripped_handle_search_query
230
+ skus = variants.filter_map { |v| v.sku.to_s.strip.downcase }.reject(&:empty?).uniq
231
+ skus.inject(handle.to_s) do |h, sku|
232
+ h.sub(/(^|-)#{Regexp.escape(sku)}((?=-)|$)/i, '')
233
+ .gsub(/^-|-$/, '')
234
+ end
235
+ end
173
236
  end
174
237
  end
@@ -10,13 +10,26 @@ module Showroom
10
10
  class ProductVariant < Resource
11
11
  main_attrs :id, :title, :price, :compare_at_price, :sku
12
12
 
13
- # Returns true when the variant is available for purchase.
13
+ # Returns true when the variant is available for purchase, false when
14
+ # explicitly unavailable, or nil when the source payload omits the
15
+ # +available+ key (some Shopify storefronts do not expose it on
16
+ # +/products/{handle}.json+).
14
17
  #
15
- # @return [Boolean]
18
+ # @return [Boolean, nil]
16
19
  def available?
20
+ return nil unless availability_known? # rubocop:disable Style/ReturnNilInPredicateMethodDefinition
21
+
17
22
  @attrs['available'] == true
18
23
  end
19
24
 
25
+ # Returns true when the source payload included an +available+ key,
26
+ # making {#available?} authoritative.
27
+ #
28
+ # @return [Boolean]
29
+ def availability_known?
30
+ @attrs.key?('available')
31
+ end
32
+
20
33
  # Returns true when +compare_at_price+ is present and greater than +price+.
21
34
  #
22
35
  # @return [Boolean]
@@ -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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - 01max