Class: AboutYou::SDK::Factory::DefaultModelFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/AboutYou/Factory/default_model_factory.rb

Overview

This class creates model objects from a given api response the methods will get called automatically from the query so no need to call them by hand

author

Collins GmbH & Co KG

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shop_api = nil) ⇒ DefaultModelFactory

Constructor for AboutYou::SDK::Factory::DefaultModelFactory

  • Args :

    • shop_api -> the client which should perform the api calls

    • category_manager -> the category manager responsible for managing the categories

    • facet_manager -> the facet manager responsible for managing the facets

  • Returns :

    • an instance of AboutYou::SDK::Factory::DefaultModelFactory



33
34
35
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 33

def initialize(shop_api = nil)
  self.shop_api = shop_api
end

Instance Attribute Details

#category_managerObject

the category manager responsible for managing the categories



18
19
20
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 18

def category_manager
  @category_manager
end

#facet_managerObject

the facet manager responsible for managing the facets



20
21
22
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 20

def facet_manager
  @facet_manager
end

#shop_apiObject

The client which performs the api calls



16
17
18
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 16

def shop_api
  @shop_api
end

Instance Method Details

#base_image_url=(base_url) ⇒ Object

sets the baseimage url for the image model

  • Args :

    • base_url -> the url which should be used by the image model



56
57
58
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 56

def base_image_url=(base_url)
  AboutYou::SDK::Model::Image.base_url = base_url
end

#create_app(json_object) ⇒ Object

creates an app model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::App



552
553
554
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 552

def create_app(json_object)
  AboutYou::SDK::Model::App.create_from_json(json_object)
end

#create_autocomplete(json_object, _query) ⇒ Object

creates an autocomplete model

  • Args :

    • json_object -> the api response in json format

    • query -> the query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::Autocomplete



70
71
72
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 70

def create_autocomplete(json_object, _query)
  AboutYou::SDK::Model::Autocomplete.create_from_json(json_object, self)
end

#create_basket(json_object, _query) ⇒ Object

creates a basket model

  • Args :

    • json_object -> the api response in json format

    • query -> the query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::Basket



84
85
86
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 84

def create_basket(json_object, _query)
  AboutYou::SDK::Model::Basket.create_from_json(json_object, self)
end

#create_basket_item(json_object, products) ⇒ Object

creates a basket item model

  • Args :

    • json_object -> the api response in json format

    • products -> the product models for which items should be created

  • Returns :

    • an instance of AboutYou::SDK::Model::BasketItem



98
99
100
101
102
103
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 98

def create_basket_item(json_object, products)
  AboutYou::SDK::Model::Basket::BasketItem.create_from_json(
    json_object,
    products
  )
end

#create_basket_set(json_object, products) ⇒ Object

creates a basket set model

  • Args :

    • json_object -> the api response in json format

    • products -> the product models for which items should be created

  • Returns :

    • an instance of AboutYou::SDK::Model::BasketSet



115
116
117
118
119
120
121
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 115

def create_basket_set(json_object, products)
  AboutYou::SDK::Model::BasketSet.create_from_json(
    json_object,
    self,
    products
  )
end

#create_basket_set_item(json_object, products) ⇒ Object

creates a basket setitem model

  • Args :

    • json_object -> the api response in json format

    • products -> the product models for which items should be created

  • Returns :

    • an instance of AboutYou::SDK::Model::BasketSetItem



133
134
135
136
137
138
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 133

def create_basket_set_item(json_object, products)
  AboutYou::SDK::Model::BasketSetItem.create_from_json(
    json_object,
    products
  )
end

#create_brand(json_object) ⇒ Object

this method creates the brand model

  • Args :

    • json_object -> the response from the api in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::Brand



659
660
661
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 659

def create_brand(json_object)
  AboutYou::SDK::Model::Brand.create_from_json(json_object)
end

#create_categories_facets(json_array) ⇒ Object

this method creates the facets for given categories

  • Args :

    • jsonArray -> an Array containing the Api response

  • Returns :

    • a Hash containing pairs of category_id => category



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 634

