Class: SuggestGrid::ActionController

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

Overview

ActionController

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/action_controller.rb', line 10

def instance
  @instance
end

Instance Method Details

#delete_actions(type, user_id = nil, item_id = nil, older_than = nil) ⇒ Object

Warning: Please use get actions with the exact parameters first to inspect the actions to be deleted.

  • Type must be provided.

  • If user id is provided, all actions of the user will be deleted.

  • If item id is provided, all actions on the item will be deleted.

  • If older than is provided, all actions older than the timestamp or the

duration will be deleted.

  • If a number of these parameters are provided, the intersection of these

parameters will be deleted.

  • In addition to a type, at least one of these parameters must be

provided. In order to delete all the actions of a type, delete the type. parameter and at least one other parameter is required. id. the given duration, or the given time number. Could be a ISO 8601 duration, or a Unix time number. Specifications are available at en.wikipedia.org/wiki/ISO_8601#Durations, or en.wikipedia.org/wiki/Unix_time.

Parameters:

  • type (String)

    Required parameter: Delete actions of a type. This

  • user_id (String) (defaults to: nil)

    Optional parameter: Delete actions of a user id.

  • item_id (String) (defaults to: nil)

    Optional parameter: Delete actions of an item

  • older_than (String) (defaults to: nil)

    Optional parameter: Delete actions older than

Returns:

  • MessageResponse response from the API call



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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/suggestgrid/controllers/action_controller.rb', line 230

def delete_actions(type,
                   user_id = nil,
                   item_id = nil,
                   older_than = nil)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/actions'
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'type' => type,
      'user_id' => user_id,
      'item_id' => item_id,
      'older_than' => older_than
    },
    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.delete(
    _query_url,
    headers: _headers
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 400
    raise ErrorResponse.new(
      'Required user id or item id parameters are missing.',
      _context
    )
  elsif _context.response.status_code == 422
    raise ErrorResponse.new(
      'No query parameter (user id, item id, or older than) is given.' \
      ' qIn order to delete all actionsdelete the type.',
      _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

#get_actions(type = nil, user_id = nil, item_id = nil, older_than = nil, size = nil, from = nil) ⇒ Object

Get actions. Defaut responses will be paged by 10 actios each. Type, user id, item id, or older than parameters could be provided. The intersection of the provided parameters will be returned. given duration, or the given time number. Could be a ISO 8601 duration, or a Unix time number. Specifications are available at en.wikipedia.org/wiki/ISO_8601#Durations, or en.wikipedia.org/wiki/Unix_time. 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:

  • type (String) (defaults to: nil)

    Optional parameter: Get actions of a type.

  • user_id (String) (defaults to: nil)

    Optional parameter: Get actions of a user id.

  • item_id (String) (defaults to: nil)

    Optional parameter: Get actions of an item id.

  • older_than (String) (defaults to: nil)

    Optional parameter: Get actions older than the

  • 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:

  • ActionsResponse response from the API call



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
# File 'lib/suggestgrid/controllers/action_controller.rb', line 158

def get_actions(type = nil,
                user_id = nil,
                item_id = nil,
                older_than = nil,
                size = nil,
                from = nil)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/actions'
  _query_builder = APIHelper.append_url_with_query_parameters(
    _query_builder,
    {
      'type' => type,
      'user_id' => user_id,
      'item_id' => item_id,
      'older_than' => older_than,
      '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)
  ActionsResponse.from_hash(decoded)
end

#instanceObject



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

def instance
  self.class.instance
end

#post_action(action) ⇒ Object

Posts an action to the given type in the body. The body must have user id, item id and type. Rating is required for actions sent to an explicit type.

Parameters:

  • action (Action)

    Required parameter: The action to be posted.

Returns:

  • MessageResponse response from the API call



22
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
63
64
65
66
67
68
69
70
71
72
# File 'lib/suggestgrid/controllers/action_controller.rb', line 22

def post_action(action)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/actions'
  _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: action.to_json
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 400
    raise ErrorResponse.new(
      'Required user id or item id parameters are missing from the' \
      ' request.',
      _context
    )
  elsif _context.response.status_code == 402
    raise ErrorResponse.new(
      'Action limit exceeded.',
      _context
    )
  elsif _context.response.status_code == 404
    raise ErrorResponse.new(
      'Action type 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)
  MessageResponse.from_hash(decoded)
end

#post_bulk_actions(actions) ⇒ Object

Posts bulk actions to SuggestGrid. The recommended method for posting multiple actions at once. separated with newlines. Note that this is not a valid JSON data structure. The body size is limited to 10 thousand lines.

Parameters:

  • actions (Collection)

    Required parameter: List of actions to be posted

Returns:

  • BulkPostResponse response from the API call



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/suggestgrid/controllers/action_controller.rb', line 80

def post_bulk_actions(actions)
    body = ''
    actions.each do |action|
        body += "#{action.to_json}\n"
    end
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/actions/_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 == 402
    raise ErrorResponse.new(
      'Action limit exceeded.',
      _context
    )
  elsif _context.response.status_code == 404
    raise ErrorResponse.new(
      'Action type does not exists.',
      _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