suggestgrid 0.1.17.pre.SNAPSHOT → 0.1.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/suggest_grid/api_helper.rb +2 -2
  3. data/lib/suggest_grid/configuration.rb +2 -1
  4. data/lib/suggest_grid/controllers/action_controller.rb +77 -99
  5. data/lib/suggest_grid/controllers/base_controller.rb +27 -2
  6. data/lib/suggest_grid/controllers/metadata_controller.rb +259 -209
  7. data/lib/suggest_grid/controllers/recommendation_controller.rb +32 -44
  8. data/lib/suggest_grid/controllers/similarity_controller.rb +32 -44
  9. data/lib/suggest_grid/controllers/type_controller.rb +74 -104
  10. data/lib/suggest_grid/exceptions/api_exception.rb +1 -1
  11. data/lib/suggest_grid/exceptions/delete_error_response_exception.rb +52 -0
  12. data/lib/suggest_grid/exceptions/detailed_error_response_exception.rb +3 -3
  13. data/lib/suggest_grid/exceptions/error_response_exception.rb +3 -3
  14. data/lib/suggest_grid/exceptions/limit_exceeded_error_response_exception.rb +3 -3
  15. data/lib/suggest_grid/http/auth/basic_auth.rb +17 -0
  16. data/lib/suggest_grid/http/faraday_client.rb +43 -0
  17. data/lib/suggest_grid/http/http_call_back.rb +2 -2
  18. data/lib/suggest_grid/http/http_client.rb +13 -43
  19. data/lib/suggest_grid/http/http_request.rb +24 -8
  20. data/lib/suggest_grid/models/actions_response.rb +58 -0
  21. data/lib/suggest_grid/models/{schema_error_response.rb → bulk_post_error.rb} +4 -4
  22. data/lib/suggest_grid/models/bulk_post_response.rb +49 -0
  23. data/lib/suggest_grid/models/get_recommended_items_body.rb +14 -5
  24. data/lib/suggest_grid/models/get_recommended_users_body.rb +11 -2
  25. data/lib/suggest_grid/models/get_similar_items_body.rb +14 -5
  26. data/lib/suggest_grid/models/get_similar_users_body.rb +11 -2
  27. data/lib/suggest_grid/models/items_response.rb +10 -1
  28. data/lib/suggest_grid/models/users_response.rb +10 -1
  29. data/lib/suggest_grid.rb +53 -53
  30. data/spec/swagger.yaml +185 -87
  31. metadata +41 -15
  32. data/lib/suggest_grid/exceptions/bulk_schema_error_response_exception.rb +0 -37
  33. data/lib/suggest_grid/http/unirest_client.rb +0 -41
  34. data/lib/suggest_grid/models/count_response.rb +0 -35
  35. data/lib/suggest_grid/models/delete_error_response.rb +0 -80
  36. data/lib/suggest_grid/models/metadata_information_response.rb +0 -35
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ada01f923b1b9dd491c6a8a6666b7ffe8f709b8c
4
- data.tar.gz: 72fe56da2934b0387707159761dce05a084ca608
3
+ metadata.gz: 4d16c4bda91e756fab948c163a5eca7f9be6b13b
4
+ data.tar.gz: 5b7380e60f648cb6cb4ba6490a651be6110966f2
5
5
  SHA512:
6
- metadata.gz: 19d22bb73bc52bb9d7a9d9bda5a55088a30d16264da5c993e55040e1359fbfce1ff860d940247385689fe82098312dfaecf70234bcd4ee0607e52106efc0bdc1
7
- data.tar.gz: 9970d65ae9a06d043835180da9da72e4c6ec578a381cb549cc22d9f2232e732f12e1f689936acb15a76fe2f50de3acb093aff564e4896a741f32eef696fec8fa
6
+ metadata.gz: 9b823a73f7c09feab64633bda1001e68fcfe7c95923a39bf7753b2402124a88673d20ac01e29c9c775a13a72aaa4d1e75f381e988e6cf7a0f3b4a8b650359f01
7
+ data.tar.gz: 2ddf6fe0bdc8da3b348c94fe4845ff188ebbaad1e4af15f7019bae5a781321e69e94ba66aba05b47128fde2c444a1a6a1af45c884e724fcb148b512467653bbc
@@ -4,7 +4,7 @@ module SuggestGrid
4
4
  class APIHelper