def create_categories_facets(json_array)
  category_manager = self.category_manager

  flatten_categories = {}
  json_array.each do |item|
    id = item['term']
    category = category_manager.category(id)
    next unless category

    category.product_count = item['count']
    flatten_categories[id] = category
  end

  flatten_categories
end

#create_categories_result(json_object, query) ⇒ Object

creates a categories result model

  • Args :

    • json_object -> the api response in json format

    • query -> the query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::CategoriesResult



452
453
454
455
456
457
458
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 452

def create_categories_result(json_object, query)
  AboutYou::SDK::Model::CategoriesResult.create_from_json(
    json_object,
    query['ids'],
    self
  )
end

#create_category(json_object) ⇒ Object

creates a category model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::Category



149
150
151
152
153
154
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 149

def create_category(json_object)
  AboutYou::SDK::Model::Category.create_from_json(
    json_object,
    category_manager
  )
end

#create_category_tree(json_array, _query) ⇒ Object

creates a category tree model

  • Args :

    • jsonArray -> an Array containing the api response

    • _query -> the _query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::CategoryTree



166
167
168
169
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 166

def create_category_tree(json_array, _query)
  initialize_category_manager(json_array)
  AboutYou::SDK::Model::CategoryTree.new(category_manager)
end

#create_child_apps(json_object, _query) ⇒ Object

creates a Has containing one or multiple pairs of app_id => instance of AboutYou::SDK::Model::App

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

    • a Hash containing pairs of app_id => instance of AboutYou::SDK::Model::App



533
534
535
536
537
538
539
540
541
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 533

def create_child_apps(json_object, _query)
  apps = {}
  json_object['child_apps'].each do |json_app|
    app = create_app(json_app)
    apps[app.id] = app
  end

  apps
end

#create_facet(json_object) ⇒ Object

creates a facet model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::Facet



180
181
182
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 180

def create_facet(json_object)
  AboutYou::SDK::Model::Facet.create_from_json(json_object)
end

#create_facet_list(json_array, _query) ⇒ Object

creates a Hash containing one or multiple pairs of facetKey => AboutYou::SDK::Model::Facet

  • Args :

    • jsonArray -> an Array containing the api response

  • Returns :

    • a Hash containing pairs of facetKey => AboutYou::SDK::Model::Facet



194
195
196
197
198
199
200
201
202
203
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 194

def create_facet_list(json_array, _query)
  facets = {}
  json_array.each do |json_facet|
    facet = create_facet(json_facet)
    key = facet.unique_key
    facets[key] = facet
  end

  facets
end

#create_facet_types(json_array, _query) ⇒ Object

creates an Array containing all facet types available

  • Args :

    • jsonArray -> an Array containing the api response

    • _query -> the _query sent to the api

  • Returns :

    • an Array containing all facet types available



290
291
292
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 290

def create_facet_types(json_array, _query)
  json_array
end

#create_facets_counts(json_object) ⇒ Object

creates a Hash containing one or multiple pairs of group_id => instance of AboutYou::SDK::Model::FacetCounts

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • a Hash containing pairs of group_id => instance of AboutYou::SDK::Model::FacetCounts



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 230

def create_facets_counts(json_object)
  facets_counts = {}

  json_object['facets'].each do |group_id, json_result_facet|
    next unless group_id[/\d/]

    facet_counts = term_facets(
      group_id,
      json_result_facet['terms']
    )
    facets_counts[group_id] =
      AboutYou::SDK::Model::FacetCounts.create_from_json(
        group_id,
        json_result_facet,
        facet_counts
      )
  end

  facets_counts
end

#create_facets_list(json_object, query = nil) ⇒ Object

creates a Hash containing one or multiple pairs of facetKey => AboutYou::SDK::Model::Facet

  • Args :

    • json_object -> the api response in json format

    • query -> the query sent to the api

  • Returns :

    • a Hash containing pairs of facetKey => AboutYou::SDK::Model::Facet



216
217
218
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 216

