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) ⇒ Result

Returns a new instance of Result.

Parameters:

  • results_hash (Hash)

    the resources.results hash from the API response



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

def initialize(results_hash)
  @data = results_hash
end

Instance Method Details

#articlesArray<ArticleSuggestion>

Returns article suggestions from the search result.

Returns:



54
55
56
# File 'lib/showroom/models/search/result.rb', line 54

def articles
  @data.fetch('articles', []).map { |h| ArticleSuggestion.new(h) }
end

#collectionsArray<CollectionSuggestion>

Returns collection suggestions from the search result.

Returns:



40
41
42
# File 'lib/showroom/models/search/result.rb', line 40

def collections
  @data.fetch('collections', []).map { |h| CollectionSuggestion.new(h) }
end

#pagesArray<PageSuggestion>

Returns page suggestions from the search result.

Returns:



47
48
49
# File 'lib/showroom/models/search/result.rb', line 47

def pages
  @data.fetch('pages', []).map { |h| PageSuggestion.new(h) }
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



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

def products(order: nil)
  validate_order!(order)
  suggestions = @data.fetch('products', []).map { |h| ProductSuggestion.new(h) }
  return suggestions unless order

  sort_product_suggestions(suggestions, order)
end

#queriesArray<QuerySuggestion>

Returns query suggestions from the search result.

Returns:



61
62
63
# File 'lib/showroom/models/search/result.rb', line 61

def queries
  @data.fetch('queries', []).map { |h| QuerySuggestion.new(h) }
end