5
5
  # Replaces template parameters in the given url
6
6
  # @param [String] The query string builder to replace the template parameters
7
- # @param [Array] The parameters to replace in the url
7
+ # @param [Hash] The parameters to replace in the url
8
8
  def self.append_url_with_template_parameters(query_builder, parameters)
9
9
  # perform parameter validation
10
10
  raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
@@ -35,7 +35,7 @@ module SuggestGrid
35
35
 
36
36
  # Appends the given set of parameters to the given query string
37
37
  # @param [String] The query string builder to replace the template parameters
38
- # @param [Array] The parameters to append
38
+ # @param [Hash] The parameters to append
39
39
  def self.append_url_with_query_parameters(query_builder, parameters)
40
40
  # perform parameter validation
41
41
  raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
@@ -11,11 +11,12 @@ module SuggestGrid
11
11
  # The password to use with basic authentication
12
12
  @basic_auth_password = 'TODO: Replace'
13
13
 
14
- # create the getters and setters
14
+ # The attribute accessors for public properties
15
15
  class << self
16
16
  attr_accessor :base_uri
17
17
  attr_accessor :basic_auth_user_name
18
18
  attr_accessor :basic_auth_password
19
19
  end
20
+
20
21
  end
21
22
  end
@@ -12,12 +12,17 @@ module SuggestGrid
12
12
  # @param [String] type Optional parameter: The type of the actions.
13
13
  # @param [String] user_id Optional parameter: The user id of the actions.
14
14
  # @param [String] item_id Optional parameter: The item id of the actions.
15
- # @param [String] older_than Optional parameter: Delete all actions of a type older than the given timestamp or time. Valid times are 1s, 1m, 1h, 1d, 1M, 1y, or unix timestamp (like 1443798195).
16
- # @return CountResponse response from the API call
15
+ # @param [String] older_than Optional parameter: Maxium timestamp of the actions. Valid times are 1s, 1m, 1h, 1d, 1M, 1y, or unix timestamp (like 1443798195).
16
+ # @param [Integer] size Optional parameter: The number of the users response. Defaults to 10. Must be between 1 and 10.000 inclusive. This parameter must be string represetation of an integer like "1".
17
+ # @param [Integer] from Optional parameter: The number of users to be skipped for response. Defaults to 0. Must be bigger than or equal to 0. This parameter must be string represetation of an integer like "1".
18
+ # @return ActionsResponse response from the API call
17
19
  def get_actions(type = nil,
18
20
  user_id = nil,
19
21
  item_id = nil,
20
- older_than = nil)
22
+ older_than = nil,
23
+ size = nil,
24
+ from = nil)
25
+
21
26
  # the base uri for api requests
22
27
  _query_builder = Configuration.base_uri.dup
23
28
 
@@ -29,7 +34,9 @@ module SuggestGrid
29
34
  'type' => type,
30
35
  'user_id' => user_id,
31
36
  'item_id' => item_id,
32
- 'older_than' => older_than
37
+ 'older_than' => older_than,
38
+ 'size' => size,
39
+ 'from' => from
33
40
  }
34
41
 
35
42
  # validate and preprocess url
@@ -37,46 +44,38 @@ module SuggestGrid
37
44
 
38
45
  # prepare headers
39
46
  _headers = {
40
- 'user-agent' => 'SUGGESTGRID',
41
47
  'accept' => 'application/json'
42
48
  }
43
49
 
44
- # Create the HttpRequest object for the call
45
- _request = @http_client.get _query_url, headers: _headers, username: Configuration.basic_auth_user_name, password: Configuration.basic_auth_password
46
-
47
- # Call the on_before_request callback
48
- @http_call_back.on_before_request(_request) if @http_call_back
49
-
50
- # Invoke the API call and get the response
51
- _response = @http_client.execute_as_string(_request)
50
+ # create the HttpRequest object for the call
51
+ _request = @http_client.get _query_url, headers: _headers
52
52
 
53
- # Wrap the request and response in an HttpContext object
54
- _context = HttpContext.new(_request, _response)
53
+ # apply authentication
54
+ BasicAuth.apply(_request)
55
55
 
56
- # Call the on_after_response callback
57
- @http_call_back.on_after_response(_context) if @http_call_back
56
+ # execute the request
57
+ _context = execute_request(_request)
58
58
 
