Class: SuggestGrid::TypeController

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

Overview

TypeController

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

def instance
  @instance
end

Instance Method Details

#create_type(type, settings = nil) ⇒ Object

Creates a new type. for the rating parameter.

Parameters:

  • type (String)

    Required parameter: The name of the type.

  • settings (TypeRequestBody) (defaults to: nil)

    Optional parameter: Optional settings

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
73
74
75
76
# File 'lib/suggestgrid/controllers/type_controller.rb', line 22

def create_type(type,
                settings = nil)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/types/{type}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'type' => type
  )
  _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.put(
    _query_url,
    headers: _headers,
    parameters: settings.to_json
  )
  BasicAuth.apply(_request)
  _context = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _context.response.status_code == 402
    raise LimitExceededErrorResponse.new(
      'Type limit reached.',
      _context
    )
  elsif _context.response.status_code == 409
    raise ErrorResponse.new(
      'Type already exists.',
      _context
    )
  elsif _context.response.status_code == 422
    raise ErrorResponse.new(
      'Rating type is not `implicit` or `explicit`.',
      _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

#delete_all_typesObject

Deletes ALL the types and ALL the actions.

Returns:

  • MessageResponse response from the API call



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/suggestgrid/controllers/type_controller.rb', line 210

def delete_all_types
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/types'
  _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_type(type) ⇒ Object

Warning: Deletes the type with all of its actions and its recommendation model. deleted.

Parameters:

  • type (String)

    Required parameter: The name of the type to be

Returns:

  • MessageResponse response from the API call



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
168
169
170
171
# File 'lib/suggestgrid/controllers/type_controller.rb', line 130

def delete_type(type)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/types/{type}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'type' => type
  )
  _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 == 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

#get_all_typesObject

Returns all type names in an array named types.

Returns:

  • GetTypesResponse response from the API call



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/type_controller.rb', line 175

def get_all_types
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/types'
  _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)
  GetTypesResponse.from_hash(decoded)
end

#get_type(type) ⇒ Object

Returns the options of a type. The response rating parameter. properties.

Parameters:

  • type (String)

    Required parameter: The name of the type to get

Returns:

  • GetTypeResponse response from the API call



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
# File 'lib/suggestgrid/controllers/type_controller.rb', line 82

def get_type(type)
  # Prepare query url.
  _query_builder = Configuration.base_uri.dup
  _query_builder << '/v1/types/{type}'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'type' => type
  )
  _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(
      '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)
  GetTypeResponse.from_hash(decoded)
end

#instanceObject



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

def instance
  self.class.instance
end