def create_facets_list(json_object, query = nil)
  create_facet_list(json_object['facet'], query)
end

#create_image(json_object) ⇒ Object

creates an image model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::Image



303
304
305
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 303

def create_image(json_object)
  AboutYou::SDK::Model::Image.create_from_json(json_object)
end

#create_order(json_object, _query) ⇒ Object

creates an order model

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::Order



502
503
504
505
506
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 502

def create_order(json_object, _query)
  basket = create_basket(json_object['basket'])

  AboutYou::SDK::Model::Order.new(json_object['order_id'], basket)
end

#create_price_ranges(json_object) ⇒ Object

this method creates a price range model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an Array containing instances of AboutYou::SDK::Model::PriceRange



599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 599

def create_price_ranges(json_object)
  price_ranges = []
  json_object['ranges'].each do |range|
    price_ranges.push(
      AboutYou::SDK::Model::PriceRange.create_from_json(
        range
      )
    )
  end

  price_ranges
end

#create_product(json_object) ⇒ Object

creates a product model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::Product



316
317
318
319
320
321
322
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 316

def create_product(json_object)
  AboutYou::SDK::Model::Product.create_from_json(
    json_object,
    self,
    shop_api.app_id
  )
end

#create_product_search_result(json_object, _query) ⇒ Object

creates a product searchresult model

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::ProductSearchResult



435
436
437
438
439
440
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 435

def create_product_search_result(json_object, _query)
  AboutYou::SDK::Model::ProductSearchResult.create_from_json(
    json_object,
    self
  )
end

#create_products_ean_result(json_object, _query) ⇒ Object

creates a products eanresult model

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::ProductsEanResult



418
419
420
421
422
423
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 418

def create_products_ean_result(json_object, _query)
  AboutYou::SDK::Model::ProductsEansResult.create_from_json(
    json_object,
    self
  )
end

#create_products_result(json_object, _query) ⇒ Object

creates a products result model

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::ProductsResult



401
402
403
404
405
406
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 401

def create_products_result(json_object, _query)
  AboutYou::SDK::Model::ProductsResult.create_from_json(
    json_object,
    self
  )
end

#create_sale_facet(json_object) ⇒ Object

this method creates a sale counts model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::SaleCounts



621
622
623
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 621

def create_sale_facet(json_object)
  AboutYou::SDK::Model::SaleCounts.create_from_json(json_object)
end

#create_single_product(json_object) ⇒ Object

creates a single product model

  • Args :

    • json_object -> the api response in json format

  • Returns :

    • an instance of AboutYou::SDK::Model::Product



387
388
389
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 387

def create_single_product(json_object)
  createProduct(json_object)
end

#create_spell_correction(json_array, _query) ⇒ Object

creates an spell correction result

  • Args :

    • json_array -> the api response in an array

  • Returns :

    • an Array containing String



565
566
567
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 565

def create_spell_correction(json_array, _query)
  json_array
end

#create_suggest(jsonArray, _query) ⇒ Object

creates an Array containing suggests

  • Args :

    • jsonArray -> an Array with the api response

    • _query -> the _query sent to the api

  • Returns :

    • an Array containing suggests



470
471
472
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 470

def create_suggest(jsonArray, _query)
  jsonArray
end

#create_variant(json_object, product) ⇒ Object

creates a variant model

  • Args :

    • json_object -> the api response in json format

    • product -> the product of the variant which should be created

  • Returns :

    • an instance of AboutYou::SDK::Model::Variant



484
485
486
487
488
489
490
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 484

def create_variant(json_object, product)
  AboutYou::SDK::Model::Variant.create_from_json(
    json_object,
    self,
    product
  )
end

#create_variants_result(json_object, _query) ⇒ Object

creates a variants result model

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::VariantsResult



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 334

