Module: AboutYou::SDK::QueryBuilder

Included in:
Query
Defined in:
lib/AboutYou/query_builder.rb

Overview

the QueryBuilder is a module supporting the query with mroe complex logic concerning the building of the queries for the api-call

Author

Collins GmbH & Co KG

Instance Method Summary collapse

Instance Method Details

#add_items_to_basket(session_id, items) ⇒ Object

builds the basket-query

  • Args :

    • session_id -> a String containing the session id of an user

    • items -> an Array containing all of the items which should be added

  • Returns :

    • Instance of AboutYou::SDK::Query



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/AboutYou/query_builder.rb', line 79

def add_items_to_basket(session_id, items)
  check_session_id(session_id)

  order_lines = []

  items.each do |item|
    order_line = {
      'id' => item.id,
      'variant_id' => item.variant_id
    }

    if item.additional_data
      order_line['additional_data'] = item.additional_data
    end

    order_lines.push(order_line)
  end

  query.push(
    'basket' => {
      'session_id' => session_id,
      'order_lines' => order_lines
    }
  )

  self
end

#add_itemsets_to_basket(session_id, itemsets) ⇒ Object

builds the basket-query

  • Args :

    • session_id -> a String containing the session id of an user

    • itemSets -> an instance of AboutYou::SDK::Model::BasketSet

  • Returns :

    • Instance of AboutYou::SDK::Query



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/AboutYou/query_builder.rb', line 117

def add_itemsets_to_basket(session_id, itemsets)
  check_session_id(session_id)

  order_lines = []

  itemsets.each do |item_set|
    order_line = {
      'id' => item_set.id,
      'set_items' => []
    }

    if item_set.additional_data
      order_line['additional_data'] = item_set.additional_data
    end

    item_set.items.each do |item|
      entry = {
        'variant_id' => item.variant_id
      }
      if item.additional_data
        entry['additional_data'] = item.additional_data
      end
      order_line['set_items'].push(entry)
    end

    order_lines.push(order_line)
  end

  query.push(
    'basket' => {
      'session_id' => session_id,
      'order_lines' => order_lines
    }
  )

  self
end

#add_to_basket(session_id, product_variant_id, basket_item_id) ⇒ Object

builds the basket-query

  • Args :

    • session_id -> a String containing the session id of an user

    • productVariantId -> ID of product variant

    • basketItemId -> ID of single item or set in the basket

  • Returns :

    • Instance of AboutYou::SDK::Query



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/AboutYou/query_builder.rb', line 166

def add_to_basket(session_id, product_variant_id, basket_item_id)
  check_session_id(session_id)

  query.push(
    'basket' => {
      'session_id' => session_id,
      'order_lines' => [{
        'id' => basket_item_id,
        'variant_id' => Integer(product_variant_id)
      }]
    }
  )

  self
end

#check_session_id(session_id) ⇒ Object

checks whether the session id is valid or not

  • Args :

    • session_id -> the session id of a user

  • Fails :

    • if the session id is not a String

    • if the session id has less then 4 characters



569
570
571
572
573
574
# File 'lib/AboutYou/query_builder.rb', line 569

def check_session_id(session_id)
  fail 'InvalidArgumentException! The session id must be a string' unless
  session_id.is_a? String
  fail 'InvalidArgumentException! The session id must have at least
    5 characters' unless session_id.length > 4
end

#fetch_autocomplete(searchword, limit = nil, types = nil) ⇒ Object

builds the autocompletion-query

  • Args :

    • searchword -> a String containing the word to search completitions for

    • limit -> Maximum number of results [optional]

    • types -> Array of types to search for [optional]

  • Fails :

    • if the searchword is not a String

    • if the limit cant be transformed into an integer

  • Returns :

    • Instance of AboutYou::SDK::Query



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/AboutYou/query_builder.rb', line 25

def fetch_autocomplete(searchword, limit = nil, types = nil)
  unless searchword.is_a? String
    fail 'InvalidArgumentException! searchword must be a string'
  end

  # downcase is a workaround of ticket SAPI-532
  options = { 'searchword' => searchword.downcase }

  unless limit.nil?
    fail 'InvalidArgumentException! limit must be an integer' unless
    limit.is_a?(Integer) || limit[/\d/]
    options['limit'] = Integer(limit)
  end
  
  options['types'] = types unless types.empty?

  query.push(
    'autocompletion' => options
  )

  self
end

#fetch_basket(session_id) ⇒ Object

builds the basket-query

  • Args :

    • session_id -> a String containing the session id of an user

  • Returns :

    • Instance of AboutYou::SDK::Query



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/AboutYou/query_builder.rb', line 57

