Class: SuggestGrid::MetadataController

Inherits:
BaseController show all
Defined in:
lib/suggestgrid/controllers/metadata_controller.rb

Overview

MetadataController

Class Attribute Summary collapse

Attributes inherited from BaseController

#http_call_back, #http_client

Instance Method Summary collapse

Methods inherited from BaseController

#execute_request, #initialize, #validate_parameters, #validate_response

Constructor Details

This class inherits a constructor from SuggestGrid::BaseController

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



10
11
12
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 10

def instance
  @instance
end

Instance Method Details

#delete_all_itemsObject

Warning: Deletes all item metadata from SuggestGrid.

Returns:

  • MessageResponse response from the API call



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 542

def delete_all_items
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/items'
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.delete(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  MessageResponse.from_hash(decoded)
end

#delete_all_usersObject

Warning: Deletes all user metadata from SuggestGrid.

Returns:

  • MessageResponse response from the API call



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 263

def delete_all_users
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/users'
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.delete(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  MessageResponse.from_hash(decoded)
end

#delete_item(item_id) ⇒ Object

Deletes an item metadata with the given item id. metadata.

Parameters:

  • item_id (String)

    Required parameter: The item id to delete its

Returns:

  • MessageResponse response from the API call



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 503

def delete_item(item_id)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/items/{item_id}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'item_id' => item_id
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.delete(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  MessageResponse.from_hash(decoded)
end

#delete_user(user_id) ⇒ Object

Deletes a user metadata with the given user id. metadata.

Parameters:

  • user_id (String)

    Required parameter: The user id to delete its

Returns:

  • MessageResponse response from the API call



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 224

def delete_user(user_id)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/users/{user_id}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'user_id' => user_id
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.delete(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  MessageResponse.from_hash(decoded)
end

#get_item(item_id) ⇒ Object

Returns an item metadata if it exists. metadata.

Parameters:

  • item_id (String)

    Required parameter: The item id to get its

Returns:

  • Metadata response from the API call



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 405

def get_item(item_id)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/items/{item_id}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'item_id' => item_id
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 404
    raise ErrorResponse.new(
      'Item does not exists.',
      _context
    )
  end
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  Metadata.from_hash(decoded)
end

#get_items(size = nil, from = nil) ⇒ Object

Gets items and total count of items. Page and per-page parameters could be set. Defaults to 10. Must be between 1 and 10,000 inclusive. This parameter must be string represetation of an integer like “1”. from the response. Defaults to 0. Must be bigger than or equal to 0. This parameter must be string represetation of an integer like “1”.

Parameters:

  • size (Long) (defaults to: nil)

    Optional parameter: The number of the users response.

  • from (Long) (defaults to: nil)

    Optional parameter: The number of users to be skipped

Returns:

  • ItemsResponse response from the API call



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 457

def get_items(size = nil,
              from = nil)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/items'
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'size' => size,
      'from' => from
    },
    array_serialization: Configuration.array_serialization
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  ItemsResponse.from_hash(decoded)
end

#get_user(user_id) ⇒ Object

Returns a user metadata if it exists. metadata.

Parameters:

  • user_id (String)

    Required parameter: The user id to get its

Returns:

  • Metadata response from the API call



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 126

def get_user(user_id)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/users/{user_id}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'user_id' => user_id
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 404
    raise ErrorResponse.new(
      'User does not exists.',
      _context
    )
  end
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  Metadata.from_hash(decoded)
end

#get_users(size = nil, from = nil) ⇒ Object

Get items and total count of items. Page and per-page parameters could be set. Defaults to 10. Must be between 1 and 10,000 inclusive. This parameter must be string represetation of an integer like “1”. from the response. Defaults to 0. Must be bigger than or equal to 0. This parameter must be string represetation of an integer like “1”.

Parameters:

  • size (Long) (defaults to: nil)

    Optional parameter: The number of the users response.

  • from (Long) (defaults to: nil)

    Optional parameter: The number of users to be skipped

Returns:

  • UsersResponse response from the API call



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 178

def get_users(size = nil,
              from = nil)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/users'
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'size' => size,
      'from' => from
    },
    array_serialization: Configuration.array_serialization
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.get(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  UsersResponse.from_hash(decoded)
end

#instanceObject



13
14
15
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 13

def instance
  self.class.instance
end

#post_bulk_items(items) ⇒ Object

Posts item metadata in bulk. Note that this operation completely overrides metadata with the same ids, if they exist. objects separated with newlines. Each item metadata object must have its id field. Note that this is not a valid JSON data structure. The body size is limited to 10 thousand lines.

Parameters:

  • items (Collection)

    Required parameter: List of item metadata, whose size is limited to 10 thousand.

Returns:

  • BulkPostResponse response from the API call



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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 351

def post_bulk_items(items)
    body = ''
    items.each do |item|
        body += "#{item.to_json}\n"
    end
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/items/_bulk'
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'text/plain; charset=utf-8'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.post(
    _query_url,
    headers: _headers,
    parameters: body
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 400
    raise ErrorResponse.new(
      'Body is missing.',
      _context
    )
  elsif _context.response.status_code == 413
    raise ErrorResponse.new(
      'Bulk request maximum line count exceeded.',
      _context
    )
  end
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  BulkPostResponse.from_hash(decoded)
end

#post_bulk_users(users) ⇒ Object

Posts user metadata in bulk. Note that this operation completely overrides metadata with the same ids, if they exist. objects separated with newlines. Each user metadata object must have its id field. Note that this is not a valid JSON data structure. The body size is limited to 10 thousand lines.

Parameters:

  • users (Collection)

    Required parameter: List of user metadata, whose size is limited to 10 thousand.

Returns:

  • BulkPostResponse response from the API call



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 72

def post_bulk_users(users)
    body = ''
    users.each do |user|
        body += "#{user.to_json}\n"
    end
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/users/_bulk'
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'text/plain; charset=utf-8'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.post(
    _query_url,
    headers: _headers,
    parameters: body
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 400
    raise ErrorResponse.new(
      'Body is missing.',
      _context
    )
  elsif _context.response.status_code == 413
    raise ErrorResponse.new(
      'Bulk request maximum line count exceeded.',
      _context
    )
  end
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  BulkPostResponse.from_hash(decoded)
end

#post_item(item) ⇒ Object

Posts an item metadata. Note that this operation completely overrides previous metadata for the id, if it exists. Metadata format has its restrictions.

Parameters:

  • item (Metadata)

    Required parameter: The metadata to be saved.

Returns:

  • MessageResponse response from the API call



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 302

def post_item(item)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/items'
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.post(
    _query_url,
    headers: _headers,
    parameters: item.to_json
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 400
    raise DetailedErrorResponse.new(
      'Metadata is invalid.',
      _context
    )
  end
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  MessageResponse.from_hash(decoded)
end

#post_user(user) ⇒ Object

Posts a user metadata. Note that this operation completely overrides previous metadata for the id, if it exists. Metadata format has its restrictions.

Parameters:

  • user (Metadata)

    Required parameter: The metadata to be saved.

Returns:

  • MessageResponse response from the API call



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/suggestgrid/controllers/metadata_controller.rb', line 23

def post_user(user)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/users'
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8'
  }

  # Prepare and execute HttpRequest.
  _request = @http_client.post(
    _query_url,
    headers: _headers,
    parameters: user.to_json
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 400
    raise DetailedErrorResponse.new(
      'Metadata is invalid.',
      _context
    )
  end
  unless _context.response.status_code.between?(200, 208)
    raise ErrorResponse.new(
      'Unexpected internal error.',
      _context
    )
  end
  validate_response(_context)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  MessageResponse.from_hash(decoded)
end