Class: AboutYou::SDK::Query

Inherits:
Object
  • Object
show all
Includes:
QueryBuilder
Defined in:
lib/AboutYou/query.rb

Overview

The Query class coordinates the building, executing and parsing of one or multiple API-calls

Author

Collins GmbH & Co KG

Constant Summary collapse

QUERY_TREE =

used for checking whether a query contains a category-request or not

'category_tree'
QUERY_FACETS =

used for checking whether a query contains a facets-request or not

'facets'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryBuilder

#add_items_to_basket, #add_itemsets_to_basket, #add_to_basket, #check_session_id, #fetch_categories_by_ids, #fetch_category_tree, #fetch_child_apps, #fetch_facet, #fetch_facet_types, #fetch_live_variant_by_ids, #fetch_order, #fetch_spell_correction, #fetch_suggest, #initiate_order, #remove_from_basket, #update_basket

Constructor Details

#initialize(client, factory) ⇒ Query

the Constructor for the Query class

  • Args :

    • client -> an instance of AboutYou::SDK::Client

    • factory -> an instance of AboutYou::SDK::Factory::DefaultModelFactory

  • Returns :

    • Instance of AboutYou::SDK::Query



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/AboutYou/query.rb', line 41

def initialize(client, factory)
  self.client = client
  self.query = []
  self.ghost_query = []
  self.all_query = []
  self.factory = factory
  self.mapping = {
    'autocompletion' => 'create_autocomplete',
    'basket'         => 'create_basket',
    'category'       => 'create_categories_result',
    'category_tree'  => 'create_category_tree',
    'facets'         => 'create_facets_list',
    'facet'          => 'create_facet_list',
    'facet_types'    => 'create_facet_types',
    'products'       => 'create_products_result',
    'products_eans'  => 'create_products_ean_result',
    'product_search' => 'create_product_search_result',
    'suggest'        => 'create_suggest',
    'get_order'      => 'create_order',
    'initiate_order' => 'initiate_order',
    'child_apps'     => 'create_child_apps',
    'live_variant'   => 'create_variants_result',
    'did_you_mean'   => 'create_spell_correction'
  }
end

Instance Attribute Details

#all_queryObject

the actual query send to the api containing query and ghost-query



29
30
31
# File 'lib/AboutYou/query.rb', line 29

def all_query
  @all_query
end

#clientObject

The Api-Client which performs the Api-Calls



19
20
21
# File 'lib/AboutYou/query.rb', line 19

def client
  @client
end

#factoryObject

the model factory which builds the models from the api-response



25
26
27
# File 'lib/AboutYou/query.rb', line 25

def factory
  @factory
end

#ghost_queryObject

a helper-query used for fetching categories and/or facets if needed



27
28
29
# File 'lib/AboutYou/query.rb', line 27

def ghost_query
  @ghost_query
end

#mappingObject

a Hash containing the mapping for api_call_name => method_name_in_model_factory



23
24
25
# File 'lib/AboutYou/query.rb', line 23

def mapping
  @mapping
end

#queryObject

the query built by the app itself



21
22
23
# File 'lib/AboutYou/query.rb', line 21

def query
  @query
end

Instance Method Details

#check_response(json_response) ⇒ Object

this method checks whether the api delievered a valid json-response or not

  • Args :

    • jsonResponse -> the plain json received from the api

  • Fails :

    • if the response is false, not an array or has a different amount of subresponses then requests

    • if there is any subresponse which was not requested

    • if there is no model factory mapping for a subresponse



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/AboutYou/query.rb', line 270

def check_response(json_response)
  fail 'UnexpectedResultException!' if
  json_response == false ||
  !json_response.is_a?(Array) ||
  json_response.count != all_query.count

  (0..json_response.count - 1).each do |index|
    current_query = all_query[index]
    response_key  = json_response[index].keys[0]
    query_key     = current_query.keys[0]

    fail 'UnexpectedResultException! result ' + String(query_key) +
      ' expected, but ' + String(response_key) + ' given on position ' +
      String(index) + ' - query: ' + current_query.to_json if
    response_key != query_key

    fail 'UnexpectedResultException! ' + String(response_key) +
      ' is unknown result' unless mapping.key? response_key
  end
end

#executeObject

requests all of the queries and returns the parsed api-response

  • Returns :

    • an Array containing all of the models build with the data of the api



243
244
245
246
# File 'lib/AboutYou/query.rb', line 243

def execute
  return [] if query.empty? && ghost_query.empty?
  parse_result(client.request(query_string), query.count > 1)
end

#execute_singleObject

requests all of the queries and returns only the first parsed api-response

  • Returns :

    • the first model build with the data of the api



254
255
256
# File 'lib/AboutYou/query.rb', line 254

def execute_single
  execute[-1]
end

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

wrapper-method for fetch_autocomplete which coordinates Category- and Facet-Manager

  • 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]

  • Returns :

    • Instance of AboutYou::SDK::Query



78
79
80
81
82
83
84
85
# File 'lib/AboutYou/query.rb', line 78

