class AboutYou::SDK::Criteria::ProductSearchCriteria

This class is used as the criteria for a product search Its instance is meant to be passed to the fetchProducrSearch method To add a specific criteria simply call the method on the instance

author

Collins GmbH & Co KG

Constants

FILTER_ATTRIBUTES

api-call-name for filtering the facets

FILTER_CATEGORY_IDS

api-call-name for filtering the categories

FILTER_PRICE

api-call-name for filtering the prices

FILTER_SALE

api-call-name for filtering the sale

FILTER_SEARCHWORD

api-call-name for filtering the searchword

SORT_ASC

api-call-name for sorting ascending

SORT_CREATED

Sort-argument passed to api for creation date

SORT_DESC

api-call-name for sorting descending

SORT_MOST_VIEWED

Sort-argument passed to api for most viewed

SORT_PRICE

Sort-argument passed to api for price

SORT_RELEVANCE

Sort-argument passed to api for relevance

SORT_TYPE_CREATED

api-call-name for sorting after created_date

SORT_TYPE_MOST_VIEWED

api-call-name for sorting after most_viewed

SORT_TYPE_PRICE

api-call-name for sorting after price

SORT_TYPE_RELEVANCE

api-call-name for sorting after relevance

SORT_TYPE_UPDATED

api-call-name for sorting after updated_date

SORT_UPDATED

Sort-argument passed to api for last updated

Attributes

filter[RW]

the filter applied to the api-call

result[RW]

a specification of the result which should be fetched from api

session_id[RW]

the session id of a user

Public Class Methods

new(session_id) click to toggle source

Constructor for AboutYou::SDK::Criteria::ProductSearchCriteria

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 64
def initialize(session_id)
  self.filter    = {}
  self.session_id = session_id
  self.result    = {}
end

Public Instance Methods

boost_products(ids) click to toggle source

this method lets the api return products. It will prefer to return products which are given by ids

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 401
def boost_products(ids)
  mapped_ids = []
  ids = ids.map do
    |val|
    mapped_ids.push(val.id) if val.instance_of?(Product)
    mapped_ids.push(val)
  end
  mapped_ids = mapped_ids.uniq
  result['boosts'] = ids

  self
end
check_facet_limit(limit) click to toggle source

this method checks whether a given facet limit is valid or not

  • Args :

    • limit -> limit which should be checked

  • Fails :

    • if limit is not an integer

    • if limit is smaller then -1

  • Returns :

    • nil if there is no error

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 362
def check_facet_limit(limit)
  fail 'InvalidArgumentException! limit must be an integer' unless
  limit.is_a?(Fixnum) || limit.is_a?(Integer)

  fail 'InvalidArgumentException! limit must be positive or -1
    for unlimited facets' if limit < -1

  nil
end
filter_by(key, value) click to toggle source

adds the filter passed to the api the given filter value

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 80
def filter_by(key, value)
  @filter[key] = value

  self
end
filter_by_category_ids(category_ids, append = false) click to toggle source

sets a filter for filtering by category ids

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 137
def filter_by_category_ids(category_ids, append = false)
  category_ids = filter(FILTER_CATEGORY_IDS) + category_ids if
  append && filter(FILTER_CATEGORY_IDS)

  category_ids = category_ids.uniq

  filter_by(FILTER_CATEGORY_IDS, category_ids)
end
filter_by_facet_group(facet_group, append = false) click to toggle source

sets a filter for filtering by Facet-Group

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 182
def filter_by_facet_group(facet_group, append = false)
  filter_by_facet_ids(facet_group.ids, append)
end
filter_by_facet_group_set(facet_group_set, append = false) click to toggle source

sets a filter for filtering by Facet-Groupset

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 196
def filter_by_facet_group_set(facet_group_set, append = false)
  filter_by_facet_ids(facet_group_set.ids, append)
end
filter_by_facet_ids(attributes, append = false) click to toggle source

sets a filter for filtering by category ids

  • Args :

    • attributes -> Array of Hashes containing the relation Group_Id => Array of Facet_Ids

    • append -> determines whether to append the category ids to an already set value or not [optional]

  • Returns :

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 156
def filter_by_facet_ids(attributes, append = false)
  merged = attributes[0]
  if append && filter(FILTER_ATTRIBUTES)
    merged = filter(FILTER_ATTRIBUTES)
    attributes.each do |group_id, facet_ids|
      if merged.key?(group_id)
        merged[group_id] = (merged[group_id] + facet_ids).uniq
      else
        merged[group_id] = facet_ids
      end
    end
  end

  filter_by(FILTER_ATTRIBUTES, merged)