def fetch_basket(session_id)
  check_session_id(session_id)

  query.push(
    'basket' => {
      'session_id' => session_id
    }
  )

  self
end

#fetch_categories_by_ids(ids = nil) ⇒ Object

builds the category-query

  • Args :

    • ids -> either a single category ID as integer or an array of IDs

  • Fails :

    • if a category id is smaller then 1

  • Returns :

    • Instance of AboutYou::SDK::Query



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/AboutYou/query_builder.rb', line 249

def fetch_categories_by_ids(ids = nil)
  if ids.nil?
    query.push(
      'category' => nil
    )
  else
    # we allow to pass a single ID instead of an array
    ids = Array(ids)

    ids.each do |id|
      id = Integer(id)
      if id < 1
        fail 'InvalidArgumentException! A single category ID must be greater than 0'
      end
    end

    ids = ids.map { |id| Integer(id) }

    query.push(
      'category' => {
        'ids' => ids
      }
    )
  end

  self
end

#fetch_category_treeObject

builds the category_tree-query

  • Returns :

    • Instance of AboutYou::SDK::Query



283
284
285
286
287
288
289
290
291
# File 'lib/AboutYou/query_builder.rb', line 283

def fetch_category_tree
  query.push(
    'category_tree' => {
      'version' => '2'
    }
  )

  self
end

#fetch_child_appsObject

builds the child_apps-query

  • Returns :

    • Instance of AboutYou::SDK::Query



541
542
543
544
545
546
547
# File 'lib/AboutYou/query_builder.rb', line 541

def fetch_child_apps
  query.push(
    'child_apps' => nil
  )

  self
end

#fetch_facet(params) ⇒ Object

builds the facet-query

  • Args :

    • params -> Hash containing 2 keys: “id”, “group_id”

  • Fails :

    • if params is not set

  • Returns :

    • Instance of AboutYou::SDK::Query



461
462
463
464
465
466
467
468
469
# File 'lib/AboutYou/query_builder.rb', line 461

def fetch_facet(params)
  fail 'InvalidArgumentException! no params given' if params.empty?

  query.push(
    'facet' => params
  )

  self
end

#fetch_facet_typesObject

builds the facet_types-query

  • Returns :

    • Instance of AboutYou::SDK::Query



477
478
479
480
481
482
483
# File 'lib/AboutYou/query_builder.rb', line 477

def fetch_facet_types
  query.push(
    'facet_types' => nil
  )

  self
end

#fetch_facets(group_ids = []) ⇒ Object

builds the facets-query

  • Args :

    • group_ids -> Array of group ids [optional]

  • Returns :

    • Instance of AboutYou::SDK::Query



437
438
439
440
441
442
443
444
445
446
447
# File 'lib/AboutYou/query_builder.rb', line 437

def fetch_facets(group_ids = [])
  group_ids = group_ids.map { |groupId| Integer(groupId) } if group_ids

  query.push(
    'facets' => {
      'group_ids' => group_ids
    }
  )

  self
end

#fetch_live_variant_by_ids(ids) ⇒ Object

builds the live_variant-query

  • Args :

    • ids -> Either a single id or an Array of ids which should be fetched

  • Returns :

    • Instance of AboutYou::SDK::Query



325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/AboutYou/query_builder.rb', line 325

def fetch_live_variant_by_ids(ids)
  # we allow to pass a single ID instead of an array
  ids = Array(ids)
  ids = ids.map { |id| Integer(id) }

  query.push(
    'live_variant' => {
      'ids' => ids
    }
  )

  self
end

#fetch_order(order_id) ⇒ Object

builds the get_order-query

  • Args :

    • orderId -> id of the order which should be fetched

  • Returns :

    • Instance of AboutYou::SDK::Query



370
371
372
373
374
375
376
377
378
# File 'lib/AboutYou/query_builder.rb', line 370

def fetch_order(order_id)
  query.push(
    'get_order' => {
      'order_id' => order_id
    }
  )

  self
end

#fetch_product_search(criteria) ⇒ Object

builds the product_search-query

  • Args :

    • criteria -> Hash containing one or multiple search terms

  • Returns :

    • Instance of AboutYou::SDK::Query



418
419
420
421
422
423
424
425
426
# File 'lib/AboutYou/query_builder.rb', line 418

def fetch_product_search(criteria)
  check_session_id(criteria.session_id)

  query.push(
    'product_search' => criteria.to_array
  )

  self
end

#fetch_products_by_eans(eans, fields = []) ⇒ Object