def fetch_autocomplete(searchword, limit = nil, types = nil)
  super(searchword, limit, types)

  require_category_tree
  require_facets

  self
end

#fetch_basket(session_id) ⇒ Object

wrapper-method for fetch_basket which coordinates Category- and Facet-Manager

  • Args :

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

  • Returns :

    • Instance of AboutYou::SDK::Query



96
97
98
99
100
101
102
103
# File 'lib/AboutYou/query.rb', line 96

def fetch_basket(session_id)
  super(session_id)

  require_category_tree
  require_facets

  self
end

#fetch_facets(group_ids = []) ⇒ Object



105
106
107
108
109
# File 'lib/AboutYou/query.rb', line 105

def fetch_facets(group_ids = [])
  super(group_ids)

  self
end

#fetch_product_search(criteria) ⇒ Object

wrapper-method for fetch_product_search which coordinates Category- and Facet-Manager

  • Args :

    • criteria -> Hash containing one or multiple search terms

  • Returns :

    • Instance of AboutYou::SDK::Query



162
163
164
165
166
167
168
169
# File 'lib/AboutYou/query.rb', line 162

def fetch_product_search(criteria)
  super(criteria)

  require_category_tree if criteria.requires_categories
  require_facets if criteria.requires_facets

  self
end

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

wrapper-method for fetch_products_by_eans which coordinates Category- and Facet-Manager

  • 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



142
143
144
145
146
147
148
149
150
151
# File 'lib/AboutYou/query.rb', line 142

def fetch_products_by_eans(eans, fields = [])
  super(eans, fields)

  require_category_tree if
  AboutYou::SDK::Criteria::ProductFields.requires_categories(fields)
  require_facets if
  AboutYou::SDK::Criteria::ProductFields.requires_facets(fields)

  self
end

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

wrapper-method for fetch_products_by_ids which coordinates Category- and Facet-Manager

  • 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



121
122
123
124
125
126
127
128
129
130
# File 'lib/AboutYou/query.rb', line 121

def fetch_products_by_ids(ids, fields = [])
  super(ids, fields)

  require_category_tree if
  AboutYou::SDK::Criteria::ProductFields.requires_categories(fields)
  require_facets if
  AboutYou::SDK::Criteria::ProductFields.requires_facets(fields)

  self
end

#parse_result(json_response, is_multi_request = true) ⇒ Object

parses the plain json result from the api and calls the respective modelfactory methods for building the models for the json resposne

  • Args :

    • jsonResponse -> the plain json received from the api

    • isMultiRequest -> boolean which determines whether the request has more then one query or not [optional]

  • Returns :

    • an Array containing all models build from the modelfactory



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/AboutYou/query.rb', line 302

def parse_result(json_response, is_multi_request = true)
  check_response(json_response)
  results = []
  query_ids = []

  all_query.each do |query|
    query_ids.push(query.keys[0])
  end

  json_response.each_with_index do |response_object, index|
    current_query   = all_query[index]
    result_key      = response_object.keys[0]
    json_object     = response_object[result_key]
    query_key       = current_query.keys[0]
    factory         = self.factory
    if json_object.is_a?(Hash) && json_object['error_code']
      result = factory.pre_handle_error(
        json_object,
        result_key,
        is_multi_request
      )

      if result != false
        results.push(
          result_key => result
        )
        next
      end
    end

    query    = current_query[query_key]
    query_id = query_ids[index]
    if query_id == QUERY_FACETS
      factory.update_facet_manager(json_object, query)
    elsif query_id == QUERY_TREE
      factory.initialize_category_manager(json_object)
    else
      method = mapping[result_key]
      result = factory.send(method, json_object, query)
      results.push(result)
    end
  end

  results
end

#query_stringObject

the methods builds the complete query string which represents the body of the api call

  • Returns :

    • JSON-String containing all queries which need to be executed



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

def query_string
  result = []

  ghost_query.each do |ghost_query|
    result.push(
      ghost_query[ghost_query.keys[0]]
    ) if ghost_query.is_a?(Hash)
  end

  self.all_query = result + query

  (result + query).to_json
end

#require_category_tree(fetch_forced = false) ⇒ Object

this method checks whether it is neccessary for the query to get the category-tree from the api

  • Args :

    • fetchForced -> determines whether the requirance of the category tree is forced

  • Returns :

    • Instance of AboutYou::SDK::Query



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/AboutYou/query.rb', line 180

def require_category_tree(fetch_forced = false)
  return self unless

  fetch_forced || factory.category_manager.empty?

  ghost_query.push(
    QUERY_TREE => {
      'category_tree' => {
        'version' => '2'
      }
    }
  )

  self
end

#require_facets(fetch_forced = false) ⇒ Object

this method checks whether it is neccessary for the query to get the facets from the api

  • Args :

    • fetchForced -> determines whether the requirance of the facets is forced

  • Returns :

    • Instance of AboutYou::SDK::Query



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/AboutYou/query.rb', line 205

def require_facets(fetch_forced = false)
  return self unless fetch_forced || factory.facet_manager.empty?

  ghost_query.push(
    QUERY_FACETS => {
      'facets' => {}
    }
  )

  self
end