59
- # Endpoint error handling using HTTP status codes.
60
- if _response.status_code == 400
61
- raise ErrorResponseException.new '400 - Required `user_id` or `item_id` parameters are missing from the request body.', _context
62
- elsif _response.status_code == 429
59
+ # endpoint error handling using HTTP status codes.
60
+ if _context.response.status_code == 429
63
61
  raise ErrorResponseException.new '429 - Too many requests.', _context
64
- elsif _response.status_code == 500
62
+ elsif _context.response.status_code == 500
65
63
  raise APIException.new '500 - Unexpected internal error.', _context
66
64
  end
67
65
 
68
- # Global error handling using HTTP status codes.
66
+ # global error handling using HTTP status codes.
69
67
  validate_response(_context)
70
68
 
71
- # Return appropriate response type
72
- decoded = APIHelper.json_deserialize(_response.raw_body)
73
- return CountResponse.from_hash(decoded)
69
+ # return appropriate response type
70
+ decoded = APIHelper.json_deserialize(_context.response.raw_body)
71
+ return ActionsResponse.from_hash(decoded)
74
72
  end
75
73
 
76
74
  # Post an Action
77
75
  # @param [Action] action Required parameter: The action to be posted.
78
76
  # @return MessageResponse response from the API call
79
77
  def post_action(action)
78
+
80
79
  # the base uri for api requests
81
80
  _query_builder = Configuration.base_uri.dup
82
81
 
@@ -88,44 +87,37 @@ module SuggestGrid
88
87
 
89
88
  # prepare headers
90
89
  _headers = {
91
- 'user-agent' => 'SUGGESTGRID',
92
90
  'accept' => 'application/json',
93
91
  'content-type' => 'application/json; charset=utf-8'
94
92
  }
95
93
 
96
- # Create the HttpRequest object for the call
97
- _request = @http_client.post _query_url, headers: _headers, parameters: action.to_json, username: Configuration.basic_auth_user_name, password: Configuration.basic_auth_password
98
-
99
- # Call the on_before_request callback
100
- @http_call_back.on_before_request(_request) if @http_call_back
94
+ # create the HttpRequest object for the call
95
+ _request = @http_client.post _query_url, headers: _headers, parameters: action.to_json
101
96
 
102
- # Invoke the API call and get the response
103
- _response = @http_client.execute_as_string(_request)
97
+ # apply authentication
98
+ BasicAuth.apply(_request)
104
99
 
105
- # Wrap the request and response in an HttpContext object
106
- _context = HttpContext.new(_request, _response)
100
+ # execute the request
101
+ _context = execute_request(_request)
107
102
 
108
- # Call the on_after_response callback
109
- @http_call_back.on_after_response(_context) if @http_call_back
110
-
111
- # Endpoint error handling using HTTP status codes.
112
- if _response.status_code == 400
103
+ # endpoint error handling using HTTP status codes.
104
+ if _context.response.status_code == 400
113
105
  raise ErrorResponseException.new '400 - Required `user_id` or `item_id` parameters are missing from the request body.', _context
114
- elsif _response.status_code == 402
106
+ elsif _context.response.status_code == 402
115
107
  raise ErrorResponseException.new '402 - Action limit exceeded.', _context
116
- elsif _response.status_code == 404
108
+ elsif _context.response.status_code == 404
117
109
  raise ErrorResponseException.new '404 - Type does not exists.', _context
118
- elsif _response.status_code == 429
110
+ elsif _context.response.status_code == 429
119
111
  raise ErrorResponseException.new '429 - Too many requests.', _context
120
- elsif _response.status_code == 500
112
+ elsif _context.response.status_code == 500
121
113
  raise APIException.new '500 - Unexpected internal error.', _context
122
114
  end
123
115
 
124
- # Global error handling using HTTP status codes.
116
+ # global error handling using HTTP status codes.
125
117
  validate_response(_context)
126
118
 
127
- # Return appropriate response type
128
- decoded = APIHelper.json_deserialize(_response.raw_body)
119
+ # return appropriate response type
120
+ decoded = APIHelper.json_deserialize(_context.response.raw_body)
129
121
  return MessageResponse.from_hash(decoded)
130
122
  end
131
123
 
