class AboutYou::SDK::Factory::DefaultModelFactory

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

Attributes

category_manager[RW]

the category manager responsible for managing the categories

facet_manager[RW]

the facet manager responsible for managing the facets

shop_api[RW]

The client which performs the api calls

Public Class Methods

new(shop_api = nil) click to toggle source

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 :

# File lib/AboutYou/Factory/default_model_factory.rb, line 33
def initialize(shop_api = nil)
  self.shop_api = shop_api
end

Public Instance Methods

base_image_url=(base_url) click to toggle source

sets the baseimage url for the image model

  • Args :

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

# 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) click to toggle source

creates an app model

# 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) click to toggle source

creates an autocomplete model

# 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) click to toggle source

creates a basket model

  • Args :

    • json_object -> the api response in json format

    • query -> the query sent to the api

  • Returns :

# 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) click to toggle source

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 :

# 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) click to toggle source

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 :

# 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) click to toggle source

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 :

# 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) click to toggle source

this method creates the brand model

# 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) click to toggle source

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

# 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) click to toggle source

creates a categories result model

# 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) click to toggle source

creates a category model

# 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) click to toggle source

creates a category tree model

# 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) click to toggle source

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 :

# 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) click to toggle source

creates a facet model

# 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) click to toggle source

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

  • Args :

    • jsonArray -> an Array containing the api response

  • Returns :

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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 :

# 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) click to toggle source

creates an image model

# 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) click to toggle source

creates an order model

  • Args :

    • json_object -> the api response in json format

    • _query -> the _query sent to the api

  • Returns :

# 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) click to toggle source

this method creates a price range model

# 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) click to toggle source

creates a product model

# 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) click to toggle source

creates a product searchresult model

# 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) click to toggle source

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

# 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) click to toggle source

creates a products result model

# 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) click to toggle source

this method creates a sale counts model

# 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) click to toggle source

creates a single product model

# 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) click to toggle source

creates an spell correction result

  • Args :

    • json_array -> the api response in an array

  • Returns :

    • an Array containing String

# File lib/AboutYou/Factory/default_model_factory.rb, line 565
def create_spell_correction(json_array, _query)
  json_array
end
create_suggest(jsonArray, _query) click to toggle source

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

# File lib/AboutYou/Factory/default_model_factory.rb, line 470
def create_suggest(jsonArray, _query)
  jsonArray
end
create_variant(json_object, product) click to toggle source

creates a variant model

  • Args :

    • json_object -> the api response in json format

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

  • Returns :

# 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) click to toggle source

creates a variants result model

# 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) click to toggle source

this methods initiates the category manager

  • Args :

    • json_object -> the api response in json format

# 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) click to toggle source

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

# 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) click to toggle source

creates an initiate order model

# 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) click to toggle source

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

# 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) click to toggle source

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 :

# 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) click to toggle source

this methods updates the facet manager

  • Args :

    • json_object -> the api response in json format

    • query -> the query sent to the api

# 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