end
filter_by_price_range(from = 0, to = 0) click to toggle source

sets a filter for filtering by Price range

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 210
def filter_by_price_range(from = 0, to = 0)
  from = Integer(from)
  to = Integer(to)
  price = {}

  price['from'] = from if from > 0
  price['to'] = to if to > 0

  filter_by(FILTER_PRICE, price)
end
filter_by_sale(sale = true) click to toggle source

sets a filter for filtering by sale

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 108
def filter_by_sale(sale = true)
  sale = nil unless sale.is_a?(TrueClass) || sale.is_a?(FalseClass)

  filter_by(FILTER_SALE, sale)
end
filter_by_searchword(searchword) click to toggle source

sets a filter for filtering by a searchword

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 123
def filter_by_searchword(searchword)
  filter_by(FILTER_SEARCHWORD, searchword)
end
requires_categories() click to toggle source

this method decides whether categories are required to fetch from api

  • Returns :

    • a boolean stating whether to fetch categories from api or not

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 437
def requires_categories
  (result.key?('fields') &&
    AboutYou::SDK::Criteria::ProductFields.requires_categories(
    result['fields']
  )) || (
    result.key?('categories') &&
      result['categories']
    )
end
requires_facets() click to toggle source

this method decides whether facets are required to fetch from api

  • Returns :

    • a boolean stating whether to fetch facets from api or not

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 453
def requires_facets
  (result.key?('fields') &&
    AboutYou::SDK::Criteria::ProductFields.requires_facets(
    result['fields']
  )) || (
    result.key?('facets') &&
      !result['facets'].empty?
    )
end
select_all_facets(limit) click to toggle source

this method will make the api return all facets

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 338
def select_all_facets(limit)
  check_facet_limit(limit)
  result['facets'] = {
    FACETS_ALL => {
      'limit' => limit
    }
  }

  self
end
select_categories(enable = true) click to toggle source

this method lets the api return the categories if stated so

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 381
def select_categories(enable = true)
  if enable
    result['categories'] = true
  else
    result['categories'] = nil
  end

  self
end
select_facets_by_group_id(group_id, limit) click to toggle source

this method adds a field to the api-request which enables you to receive Facets by a given group id

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 312
def select_facets_by_group_id(group_id, limit)
  check_facet_limit(limit)

  fail "InvalidArgumentException! Group id must be an integer or a
  string containing digits" unless
  group_id.is_a?(Fixnum) || Integer(group_id)

  result['facets'] = {} unless result['facets']

  result['facets'][String(group_id)] = {
    'limit' => limit
  } unless
  result['facets'][group_id]

  self
end
select_price_ranges(enable = true) click to toggle source

this method determines whether you receive price ranges or not

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 288
def select_price_ranges(enable = true)
  if enable
    result['price'] = true
  else
    result['price'] = nil
  end

  self
end
select_product_fields(fields) click to toggle source

this method lets you select certain AboutYou::SDK::Criteria::ProductFields which will be returned from api

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 424
def select_product_fields(fields)
  result['fields'] =
    AboutYou::SDK::Criteria::ProductFields.filter_fields(fields)

  self
end
select_sales(enable = true) click to toggle source

this method determines whether you receive only sales products or not

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 269
def select_sales(enable = true)
  if enable
    result['sale'] = true
  else
    result['sale'] = nil
  end

  self
end
set_limit(limit, offset = 0) click to toggle source

sets a limit and offset for the product search

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 250
def set_limit(limit, offset = 0)
  limit = [[limit, 200].min, 0].max
  result['limit'] = limit

  offset = [offset, 0].max
  result['offset'] = offset

  self
end
sort_by(type, direction = SORT_ASC) click to toggle source

sets a sorting-filter and direction

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 231
def sort_by(type, direction = SORT_ASC)
  result['sort'] = {
    'by'        => type,
    'direction' => direction
  }

  self
end
to_array() click to toggle source

this method turns an instance of AboutYou::SDK::Criteria::product_search_criteria in an Array which can be passed directly to the api

  • Returns :

    • an Array containing all the parameters set on the instance

# File lib/AboutYou/Criteria/product_search_criteria.rb, line 471
def to_array
  params = {
    'session_id' => session_id
  }

  params['result'] = result if result
  params['filter'] = @filter unless @filter.empty?

  params
end