def create_variants_result(json_object, _query)
  variants = {}
  errors = []
  product_ids = []
  product_search_result = false

  json_object.each do |id, data|
    if data['error_code']
      errors.push(id)
    else
      variants[data['id']] = data['product_id']
      product_ids.push(data['product_id'])
    end
  end

  if product_ids.count > 0
    product_ids = product_ids.uniq
    # search products for valid variants

    product_search_result = shop_api.fetch_products_by_ids(
        product_ids, [
          AboutYou::SDK::Criteria::ProductFields::ATTRIBUTES_MERGED,
          AboutYou::SDK::Criteria::ProductFields::BRAND,
          AboutYou::SDK::Criteria::ProductFields::CATEGORIES,
          AboutYou::SDK::Criteria::ProductFields::DEFAULT_IMAGE,
          AboutYou::SDK::Criteria::ProductFields::DEFAULT_VARIANT,
          AboutYou::SDK::Criteria::ProductFields::DESCRIPTION_LONG,
          AboutYou::SDK::Criteria::ProductFields::DESCRIPTION_SHORT,
          AboutYou::SDK::Criteria::ProductFields::IS_ACTIVE,
          AboutYou::SDK::Criteria::ProductFields::IS_SALE,
          AboutYou::SDK::Criteria::ProductFields::MAX_PRICE,
          AboutYou::SDK::Criteria::ProductFields::MIN_PRICE,
          AboutYou::SDK::Criteria::ProductFields::VARIANTS
        ]
      )
  end

  AboutYou::SDK::Model::VariantsResult.create(
    variants,
    errors,
    product_search_result
  )
end

#initialize_category_manager(json_object) ⇒ Object

this methods initiates the category manager

  • Args :

    • json_object -> the api response in json format



575
576
577
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 575

def initialize_category_manager(json_object)
  category_manager.parse_json(json_object, self)
end

#initialize_managers(facet_manager, category_manager) ⇒ Object

this method initializes the managers

  • Args :

    • facet_manager -> the facet manager responsible for managing the facets

    • category_manager -> the category manager responsible for managing the categories



44
45
46
47
48
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 44

def initialize_managers(facet_manager, category_manager)
  self.category_manager = category_manager
  self.facet_manager = facet_manager
  AboutYou::SDK::Model::FacetGroupSet.facet_manager = self.facet_manager
end

#initiate_order(json_object, _query) ⇒ Object

creates an initiate order model

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

    • an instance of AboutYou::SDK::Model::InitiateOrder



518
519
520
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 518

def initiate_order(json_object, _query)
  AboutYou::SDK::Model::InitiateOrder.create_from_json(json_object)
end

#pre_handle_error(json, result_key, is_multi_request) ⇒ Object

this method tries to handle errors which are received from the api

  • Args :

    • json -> the api response in json format

    • resultKey -> the result key received from the api

    • isMultiRequest -> determines whether the api-request was multiquery or not

  • Fails :

    • if the result_key is not basket and the json response does not contain order_lines



674
675
676
677
678
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 674

def pre_handle_error(json, result_key, is_multi_request)
  return if result_key == 'basket' && json['order_lines']

  fail 'ResultError!' + json + is_multi_request
end

#term_facets(group_id, json_terms) ⇒ Object

creates an Array containing one or multiple instances of AboutYou::SDK::Model::FacetCount

  • Args :

    • group_id -> the group id of the facets

    • jsonTerms -> the single facet terms in json format

  • Returns :

    • an Array containing instances of AboutYou::SDK::Model::FacetCount



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 262

def term_facets(group_id, json_terms)
  facet_counts = []
  json_terms.each do |json_term|
    id = Integer(json_term['term'])
    facet = facet_manager.facet(group_id, id)
    next unless facet

    # TODO: Handle error, write test
    facet_counts.push(AboutYou::SDK::Model::FacetCount.new(
        facet,
        json_term['count']
      )
    )
  end

  facet_counts
end

#update_facet_manager(json_object, query) ⇒ Object

this methods updates the facet manager

  • Args :

    • json_object -> the api response in json format

    • query -> the query sent to the api



586
587
588
# File 'lib/AboutYou/Factory/default_model_factory.rb', line 586

def update_facet_manager(json_object, query)
  facet_manager.parse_json(json_object, self, query)
end