aboutyou-sdk 0.0.24 → 0.0.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/{aboutyou-sdk/CacheProvider/Dalli.rb → AboutYou/CacheProvider/dalli.rb} +18 -19
- data/lib/{aboutyou-sdk/CacheProvider/Redis.rb → AboutYou/CacheProvider/redis.rb} +19 -20
- data/lib/{aboutyou-sdk/Criteria/ProductFields.rb → AboutYou/Criteria/product_fields.rb} +25 -27
- data/lib/{aboutyou-sdk/Criteria/ProductSearchCriteria.rb → AboutYou/Criteria/product_search_criteria.rb} +128 -123
- data/lib/{aboutyou-sdk/Factory/DefaultModelFactory.rb → AboutYou/Factory/default_model_factory.rb} +267 -224
- data/lib/AboutYou/Model/Basket/abstract_basket_item.rb +57 -0
- data/lib/AboutYou/Model/Basket/basket_item.rb +95 -0
- data/lib/AboutYou/Model/Basket/basket_set.rb +199 -0
- data/lib/AboutYou/Model/Basket/basket_set_item.rb +54 -0
- data/lib/AboutYou/Model/Basket/basket_variant_item.rb +123 -0
- data/lib/{aboutyou-sdk/Model/CategoryManager/DefaultCategoryManager.rb → AboutYou/Model/CategoryManager/default_category_manager.rb} +80 -65
- data/lib/{aboutyou-sdk/Model/FacetManager/DefaultFacetManager.rb → AboutYou/Model/FacetManager/default_facet_manager.rb} +58 -50
- data/lib/{aboutyou-sdk/Model/ProductSearchResult/FacetCount.rb → AboutYou/Model/ProductSearchResult/facet_count.rb} +2 -3
- data/lib/AboutYou/Model/ProductSearchResult/facet_counts.rb +67 -0
- data/lib/{aboutyou-sdk/Model/ProductSearchResult/PriceRange.rb → AboutYou/Model/ProductSearchResult/price_range.rb} +17 -18
- data/lib/AboutYou/Model/ProductSearchResult/sale_counts.rb +79 -0
- data/lib/AboutYou/Model/ProductSearchResult/terms_count.rb +40 -0
- data/lib/{aboutyou-sdk/Model/AbstractModel.rb → AboutYou/Model/abstract_model.rb} +2 -3
- data/lib/{aboutyou-sdk/Model/App.rb → AboutYou/Model/app.rb} +15 -15
- data/lib/{aboutyou-sdk/Model/Autocomplete.rb → AboutYou/Model/autocomplete.rb} +25 -32
- data/lib/AboutYou/Model/basket.rb +235 -0
- data/lib/AboutYou/Model/categories_result.rb +24 -0
- data/lib/AboutYou/Model/category.rb +102 -0
- data/lib/AboutYou/Model/category_tree.rb +37 -0
- data/lib/AboutYou/Model/facet.rb +86 -0
- data/lib/AboutYou/Model/facet_group.rb +96 -0
- data/lib/AboutYou/Model/facet_group_set.rb +209 -0
- data/lib/AboutYou/Model/image.rb +102 -0
- data/lib/{aboutyou-sdk/Model/ImageSize.rb → AboutYou/Model/image_size.rb} +9 -5
- data/lib/AboutYou/Model/initiate_order.rb +53 -0
- data/lib/AboutYou/Model/order.rb +22 -0
- data/lib/AboutYou/Model/product.rb +353 -0
- data/lib/AboutYou/Model/product_search_result.rb +114 -0
- data/lib/AboutYou/Model/products_ean_result.rb +43 -0
- data/lib/AboutYou/Model/products_result.rb +56 -0
- data/lib/AboutYou/Model/result_error.rb +28 -0
- data/lib/AboutYou/Model/variant.rb +214 -0
- data/lib/AboutYou/Model/variants_result.rb +91 -0
- data/lib/AboutYou/client.rb +128 -0
- data/lib/{aboutyou-sdk/Constants.rb → AboutYou/constants.rb} +3 -5
- data/lib/AboutYou/query.rb +343 -0
- data/lib/AboutYou/query_builder.rb +530 -0
- data/lib/ay.rb +566 -0
- data/tests/Sinatra-test/Main.rb +3 -0
- data/tests/testAutocomplete.rb +5 -6
- data/tests/testCatFilter.rb +8 -9
- data/tests/testCatTree.rb +3 -3
- data/tests/testCategoryCaching.rb +4 -5
- data/tests/{testFacetCaching.rb → testDalliCache.rb} +4 -5
- data/tests/testFacetTypes.rb +5 -6
- data/tests/testFacets.rb +7 -8
- data/tests/testFetchFacet.rb +5 -6
- data/tests/testJavaScript.rb +3 -4
- data/tests/testProdCatLongestPath.rb +7 -8
- data/tests/testProductSearchResult.rb +6 -7
- data/tests/testProductsByEans.rb +6 -7
- data/tests/testProductsByIds.rb +6 -7
- data/tests/testRedisCache.rb +4 -5
- data/tests/testSuggest.rb +5 -6
- data/tests/testVariantsByIds.rb +7 -8
- metadata +46 -46
- data/lib/aboutyou-sdk.rb +0 -596
- data/lib/aboutyou-sdk/Client.rb +0 -134
- data/lib/aboutyou-sdk/Model/Basket.rb +0 -268
- data/lib/aboutyou-sdk/Model/Basket/AbstractBasketItem.rb +0 -76
- data/lib/aboutyou-sdk/Model/Basket/BasketItem.rb +0 -100
- data/lib/aboutyou-sdk/Model/Basket/BasketSet.rb +0 -192
- data/lib/aboutyou-sdk/Model/Basket/BasketSetItem.rb +0 -46
- data/lib/aboutyou-sdk/Model/Basket/BasketVariantItem.rb +0 -137
- data/lib/aboutyou-sdk/Model/CategoriesResult.rb +0 -15
- data/lib/aboutyou-sdk/Model/Category.rb +0 -126
- data/lib/aboutyou-sdk/Model/CategoryTree.rb +0 -35
- data/lib/aboutyou-sdk/Model/Facet.rb +0 -105
- data/lib/aboutyou-sdk/Model/FacetGroup.rb +0 -110
- data/lib/aboutyou-sdk/Model/FacetGroupSet.rb +0 -242
- data/lib/aboutyou-sdk/Model/Image.rb +0 -134
- data/lib/aboutyou-sdk/Model/InitiateOrder.rb +0 -50
- data/lib/aboutyou-sdk/Model/Order.rb +0 -17
- data/lib/aboutyou-sdk/Model/Product.rb +0 -335
- data/lib/aboutyou-sdk/Model/ProductSearchResult.rb +0 -125
- data/lib/aboutyou-sdk/Model/ProductSearchResult/FacetCounts.rb +0 -59
- data/lib/aboutyou-sdk/Model/ProductSearchResult/SaleCounts.rb +0 -71
- data/lib/aboutyou-sdk/Model/ProductSearchResult/TermsCount.rb +0 -36
- data/lib/aboutyou-sdk/Model/ProductsEansResult.rb +0 -47
- data/lib/aboutyou-sdk/Model/ProductsResult.rb +0 -52
- data/lib/aboutyou-sdk/Model/ResultError.rb +0 -24
- data/lib/aboutyou-sdk/Model/Variant.rb +0 -255
- data/lib/aboutyou-sdk/Model/VariantsResult.rb +0 -102
- data/lib/aboutyou-sdk/Query.rb +0 -350
- data/lib/aboutyou-sdk/QueryBuilder.rb +0 -499
@@ -0,0 +1,91 @@
|
|
1
|
+
module AboutYou
|
2
|
+
module SDK
|
3
|
+
module Model
|
4
|
+
###
|
5
|
+
#
|
6
|
+
# This class represents a variants result
|
7
|
+
#
|
8
|
+
###
|
9
|
+
class VariantsResult
|
10
|
+
# @var Variant[] */
|
11
|
+
attr_accessor :variants
|
12
|
+
# @var array */
|
13
|
+
attr_accessor :errors
|
14
|
+
|
15
|
+
###
|
16
|
+
#
|
17
|
+
# @param \stdClass $json_object
|
18
|
+
# @param \Collins\ShopApi\Model\ModelFactoryInterface $factory
|
19
|
+
# @param \Collins\ShopApi\Model\ProductSearchResult $productSearchResult
|
20
|
+
#
|
21
|
+
# @return static
|
22
|
+
###
|
23
|
+
def self.create(variants, errors = [], product_search_result)
|
24
|
+
variants_result = new
|
25
|
+
variants_result.variants = {}
|
26
|
+
variants_result.errors = errors
|
27
|
+
|
28
|
+
# no variant was found
|
29
|
+
return variants_result if
|
30
|
+
product_search_result == false || variants.count == 0
|
31
|
+
|
32
|
+
# get products from product-search
|
33
|
+
products = product_search_result.products
|
34
|
+
variants.each do |variant_id, product_id|
|
35
|
+
unless products[product_id]
|
36
|
+
# product was not delivered
|
37
|
+
variants_result.errors.push(variant_id)
|
38
|
+
next
|
39
|
+
end
|
40
|
+
|
41
|
+
product = products[product_id]
|
42
|
+
variant = product.variant_by_id(variant_id)
|
43
|
+
|
44
|
+
variants_result.variants[variant_id] = variant if
|
45
|
+
variant.is_a?(AboutYou::SDK::Model::Variant)
|
46
|
+
end
|
47
|
+
|
48
|
+
variants_result
|
49
|
+
end
|
50
|
+
|
51
|
+
###
|
52
|
+
# @return bool
|
53
|
+
###
|
54
|
+
def variants_found?
|
55
|
+
variants.count > 0
|
56
|
+
end
|
57
|
+
|
58
|
+
###
|
59
|
+
# @return bool
|
60
|
+
###
|
61
|
+
def variants_not_found?
|
62
|
+
errors.count > 0
|
63
|
+
end
|
64
|
+
|
65
|
+
###
|
66
|
+
# @return Variant[]
|
67
|
+
###
|
68
|
+
def variants_found
|
69
|
+
variants
|
70
|
+
end
|
71
|
+
|
72
|
+
###
|
73
|
+
# @param int $id
|
74
|
+
#
|
75
|
+
#
|
76
|
+
# @return Variant|null
|
77
|
+
###
|
78
|
+
def variant_by_id(id)
|
79
|
+
variants[id] ? variants[id] : nil
|
80
|
+
end
|
81
|
+
|
82
|
+
###
|
83
|
+
# @return int[]
|
84
|
+
###
|
85
|
+
def variants_not_found
|
86
|
+
errors
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
module AboutYou
|
2
|
+
module SDK
|
3
|
+
###
|
4
|
+
# This Class manages the API-Calls
|
5
|
+
#
|
6
|
+
# Author:: Collins GmbH & Co KG
|
7
|
+
###
|
8
|
+
class Client
|
9
|
+
# url for staging-api-requests
|
10
|
+
API_ENDPOINT_STAGE = 'http://shop-api.staging.aboutyou.de/api'
|
11
|
+
# url for sandbox-api-requests
|
12
|
+
API_ENDPOINT_SANDBOX = 'http://shop-api.sandbox.aboutyou.de/api'
|
13
|
+
# url for live-api-requests
|
14
|
+
API_ENDPOINT_LIVE = 'https://shop-api.aboutyou.de/api'
|
15
|
+
|
16
|
+
# the app id of the app which should be represented by an instance of AY
|
17
|
+
attr_accessor :app_id
|
18
|
+
# the authentifaction token from dev-center for the app id
|
19
|
+
attr_accessor :app_password
|
20
|
+
# the url used for api calls
|
21
|
+
attr_accessor :api_endpoint
|
22
|
+
# the page id of the current page visited by an user
|
23
|
+
attr_accessor :page_id
|
24
|
+
|
25
|
+
###
|
26
|
+
# the Constructor for the Client class
|
27
|
+
#
|
28
|
+
# * *Args* :
|
29
|
+
# - +app_id+ -> The App-Id of the App
|
30
|
+
# - +app_password+ -> The Auth-Token of the App
|
31
|
+
# - +api_endpoint+ -> Can be either live or staging
|
32
|
+
# - +logger+ -> Logger-Template
|
33
|
+
#
|
34
|
+
# * *Returns* :
|
35
|
+
# - Instance of AboutYou::SDK::Client
|
36
|
+
###
|
37
|
+
def initialize(
|
38
|
+
app_id,
|
39
|
+
app_password,
|
40
|
+
_api_endpoint = 'stage',
|
41
|
+
_logger = nil
|
42
|
+
)
|
43
|
+
self.app_id = app_id
|
44
|
+
self.app_password = app_password
|
45
|
+
end
|
46
|
+
|
47
|
+
###
|
48
|
+
# Setter for app-credentials
|
49
|
+
#
|
50
|
+
# * *Args* :
|
51
|
+
# - +app_id+ -> The App-Id of the App
|
52
|
+
# - +app_password+ -> The Auth-Token of the App
|
53
|
+
###
|
54
|
+
def app_credentials=(app_id, app_password)
|
55
|
+
self.app_id = app_id
|
56
|
+
self.app_password = app_password
|
57
|
+
end
|
58
|
+
|
59
|
+
###
|
60
|
+
# Setter for the api endpoint
|
61
|
+
#
|
62
|
+
# * *Args* :
|
63
|
+
# - +api_endpoint+ -> Can be either live or staging
|
64
|
+
###
|
65
|
+
def api_endpoint=(api_endpoint)
|
66
|
+
case
|
67
|
+
when api_endpoint == 'stage'
|
68
|
+
self.api_endpoint = API_ENDPOINT_STAGE
|
69
|
+
when api_endpoint == 'sandbox'
|
70
|
+
self.api_endpoint = API_ENDPOINT_SANDBOX
|
71
|
+
when api_endpoint == 'live'
|
72
|
+
self.api_endpoint = API_ENDPOINT_LIVE
|
73
|
+
else
|
74
|
+
self.api_endpoint = api_endpoint
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
###
|
79
|
+
# Builds a JSON string representing the request-data
|
80
|
+
# Executes the API request
|
81
|
+
# builds a JSON string representing the response-data
|
82
|
+
#
|
83
|
+
# * *Args* :
|
84
|
+
# - +body+ -> the body of the api-call, containing all request data
|
85
|
+
#
|
86
|
+
# * *Returns* :
|
87
|
+
# - Json-String containing the response-body
|
88
|
+
###
|
89
|
+
def request(body)
|
90
|
+
if page_id
|
91
|
+
request = {
|
92
|
+
body: body,
|
93
|
+
basic_auth: {
|
94
|
+
username: app_id,
|
95
|
+
password: app_password
|
96
|
+
},
|
97
|
+
headers: {
|
98
|
+
'Content-Type' => 'application/json',
|
99
|
+
'Accept-Encoding' => 'gzip,deflate',
|
100
|
+
'X-Page-ID' => page_id
|
101
|
+
}
|
102
|
+
}
|
103
|
+
else
|
104
|
+
request = {
|
105
|
+
body: body,
|
106
|
+
basic_auth: {
|
107
|
+
username: app_id,
|
108
|
+
password: app_password
|
109
|
+
},
|
110
|
+
headers: {
|
111
|
+
'Content-Type' => 'application/json',
|
112
|
+
'Accept-Encoding' => 'gzip,deflate'
|
113
|
+
}
|
114
|
+
}
|
115
|
+
end
|
116
|
+
|
117
|
+
response = HTTParty.post('https://shop-api.aboutyou.de/api', request)
|
118
|
+
code = response.code
|
119
|
+
|
120
|
+
fail String(response.code) unless
|
121
|
+
code >= 200 && code < 300 || code == 304
|
122
|
+
fail 'result is not array' unless JSON.parse(response.body).is_a?(Array)
|
123
|
+
|
124
|
+
JSON.parse(response.body)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -6,9 +6,7 @@ module AboutYou
|
|
6
6
|
# Author:: Collins GmbH & Co KG
|
7
7
|
###
|
8
8
|
module Constants
|
9
|
-
|
10
|
-
# Version of the SDK
|
11
|
-
SDK_VERSION = 0.1
|
9
|
+
|
12
10
|
# Group Id for Brands
|
13
11
|
FACET_BRAND = 0
|
14
12
|
# Group Id for Color
|
@@ -55,7 +53,7 @@ module AboutYou
|
|
55
53
|
FACET_QUANTITY_PER_PACK = 263
|
56
54
|
# Group Id for Season Code
|
57
55
|
FACET_SEASON_CODE = 289
|
58
|
-
|
56
|
+
|
59
57
|
# Sort-argument passed to api for relevance
|
60
58
|
SORT_RELEVANCE = 'relevance'
|
61
59
|
# Sort-argument passed to api for last updated
|
@@ -80,4 +78,4 @@ module AboutYou
|
|
80
78
|
API_ENVIRONMENT_LIVE = 'live'
|
81
79
|
end
|
82
80
|
end
|
83
|
-
end
|
81
|
+
end
|
@@ -0,0 +1,343 @@
|
|
1
|
+
module AboutYou
|
2
|
+
module SDK
|
3
|
+
###
|
4
|
+
# The Query class coordinates the building, executing and parsing of one or multiple API-calls
|
5
|
+
#
|
6
|
+
# Author:: Collins GmbH & Co KG
|
7
|
+
###
|
8
|
+
class Query
|
9
|
+
# QueryBuilder is a helper class containing most of the logix behind
|
10
|
+
# building the api-request
|
11
|
+
include AboutYou::SDK::QueryBuilder
|
12
|
+
|
13
|
+
# used for checking whether a query contains a category-request or not
|
14
|
+
QUERY_TREE = 'category_tree'
|
15
|
+
# used for checking whether a query contains a facets-request or not
|
16
|
+
QUERY_FACETS = 'facets'
|
17
|
+
|
18
|
+
# The Api-Client which performs the Api-Calls
|
19
|
+
attr_accessor :client
|
20
|
+
# the query built by the app itself
|
21
|
+
attr_accessor :query
|
22
|
+
# a Hash containing the mapping for
|
23
|
+
# api_call_name => method_name_in_model_factory
|
24
|
+
attr_accessor :mapping
|
25
|
+
# the model factory which builds the models from the api-response
|
26
|
+
attr_accessor :factory
|
27
|
+
# a helper-query used for fetching categories and/or facets if needed
|
28
|
+
attr_accessor :ghost_query
|
29
|
+
# the actual query send to the api containing query and ghost-query
|
30
|
+
attr_accessor :all_query
|
31
|
+
|
32
|
+
###
|
33
|
+
# the Constructor for the Query class
|
34
|
+
#
|
35
|
+
# * *Args* :
|
36
|
+
# - +client+ -> an instance of AboutYou::SDK::Client
|
37
|
+
# - +factory+ -> an instance of AboutYou::SDK::Factory::DefaultModelFactory
|
38
|
+
#
|
39
|
+
# * *Returns* :
|
40
|
+
# - Instance of AboutYou::SDK::Query
|
41
|
+
###
|
42
|
+
def initialize(client, factory)
|
43
|
+
self.client = client
|
44
|
+
self.query = []
|
45
|
+
self.ghost_query = []
|
46
|
+
self.all_query = []
|
47
|
+
self.factory = factory
|
48
|
+
self.mapping = {
|
49
|
+
'autocompletion' => 'create_autocomplete',
|
50
|
+
'basket' => 'create_basket',
|
51
|
+
'category' => 'create_categories_result',
|
52
|
+
'category_tree' => 'create_category_tree',
|
53
|
+
'facets' => 'create_facets_list',
|
54
|
+
'facet' => 'create_facet_list',
|
55
|
+
'facet_types' => 'create_facet_types',
|
56
|
+
'products' => 'create_products_result',
|
57
|
+
'products_eans' => 'create_products_ean_result',
|
58
|
+
'product_search' => 'create_product_search_result',
|
59
|
+
'suggest' => 'create_suggest',
|
60
|
+
'get_order' => 'create_order',
|
61
|
+
'initiate_order' => 'initiate_order',
|
62
|
+
'child_apps' => 'create_child_apps',
|
63
|
+
'live_variant' => 'create_variants_result'
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
###
|
68
|
+
# wrapper-method for fetch_autocomplete which coordinates Category- and Facet-Manager
|
69
|
+
#
|
70
|
+
# * *Args* :
|
71
|
+
# - +searchword+ -> a String containing the word to search completitions for
|
72
|
+
# - +limit+ -> Maximum number of results [optional]
|
73
|
+
# - +types+ -> Array of types to search for [optional]
|
74
|
+
#
|
75
|
+
# * *Returns* :
|
76
|
+
# - Instance of AboutYou::SDK::Query
|
77
|
+
###
|
78
|
+
def fetch_autocomplete(searchword, limit = nil, types = nil)
|
79
|
+
super(searchword, limit, types)
|
80
|
+
|
81
|
+
require_category_tree
|
82
|
+
require_facets
|
83
|
+
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
###
|
88
|
+
# wrapper-method for fetch_basket which coordinates Category- and Facet-Manager
|
89
|
+
#
|
90
|
+
# * *Args* :
|
91
|
+
# - +session_id+ -> a String containing the session id of an user
|
92
|
+
#
|
93
|
+
# * *Returns* :
|
94
|
+
# - Instance of AboutYou::SDK::Query
|
95
|
+
###
|
96
|
+
def fetch_basket(session_id)
|
97
|
+
super(session_id)
|
98
|
+
|
99
|
+
require_category_tree
|
100
|
+
require_facets
|
101
|
+
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
def fetch_facets(group_ids = [])
|
106
|
+
super(group_ids)
|
107
|
+
|
108
|
+
self
|
109
|
+
end
|
110
|
+
|
111
|
+
###
|
112
|
+
# wrapper-method for fetch_products_by_ids which coordinates Category- and Facet-Manager
|
113
|
+
#
|
114
|
+
# * *Args* :
|
115
|
+
# - +ids+ -> Either a single id or an Array of ids which should be fetched
|
116
|
+
# - +fields+ -> Additional product fields which should be fetched for each product [optional]
|
117
|
+
#
|
118
|
+
# * *Returns* :
|
119
|
+
# - Instance of AboutYou::SDK::Query
|
120
|
+
###
|
121
|
+
def fetch_products_by_ids(ids, fields = [])
|
122
|
+
super(ids, fields)
|
123
|
+
|
124
|
+
require_category_tree if
|
125
|
+
AboutYou::SDK::Criteria::ProductFields.requires_categories(fields)
|
126
|
+
require_facets if
|
127
|
+
AboutYou::SDK::Criteria::ProductFields.requires_facets(fields)
|
128
|
+
|
129
|
+
self
|
130
|
+
end
|
131
|
+
|
132
|
+
###
|
133
|
+
# wrapper-method for fetch_products_by_eans which coordinates Category- and Facet-Manager
|
134
|
+
#
|
135
|
+
# * *Args* :
|
136
|
+
# - +eans+ -> Either a single ean or an Array of eans which should be fetched
|
137
|
+
# - +fields+ -> Additional product fields which should be fetched for each product [optional]
|
138
|
+
#
|
139
|
+
# * *Returns* :
|
140
|
+
# - Instance of AboutYou::SDK::Query
|
141
|
+
###
|
142
|
+
def fetch_products_by_eans(eans, fields = [])
|
143
|
+
super(eans, fields)
|
144
|
+
|
145
|
+
require_category_tree if
|
146
|
+
AboutYou::SDK::Criteria::ProductFields.requires_categories(fields)
|
147
|
+
require_facets if
|
148
|
+
AboutYou::SDK::Criteria::ProductFields.requires_facets(fields)
|
149
|
+
|
150
|
+
self
|
151
|
+
end
|
152
|
+
|
153
|
+
###
|
154
|
+
# wrapper-method for fetch_product_search which coordinates Category- and Facet-Manager
|
155
|
+
#
|
156
|
+
# * *Args* :
|
157
|
+
# - +criteria+ -> Hash containing one or multiple search terms
|
158
|
+
#
|
159
|
+
# * *Returns* :
|
160
|
+
# - Instance of AboutYou::SDK::Query
|
161
|
+
###
|
162
|
+
def fetch_product_search(criteria)
|
163
|
+
super(criteria)
|
164
|
+
|
165
|
+
require_category_tree if criteria.requires_categories
|
166
|
+
require_facets if criteria.requires_facets
|
167
|
+
|
168
|
+
self
|
169
|
+
end
|
170
|
+
|
171
|
+
###
|
172
|
+
# this method checks whether it is neccessary for the query to get the category-tree from the api
|
173
|
+
#
|
174
|
+
# * *Args* :
|
175
|
+
# - +fetchForced+ -> determines whether the requirance of the category tree is forced
|
176
|
+
#
|
177
|
+
# * *Returns* :
|
178
|
+
# - Instance of AboutYou::SDK::Query
|
179
|
+
###
|
180
|
+
def require_category_tree(fetch_forced = false)
|
181
|
+
return self unless
|
182
|
+
|
183
|
+
fetch_forced || factory.category_manager.empty?
|
184
|
+
|
185
|
+
ghost_query.push(
|
186
|
+
QUERY_TREE => {
|
187
|
+
'category_tree' => {
|
188
|
+
'version' => '2'
|
189
|
+
}
|
190
|
+
}
|
191
|
+
)
|
192
|
+
|
193
|
+
self
|
194
|
+
end
|
195
|
+
|
196
|
+
###
|
197
|
+
# this method checks whether it is neccessary for the query to get the facets from the api
|
198
|
+
#
|
199
|
+
# * *Args* :
|
200
|
+
# - +fetchForced+ -> determines whether the requirance of the facets is forced
|
201
|
+
#
|
202
|
+
# * *Returns* :
|
203
|
+
# - Instance of AboutYou::SDK::Query
|
204
|
+
###
|
205
|
+
def require_facets(fetch_forced = false)
|
206
|
+
return self unless fetch_forced || factory.facet_manager.empty?
|
207
|
+
|
208
|
+
ghost_query.push(
|
209
|
+
QUERY_FACETS => {
|
210
|
+
'facets' => {}
|
211
|
+
}
|
212
|
+
)
|
213
|
+
|
214
|
+
self
|
215
|
+
end
|
216
|
+
|
217
|
+
###
|
218
|
+
# the methods builds the complete query string which represents the body of the api call
|
219
|
+
#
|
220
|
+
# * *Returns* :
|
221
|
+
# - JSON-String containing all queries which need to be executed
|
222
|
+
###
|
223
|
+
def query_string
|
224
|
+
result = []
|
225
|
+
|
226
|
+
ghost_query.each do |ghost_query|
|
227
|
+
result.push(
|
228
|
+
ghost_query[ghost_query.keys[0]]
|
229
|
+
) if ghost_query.is_a?(Hash)
|
230
|
+
end
|
231
|
+
|
232
|
+
self.all_query = result + query
|
233
|
+
|
234
|
+
(result + query).to_json
|
235
|
+
end
|
236
|
+
|
237
|
+
###
|
238
|
+
# requests all of the queries and returns the parsed api-response
|
239
|
+
#
|
240
|
+
# * *Returns* :
|
241
|
+
# - an Array containing all of the models build with the data of the api
|
242
|
+
###
|
243
|
+
def execute
|
244
|
+
return [] if query.empty? && ghost_query.empty?
|
245
|
+
parse_result(client.request(query_string), query.count > 1)
|
246
|
+
end
|
247
|
+
|
248
|
+
###
|
249
|
+
# requests all of the queries and returns only the first parsed api-response
|
250
|
+
#
|
251
|
+
# * *Returns* :
|
252
|
+
# - the first model build with the data of the api
|
253
|
+
###
|
254
|
+
def execute_single
|
255
|
+
execute[-1]
|
256
|
+
end
|
257
|
+
|
258
|
+
###
|
259
|
+
# this method checks whether the api delievered a valid json-response or not
|
260
|
+
#
|
261
|
+
# * *Args* :
|
262
|
+
# - +jsonResponse+ -> the plain json received from the api
|
263
|
+
###
|
264
|
+
def check_response(json_response)
|
265
|
+
fail 'UnexpectedResultException!' if
|
266
|
+
json_response == false ||
|
267
|
+
!json_response.is_a?(Array) ||
|
268
|
+
json_response.count != all_query.count
|
269
|
+
|
270
|
+
(0..json_response.count - 1).each do |index|
|
271
|
+
current_query = all_query[index]
|
272
|
+
response_key = json_response[index].keys[0]
|
273
|
+
query_key = current_query.keys[0]
|
274
|
+
|
275
|
+
fail 'UnexpectedResultException! result ' + String(query_key) +
|
276
|
+
' expected, but ' + String(response_key) + ' given on position ' +
|
277
|
+
String(index) + ' - query: ' + current_query.to_json if
|
278
|
+
response_key != query_key
|
279
|
+
|
280
|
+
fail 'UnexpectedResultException! ' + String(response_key) +
|
281
|
+
' is unknown result' unless mapping.key? response_key
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
###
|
286
|
+
# parses the plain json result from the api and calls the respective modelfactory methods
|
287
|
+
# for building the models for the json resposne
|
288
|
+
#
|
289
|
+
# * *Args* :
|
290
|
+
# - +jsonResponse+ -> the plain json received from the api
|
291
|
+
# - +isMultiRequest+ -> boolean which determines whether the request has more then one query or not [optional]
|
292
|
+
#
|
293
|
+
# * *Returns* :
|
294
|
+
# - an Array containing all models build from the modelfactory
|
295
|
+
###
|
296
|
+
def parse_result(json_response, is_multi_request = true)
|
297
|
+
check_response(json_response)
|
298
|
+
results = []
|
299
|
+
query_ids = []
|
300
|
+
|
301
|
+
all_query.each do |query|
|
302
|
+
query_ids.push(query.keys[0])
|
303
|
+
end
|
304
|
+
|
305
|
+
json_response.each_with_index do |response_object, index|
|
306
|
+
current_query = all_query[index]
|
307
|
+
result_key = response_object.keys[0]
|
308
|
+
json_object = response_object[result_key]
|
309
|
+
query_key = current_query.keys[0]
|
310
|
+
factory = self.factory
|
311
|
+
if json_object.is_a?(Hash) && json_object['error_code']
|
312
|
+
result = factory.pre_handle_error(
|
313
|
+
json_object,
|
314
|
+
result_key,
|
315
|
+
is_multi_request
|
316
|
+
)
|
317
|
+
|
318
|
+
if result != false
|
319
|
+
results.push(
|
320
|
+
result_key => result
|
321
|
+
)
|
322
|
+
next
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
query = current_query[query_key]
|
327
|
+
query_id = query_ids[index]
|
328
|
+
if query_id == QUERY_FACETS
|
329
|
+
factory.update_facet_manager(json_object, query)
|
330
|
+
elsif query_id == QUERY_TREE
|
331
|
+
factory.initialize_category_manager(json_object)
|
332
|
+
else
|
333
|
+
method = mapping[result_key]
|
334
|
+
result = factory.send(method, json_object, query)
|
335
|
+
results.push(result)
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
results
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|