@@ -134,11 +126,12 @@ module SuggestGrid
134
126
  # @param [String] user_id Optional parameter: The user id of the actions.
135
127
  # @param [String] item_id Optional parameter: The item id of the actions.
136
128
  # @param [String] older_than Optional parameter: Delete all actions of a type older than the given timestamp or time. Valid times are 1s, 1m, 1h, 1d, 1M, 1y, or unix timestamp (like 1443798195).
137
- # @return MessageResponse response from the API call
129
+ # @return DeleteSuccessResponse response from the API call
138
130
  def delete_actions(type = nil,
139
131
  user_id = nil,
140
132
  item_id = nil,
141
133
  older_than = nil)
134
+
142
135
  # the base uri for api requests
143
136
  _query_builder = Configuration.base_uri.dup
144
137
 
@@ -158,56 +151,50 @@ module SuggestGrid
158
151
 
159
152
  # prepare headers
160
153
  _headers = {
161
- 'user-agent' => 'SUGGESTGRID',
162
154
  'accept' => 'application/json'
163
155
  }
164
156
 
165
- # Create the HttpRequest object for the call
166
- _request = @http_client.delete _query_url, headers: _headers, username: Configuration.basic_auth_user_name, password: Configuration.basic_auth_password
167
-
168
- # Call the on_before_request callback
169
- @http_call_back.on_before_request(_request) if @http_call_back
170
-
171
- # Invoke the API call and get the response
172
- _response = @http_client.execute_as_string(_request)
157
+ # create the HttpRequest object for the call
158
+ _request = @http_client.delete _query_url, headers: _headers
173
159
 
174
- # Wrap the request and response in an HttpContext object
175
- _context = HttpContext.new(_request, _response)
160
+ # apply authentication
161
+ BasicAuth.apply(_request)
176
162
 
177
- # Call the on_after_response callback
178
- @http_call_back.on_after_response(_context) if @http_call_back
163
+ # execute the request
164
+ _context = execute_request(_request)
179
165
 
180
- # Endpoint error handling using HTTP status codes.
181
- if _response.status_code == 400
166
+ # endpoint error handling using HTTP status codes.
167
+ if _context.response.status_code == 400
182
168
  raise ErrorResponseException.new '400 - Required `user_id` or `item_id` parameters are missing from the request body.', _context
183
- elsif _response.status_code == 404
184
- raise ErrorResponseException.new '404 - Type does not exists.', _context
185
- elsif _response.status_code == 422
169
+ elsif _context.response.status_code == 404
170
+ raise DeleteErrorResponseException.new '404 - Delete actions not found.', _context
171
+ elsif _context.response.status_code == 422
186
172
  raise ErrorResponseException.new '422 - No query parameter (`user_id`, `item_id`, or `older_than`) is given. In order to delete all actionsdelete the type.', _context
187
- elsif _response.status_code == 429
173
+ elsif _context.response.status_code == 429
188
174
  raise ErrorResponseException.new '429 - Too many requests.', _context
189
- elsif _response.status_code == 505
175
+ elsif _context.response.status_code == 505
190
176
  raise ErrorResponseException.new '505 - Request timed out.', _context
191
- elsif _response.status_code == 500
177
+ elsif _context.response.status_code == 500
192
178
  raise APIException.new '500 - Unexpected internal error.', _context
193
179
  end
194
180
 
195
- # Global error handling using HTTP status codes.
181
+ # global error handling using HTTP status codes.
196
182
  validate_response(_context)
197
183
 
198
- # Return appropriate response type
199
- decoded = APIHelper.json_deserialize(_response.raw_body)
200
- return MessageResponse.from_hash(decoded)
184
+ # return appropriate response type
185
+ decoded = APIHelper.json_deserialize(_context.response.raw_body)
186
+ return DeleteSuccessResponse.from_hash(decoded)
201
187
  end
202
188
 
203
189
  # Post Bulk Actions
204
190
  # @param [Collection] actions Required parameter: List of actions to be posted.
205
- # @return MessageResponse response from the API call
191
+ # @return BulkPostResponse response from the API call
206
192
  def post_bulk_actions(actions)
207
193
  body = ''
208
194
  actions.each do |action|
209
195
  body += "#{action.to_json}\n"
210
196
  end
197
+
211
198
  # the base uri for api requests
212
199
  _query_builder = Configuration.base_uri.dup
