Class: AboutYou::SDK::Criteria::ProductSearchCriteria
- Inherits:
-
Object
- Object
- AboutYou::SDK::Criteria::ProductSearchCriteria
- 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'- FACETS_ALL =
api-call name for requiring all facets
'_all'- FACETS_UNLIMITED =
api-call-value for requiring unlimited facets
-1- 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
-
#filter(key) ⇒ Object
gets the filter value of a given filter key.
-
#result ⇒ Object
a specification of the result which should be fetched from api.
-
#session_id ⇒ Object
the session id of a user.
Instance Method Summary collapse
-
#boost_products(ids) ⇒ Object
this method lets the api return products.
-
#check_facet_limit(limit) ⇒ Object
this method checks whether a given facet limit is valid or not.
-
#filter_by(key, value) ⇒ Object
adds the filter passed to the api the given filter value.
-
#filter_by_category_ids(category_ids, append = false) ⇒ Object
sets a filter for filtering by category ids.
-
#filter_by_facet_group(facet_group, append = false) ⇒ Object
sets a filter for filtering by Facet-Group.
-
#filter_by_facet_group_set(facet_group_set, append = false) ⇒ Object
sets a filter for filtering by Facet-Groupset.
-
#filter_by_facet_ids(attributes, append = false) ⇒ Object
sets a filter for filtering by category ids.
-
#filter_by_price_range(from = 0, to = 0) ⇒ Object
sets a filter for filtering by Price range.
-
#filter_by_sale(sale = true) ⇒ Object
sets a filter for filtering by sale.
-
#filter_by_searchword(searchword) ⇒ Object
sets a filter for filtering by a searchword.
-
#initialize(session_id) ⇒ ProductSearchCriteria
constructor
Constructor for AboutYou::SDK::Criteria::ProductSearchCriteria.
-
#requires_categories ⇒ Object
this method decides whether categories are required to fetch from api.
-
#requires_facets ⇒ Object
this method decides whether facets are required to fetch from api.
-
#select_all_facets(limit) ⇒ Object
this method will make the api return all facets.
-
#select_categories(enable = true) ⇒ Object
this method lets the api return the categories if stated so.
-
#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.
-
#select_price_ranges(enable = true) ⇒ Object
this method determines whether you receive price ranges or not.
-
#select_product_fields(fields) ⇒ Object
this method lets you select certain AboutYou::SDK::Criteria::ProductFields which will be returned from api.
-
#select_sales(enable = true) ⇒ Object
this method determines whether you receive only sales products or not.
-
#set_limit(limit, offset = 0) ⇒ Object
sets a limit and offset for the product search.
-
#sort_by(type, direction = SORT_ASC) ⇒ Object
sets a sorting-filter and direction.
-
#to_array ⇒ Object
this method turns an instance of AboutYou::SDK::Criteria::product_search_criteria in an Array which can be passed directly to the api.
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
-
71 72 73 74 75 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 71 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
-
56 57 58 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 56 def filter @filter end |
#result ⇒ Object
a specification of the result which should be fetched from api
58 59 60 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 58 def result @result end |
#session_id ⇒ Object
the session id of a user
60 61 62 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 60 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
-
-
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
-
87 88 89 90 91 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 87 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
-
144 145 146 147 148 149 150 151 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 144 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
-
189 190 191 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 189 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
-
203 204 205 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 203 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
-
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 163 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
-
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 217 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
-
115 116 117 118 119 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 115 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
-
130 131 132 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 130 def filter_by_searchword(searchword) filter_by(FILTER_SEARCHWORD, searchword) end |
#requires_categories ⇒ Object
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_facets ⇒ Object
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
-
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 342 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
-
-
Returns :
-
an instance of AboutYou::SDK::Criteria::ProductSearchCriteria
-
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 316 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
-
295 296 297 298 299 300 301 302 303 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 295 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
-
276 277 278 279 280 281 282 283 284 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 276 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
-
257 258 259 260 261 262 263 264 265 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 257 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
-
238 239 240 241 242 243 244 245 |
# File 'lib/AboutYou/Criteria/product_search_criteria.rb', line 238 def sort_by(type, direction = SORT_ASC) result['sort'] = { 'by' => type, 'direction' => direction } self end |
#to_array ⇒ Object
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 |