builds the products_eans-query

  • Args :

    • eans -> Either a single ean or an Array of eans which should be fetched

    • fields -> Additional product fields which should be fetched for each product [optional]

  • Returns :

    • Instance of AboutYou::SDK::Query



349
350
351
352
353
354
355
356
357
358
359
# File 'lib/AboutYou/query_builder.rb', line 349

def fetch_products_by_eans(eans, fields = [])
  query.push(
    'products_eans' => {
      'eans'   => eans,
      'fields' => AboutYou::SDK::Criteria::ProductFields.filter_fields(fields),
      'version' => '2'
    }
  )

  self
end

#fetch_products_by_ids(ids, fields = []) ⇒ Object

builds the products-query

  • Args :

    • ids -> Either a single id or an Array of ids which should be fetched

    • fields -> Additional product fields which should be fetched for each product [optional]

  • Returns :

    • Instance of AboutYou::SDK::Query



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/AboutYou/query_builder.rb', line 303

def fetch_products_by_ids(ids, fields = [])
  # we allow to pass a single ID instead of an array
  ids = Array(ids).map { |id| Integer(id) }

  query.push(
    'products' => {
      'ids' => ids,
      'fields' => AboutYou::SDK::Criteria::ProductFields.filter_fields(fields)
    }
  )
  self
end

#fetch_spell_correction(searchword, category_ids = nil) ⇒ Object

builds the spell_correction

  • Args :

    • searchword -> the search string to search for

    • category_ids -> the cat ids to filter for

  • Fails :

    • if the searchword is not a String

  • Returns :

    • Instance of AboutYou::SDK::Query



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/AboutYou/query_builder.rb', line 517

def fetch_spell_correction(searchword, category_ids = nil)
  fail 'InvalidArgumentException! searchword must be a string' unless
  searchword.is_a?(String)

  options = {
      'searchword' => searchword
  }
  options['filter'] = {
      'categories' => category_ids
  } unless category_ids.empty?

  query.push(
      'did_you_mean' => options
  )

  self
end

#fetch_suggest(searchword) ⇒ Object

builds the suggest-query

  • Args :

    • searchword -> the searchs tring to search for

  • Returns :

    • Instance of AboutYou::SDK::Query



494
495
496
497
498
499
500
501
502
# File 'lib/AboutYou/query_builder.rb', line 494

def fetch_suggest(searchword)
  query.push(
    'suggest' => {
      'searchword' => searchword
    }
  )

  self
end

#initiate_order(session_id, success_url, cancel_url, error_url) ⇒ Object

builds the initiate_order-query

  • Args :

    • session_id -> A String containing the sessionId of the User

    • successUrl -> callback URL if the order was OK

    • cancelUrl -> callback URL if the order was canceled [optional]

    • errorUrl -> callback URL if the order had any exceptions [optional]

  • Returns :

    • Instance of AboutYou::SDK::Query



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/AboutYou/query_builder.rb', line 392

def initiate_order(session_id, success_url, cancel_url, error_url)
  check_session_id(session_id)

  args = {
    'session_id' => session_id,
    'success_url' => success_url
  }
  args['cancel_url'] = cancel_url if cancel_url
  args['error_url'] = error_url if error_url

  query.push(
    'initiate_order' => args
  )

  self
end

#query_stringObject

turns all queries built in here into a json string

  • Returns :

    • Json string containing all queries built in here



555
556
557
# File 'lib/AboutYou/query_builder.rb', line 555

def query_string
  query.to_json
end

#remove_from_basket(session_id, item_ids) ⇒ Object

builds the basket-query

  • Args :

    • session_id -> a String containing the session id of an user

    • itemIds -> array of basket item ids to delete, this can be sets or single items

  • Returns :

    • Instance of AboutYou::SDK::Query



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/AboutYou/query_builder.rb', line 192

def remove_from_basket(session_id, item_ids)
  check_session_id(session_id)

  order_lines = []

  item_ids.each do |id|
    order_lines.push(
      'delete' => id
    )
  end

  query.push(
    'basket' => {
      'session_id' => session_id,
      'order_lines' => order_lines
    }
  )

  self
end

#update_basket(session_id, basket) ⇒ Object

builds the basket-query

  • Args :

    • session_id -> a String containing the session id of an user

    • basket -> an instance of AboutYou::SDK::Model::Basket

  • Returns :

    • Instance of AboutYou::SDK::Query



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/AboutYou/query_builder.rb', line 223

def update_basket(session_id, basket)
  check_session_id(session_id)

  order_lines = basket.order_lines_array
  basket_query = { 'session_id'  => session_id }
  basket_query['order_lines'] = order_lines unless order_lines.empty?

  query.push(
    'basket' => basket_query
  )

  self
end