213
200
 
@@ -219,45 +206,36 @@ module SuggestGrid
219
206
 
220
207
  # prepare headers
221
208
  _headers = {
222
- 'user-agent' => 'SUGGESTGRID',
223
209
  'accept' => 'application/json',
224
210
  'content-type' => 'text/plain; charset=utf-8'
225
211
  }
226
212
 
227
- # Create the HttpRequest object for the call
228
- _request = @http_client.post _query_url, headers: _headers, parameters: actions, username: Configuration.basic_auth_user_name, password: Configuration.basic_auth_password
229
-
230
- # Call the on_before_request callback
231
- @http_call_back.on_before_request(_request) if @http_call_back
213
+ # create the HttpRequest object for the call
214
+ _request = @http_client.post _query_url, headers: _headers, parameters: actions
232
215
 
233
- # Invoke the API call and get the response
234
- _response = @http_client.execute_as_string(_request)
216
+ # apply authentication
217
+ BasicAuth.apply(_request)
235
218
 
236
- # Wrap the request and response in an HttpContext object
237
- _context = HttpContext.new(_request, _response)
219
+ # execute the request
220
+ _context = execute_request(_request)
238
221
 
239
- # Call the on_after_response callback
240
- @http_call_back.on_after_response(_context) if @http_call_back
241
-
242
- # Endpoint error handling using HTTP status codes.
243
- if _response.status_code == 209
244
- raise BulkSchemaErrorResponseException.new '209 - Some metadata is not uploaded successfully.', _context
245
- elsif _response.status_code == 400
222
+ # endpoint error handling using HTTP status codes.
223
+ if _context.response.status_code == 400
246
224
  raise ErrorResponseException.new '400 - Body is missing.', _context
247
- elsif _response.status_code == 402
225
+ elsif _context.response.status_code == 402
248
226
  raise ErrorResponseException.new '402 - Action limit exceeded.', _context
249
- elsif _response.status_code == 429
227
+ elsif _context.response.status_code == 429
250
228
  raise ErrorResponseException.new '429 - Too many requests.', _context
251
- elsif _response.status_code == 500
229
+ elsif _context.response.status_code == 500
252
230
  raise APIException.new '500 - Unexpected internal error.', _context
253
231
  end
254
232
 
255
- # Global error handling using HTTP status codes.
233
+ # global error handling using HTTP status codes.
256
234
  validate_response(_context)
257
235
 
258
- # Return appropriate response type
259
- decoded = APIHelper.json_deserialize(_response.raw_body)
260
- return MessageResponse.from_hash(decoded)
236
+ # return appropriate response type
237
+ decoded = APIHelper.json_deserialize(_context.response.raw_body)
238
+ return BulkPostResponse.from_hash(decoded)
261
239
  end
262
240
  end
263
241
  end
@@ -4,14 +4,39 @@ module SuggestGrid
4
4
  class BaseController
5
5
  attr_accessor :http_client, :http_call_back
6
6
 
7
- @@http_client = UnirestClient.new
7
+ @@http_client = FaradayClient.new
8
+
9
+ @@global_headers = {
10
+ 'user-agent' => 'SUGGESTGRID'
11
+ }
8
12
 
9
13
  def initialize(http_client: nil, http_call_back: nil)
10
14
  @http_client = http_client ||= @@http_client
11
15
  @http_call_back = http_call_back
12
16
  end
13
17
 
14
- def validate_response(context)
18
+ def validate_parameters(args)
19
+ args.each do |name, value|
20
+ if value == nil
21
+ raise ArgumentError.new "Required parameter #{name} cannot be nil."
22
+ end
23
+ end
24
+ end
25
+
26
+ def execute_request(request, binary: false)
27
+ @http_call_back.on_before_request(request) if @http_call_back
28
+
29
+ request.headers = @@global_headers.clone.merge(request.headers)
30
+
31
+ response = binary ? @http_client.execute_as_binary(request) : @http_client.execute_as_string(request)
32
+ context = HttpContext.new(request, response)
33
+
34
+ @http_call_back.on_after_response(context) if @http_call_back
35
+
36
+ return context
37
+ end
38
+
39
+ def validate_response(context)
15
40
  if !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK
16
41
  raise APIException.new 'HTTP Response Not OK', context
17
42
  end