Class: AboutYou::SDK::Criteria::ProductSearchCriteria

Inherits:
Object
  • Object
show all
Defined in:
lib/AboutYou/Criteria/product_search_criteria.rb

Overview

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

Constant Summary collapse

SORT_TYPE_RELEVANCE =

api-call-name for sorting after relevance

'relevance'
SORT_TYPE_UPDATED =

api-call-name for sorting after updated_date

'updated_date'
SORT_TYPE_CREATED =

api-call-name for sorting after created_date

'created_date'
SORT_TYPE_MOST_VIEWED =

api-call-name for sorting after most_viewed

'most_viewed'
SORT_TYPE_PRICE =

api-call-name for sorting after price

'price'
SORT_RELEVANCE =

Sort-argument passed to api for relevance

'relevance'
SORT_UPDATED =

Sort-argument passed to api for last updated

'updated_date'
SORT_CREATED =

Sort-argument passed to api for creation date

'created_date'
SORT_MOST_VIEWED =

Sort-argument passed to api for most viewed

'most_viewed'
SORT_PRICE =

Sort-argument passed to api for price

'price'
SORT_ASC =

api-call-name for sorting ascending

'asc'
SORT_DESC =

api-call-name for sorting descending

'desc'
FILTER_SALE =

api-call-name for filtering the sale

'sale'
FILTER_CATEGORY_IDS =

api-call-name for filtering the categories

'categories'
FILTER_PRICE =

api-call-name for filtering the prices

'prices'
FILTER_SEARCHWORD =

api-call-name for filtering the searchword

'searchword'
FILTER_ATTRIBUTES =

api-call-name for filtering the facets

'facets'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_id) ⇒ ProductSearchCriteria

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

  • Args :

    • session_id -> the session is of an user

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



64
65
66
67
68
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 64

def initialize(session_id)
  self.filter    = {}
  self.session_id = session_id
  self.result    = {}
end

Instance Attribute Details

#filter(key) ⇒ Object

gets the filter value of a given filter key

  • Args :

    • key -> the name of the filter which value should be returned

  • Returns :

    • either a value of a filter or nil if filter key not set



49
50
51
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 49

def filter
  @filter
end

#resultObject

a specification of the result which should be fetched from api



51
52
53
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 51

def result
  @result
end

#session_idObject

the session id of a user



53
54
55
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 53

def session_id
  @session_id
end

Instance Method Details

#boost_products(ids) ⇒ Object

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

  • Args :

    • ids -> the products which should be boosted or the ids of them

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



401
402
403
404
405
406
407
408
409
410
411
412
# 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) ⇒ Object

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



362
363
364
365
366
367
368
369
370
# 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) ⇒ Object

adds the filter passed to the api the given filter value

  • Args :

    • key -> the name of the filter which should be applied

    • value -> the value of the filter which should be applied

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



80
81
82
83
84
# 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) ⇒ Object

sets a filter for filtering by category ids

  • Args :

    • categoryIds -> the category ids used for filtering

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

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



137
138
139
140
141
142
143
144
# 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) ⇒ Object

sets a filter for filtering by Facet-Group

  • Args :

    • facet_group -> Instance of AboutYou::SDK::Model::FacetGroup

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

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



182
183
184
# 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) ⇒ Object

sets a filter for filtering by Facet-Groupset

  • Args :

    • facet_group_set -> Instance of AboutYou::SDK::Model::FacetGroupSet

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

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



196
197
198
# 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) ⇒ Object

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 :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# 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) ⇒ Object

sets a filter for filtering by Price range

  • Args :

    • from -> states the starting value of the price filter

    • to -> states the end value of the price filter

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



210
211
212
213
214
215
216
217
218
219
# 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) ⇒ Object

sets a filter for filtering by sale

  • Args :

    • sale -> determines whether to filter by sale or not [optional]

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



108
109
110
111
112
# 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) ⇒ Object

sets a filter for filtering by a searchword

  • Args :

    • searchword -> the searchword which should be used for filtering

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



123
124
125
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 123

def filter_by_searchword(searchword)
  filter_by(FILTER_SEARCHWORD, searchword)
end

#requires_categoriesObject

this method decides whether categories are required to fetch from api

  • Returns :

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



437
438
439
440
441
442
443
444
445
# 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_facetsObject

this method decides whether facets are required to fetch from api

  • Returns :

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



453
454
455
456
457
458
459
460
461
# 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) ⇒ Object

this method will make the api return all facets

  • Args :

    • limit -> limits the received facets

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



338
339
340
341
342
343
344
345
346
347
# 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) ⇒ Object

this method lets the api return the categories if stated so

  • Args :

    • enable -> determines whether or not the categories should be received from the api [optional]

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



381
382
383
384
385
386
387
388
389
# 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) ⇒ Object

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

  • Args :

    • group_id -> group id which facets you want to receive

    • limit -> limits the received facets

  • Fails :

    • if group_id is not convertable in an Integer

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# 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) ⇒ Object

this method determines whether you receive price ranges or not

  • Args :

    • enable -> if true api will return price ranges [optional]

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



288
289
290
291
292
293
294
295
296
# 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) ⇒ Object

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

  • Args :

    • fields -> an Array of constants from AboutYou::SDK::Criteria::ProductFields

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



424
425
426
427
428
429
# 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) ⇒ Object

this method determines whether you receive only sales products or not

  • Args :

    • enable -> if true api will return only sales products [optional]

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



269
270
271
272
273
274
275
276
277
# 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) ⇒ Object

sets a limit and offset for the product search

  • Args :

    • limit -> limits the amount of results received from the api

    • offset -> sets an offset to skip results [optional]

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



250
251
252
253
254
255
256
257
258
# 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) ⇒ Object

sets a sorting-filter and direction

  • Args :

    • type -> states the type of the sorting

    • direction -> the direction of the sorting [optional]

  • Returns :

    • an instance of AboutYou::SDK::Criteria::ProductSearchCriteria



231
232
233
234
235
236
237
238
# 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_arrayObject

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



471
472
473
474
475
476
477
478
479
480
# 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