Class: Showroom::Search::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/showroom/models/search/result.rb

Overview

Wraps the resources.results hash from a Shopify search suggest response, exposing typed arrays for each resource kind.

Examples:

result = Showroom::Search::Result.new(raw['resources']['results'])
result.products     # => [Array<ProductSuggestion>]
result.collections  # => [Array<CollectionSuggestion>]
result.queries      # => [Array<QuerySuggestion>]

Instance Method Summary collapse

Constructor Details

#initialize(results_hash, client: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • results_hash (Hash)

    the resources.results hash from the API response

  • client (Showroom::Client, nil) (defaults to: nil)

    the client to propagate onto suggestions



16
17
18
19
# File 'lib/showroom/models/search/result.rb', line 16

def initialize(results_hash, client: nil)
  @data = results_hash
  @client = client
end

Instance Method Details

#articlesArray<ArticleSuggestion>

Returns article suggestions from the search result.

Returns:



56
57
58
# File 'lib/showroom/models/search/result.rb', line 56

def articles
  build_suggestions('articles', ArticleSuggestion)
end

#collectionsArray<CollectionSuggestion>

Returns collection suggestions from the search result.

Returns:



42
43
44
# File 'lib/showroom/models/search/result.rb', line 42

def collections
  build_suggestions('collections', CollectionSuggestion)
end

#pagesArray<PageSuggestion>

Returns page suggestions from the search result.

Returns:



49
50
51
# File 'lib/showroom/models/search/result.rb', line 49

def pages
  build_suggestions('pages', PageSuggestion)
end

#products(order: nil) ⇒ Array<ProductSuggestion>

Returns product suggestions from the search result, optionally sorted.

Parameters:

  • order (Symbol, nil) (defaults to: nil)

    attribute to sort by: :id, :title, :handle, or :price. When nil (default) the API response order is preserved. price is sorted numerically; all others alphabetically/numerically by natural value.

Returns:

Raises:

  • (ArgumentError)

    when an unsupported order attribute is given



31
32
33
34
35
36
37
# File 'lib/showroom/models/search/result.rb', line 31

def products(order: nil)
  validate_order!(order)
  suggestions = build_suggestions('products', ProductSuggestion)
  return suggestions unless order

  sort_product_suggestions(suggestions, order)
end

#queriesArray<QuerySuggestion>

Returns query suggestions from the search result.

Returns:



63
64
65
# File 'lib/showroom/models/search/result.rb', line 63

def queries
  build_suggestions('queries', QuerySuggestion)
end