module AboutYou::SDK::QueryBuilder

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

Public Instance Methods

add_items_to_basket(session_id, items) click to toggle source

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 :

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

builds the basket-query

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

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 :

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

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

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

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 :

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

builds the basket-query

  • Args :

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

  • Returns :

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

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 :

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

builds the category_tree-query

# File lib/AboutYou/query_builder.rb, line 283
def fetch_category_tree
  query.push(
    'category_tree' => {
      'version' => '2'
    }
  )

  self
end
fetch_child_apps() click to toggle source

builds the child_apps-query

# File lib/AboutYou/query_builder.rb, line 541
def fetch_child_apps
  query.push(
    'child_apps' => nil
  )

  self
end
fetch_facet(params) click to toggle source

builds the facet-query

  • Args :

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

  • Fails :

    • if params is not set

  • Returns :

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

builds the facet_types-query

# File lib/AboutYou/query_builder.rb, line 477
def fetch_facet_types
  query.push(
    'facet_types' => nil
  )

  self
end
fetch_facets(group_ids = []) click to toggle source

builds the facets-query

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

builds the live_variant-query

  • Args :

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

  • Returns :

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

builds the get_order-query

  • Args :

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

  • Returns :

# File lib/AboutYou/query_builder.rb, line 370
def fetch_order(order_id)
  query.push(
    'get_order' => {
      'order_id' => order_id
    }
  )

  self
end
fetch_products_by_eans(eans, fields = []) click to toggle source

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 :

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

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 :

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

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 :

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

builds the suggest-query

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

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 :

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

turns all queries built in here into a json string

  • Returns :

    • Json string containing all queries built in here

# File lib/AboutYou/query_builder.rb, line 555
def query_string
  query.to_json
end
remove_from_basket(session_id, item_ids) click to toggle source

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 :

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

builds the basket-query

# 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