suggestgrid 0.1.15.pre.SNAPSHOT

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +28 -0
  3. data/README.md +104 -0
  4. data/lib/suggest_grid/api_helper.rb +151 -0
  5. data/lib/suggest_grid/configuration.rb +21 -0
  6. data/lib/suggest_grid/controllers/action_controller.rb +257 -0
  7. data/lib/suggest_grid/controllers/base_controller.rb +20 -0
  8. data/lib/suggest_grid/controllers/metadata_controller.rb +527 -0
  9. data/lib/suggest_grid/controllers/recommendation_controller.rb +119 -0
  10. data/lib/suggest_grid/controllers/similarity_controller.rb +119 -0
  11. data/lib/suggest_grid/controllers/type_controller.rb +280 -0
  12. data/lib/suggest_grid/exceptions/api_exception.rb +16 -0
  13. data/lib/suggest_grid/http/http_call_back.rb +17 -0
  14. data/lib/suggest_grid/http/http_client.rb +112 -0
  15. data/lib/suggest_grid/http/http_context.rb +15 -0
  16. data/lib/suggest_grid/http/http_method_enum.rb +7 -0
  17. data/lib/suggest_grid/http/http_request.rb +28 -0
  18. data/lib/suggest_grid/http/http_response.rb +19 -0
  19. data/lib/suggest_grid/http/unirest_client.rb +41 -0
  20. data/lib/suggest_grid/models/action.rb +66 -0
  21. data/lib/suggest_grid/models/bulk_schema_error_response.rb +53 -0
  22. data/lib/suggest_grid/models/count_response.rb +39 -0
  23. data/lib/suggest_grid/models/error_response.rb +48 -0
  24. data/lib/suggest_grid/models/get_recommended_items_body.rb +111 -0
  25. data/lib/suggest_grid/models/get_recommended_users_body.rb +111 -0
  26. data/lib/suggest_grid/models/get_similar_items_body.rb +102 -0
  27. data/lib/suggest_grid/models/get_similar_users_body.rb +102 -0
  28. data/lib/suggest_grid/models/get_type_response.rb +39 -0
  29. data/lib/suggest_grid/models/get_types_response.rb +48 -0
  30. data/lib/suggest_grid/models/items_response.rb +53 -0
  31. data/lib/suggest_grid/models/message_response.rb +39 -0
  32. data/lib/suggest_grid/models/metadata.rb +39 -0
  33. data/lib/suggest_grid/models/metadata_information_response.rb +39 -0
  34. data/lib/suggest_grid/models/schema_error_response.rb +57 -0
  35. data/lib/suggest_grid/models/type_request_body.rb +40 -0
  36. data/lib/suggest_grid/models/users_response.rb +53 -0
  37. data/lib/suggest_grid/suggest_grid_client.rb +47 -0
  38. data/lib/suggest_grid.rb +48 -0
  39. data/spec/swagger.yaml +1169 -0
  40. metadata +123 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c02249dab28bb791116722c17977fb33500a9e9a
4
+ data.tar.gz: da4dcec2787f07bdc245dea3b0fd7c3d8fcf34ed
5
+ SHA512:
6
+ metadata.gz: afe0eceea0a2a7d9f07c6a30467d5f0aa689baa4d24d436ab31c77f07b78e0845da04e7ffd83a2d18b2e1d64bccfa240a5299a267b0f055632657d276ef558aa
7
+ data.tar.gz: 016f2caf8052d210ac1e71df72ef7968c7b46525f885d5312c1db4eeaf3026c35c39666b3c20a893e2edcceefb73a6e2e9af520974b90d62183e86206aeb7755
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ License:
2
+ ========
3
+ The MIT License (MIT)
4
+ http://opensource.org/licenses/MIT
5
+
6
+ Copyright (c) 2014 - 2016 APIMATIC Limited
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
25
+
26
+ Trade Mark:
27
+ ==========
28
+ APIMATIC is a trade mark for APIMATIC Limited
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # [ SuggestGrid Ruby Client ]( http://www.github.com/suggestgrid/suggestgrid-ruby )
2
+
3
+ We will walk through how to get started with SuggestGrid Ruby Client in three steps:
4
+
5
+ 1. [Configuration](#1-configuration)
6
+
7
+ 2. [Post actions](#2-post-actions)
8
+
9
+ 3. [Get recommendations](#3-get-recommendations)
10
+
11
+ If you did not [sign up for SuggestGrid](https://dashboard.suggestgrid.com/users/sign_up), this is the right time.
12
+
13
+ ## Getting Started
14
+
15
+ In this guide we will demonstrate how to display personalized recommendations on an existing Ruby project.
16
+
17
+ We have a movie catalog Ruby application, SuggestGridMovies, similar to IMDb.
18
+ For logged in users we want to display movies that *similar people viewed* on movie pages.
19
+ Let's implement this feature in five minutes with SuggestGrid!
20
+
21
+ ### 1. Configuration
22
+
23
+ We are beginning the development by adding the client as a dependency:
24
+
25
+ ```ruby
26
+ gem 'suggest_grid', git: 'https://github.com/suggestgrid/suggestgrid-ruby.git'
27
+ ```
28
+
29
+
30
+
31
+ Applications make their API requests to their dedicated sub-domain of `suggestgrid.space`.
32
+
33
+ Most endpoints require a username and password for authentication.
34
+
35
+ An initial user name and password is given on sign up.
36
+
37
+ It is very convenient to configure SuggestGrid by setting an authenticated `SUGGESTGRID_URL` environment variable in the format below:
38
+
39
+ `http://{user}:{pass}@{region}.suggestgrid.space/{app-uuid}`
40
+
41
+ You can authenticate your application using `SUGGESTGRID_URL` environment variable like the example below:
42
+
43
+ ```ruby
44
+ sg_uri = ENV['SUGGESTGRID_URL']
45
+
46
+ # Initialize the SuggestGrid client.
47
+ ::SuggestGridClient = SuggestGrid::SuggestGridClient.new sg_uri
48
+ ```
49
+
50
+
51
+ Every recommendation logic needs to belong to a *type*.
52
+ For this demonstration we can create an implicit type named as `views`.
53
+ This could be done either from the dashboard or with a snippet like this:
54
+
55
+ ```ruby
56
+ begin
57
+ SuggestGridClient.type.get_type('views')
58
+ puts "SuggestGrid type named 'views' already exists, skipping creation."
59
+ rescue SuggestGrid::APIException => e
60
+ SuggestGridClient.type.create_type('views')
61
+ puts "SuggestGrid type named 'views' is created."
62
+ end
63
+ ```
64
+
65
+
66
+
67
+ ### 2. Post actions
68
+
69
+ Once the type exists, let's start posting actions.
70
+ We should invoke SuggestGrid client's action.post_action when an user views an item in our application.
71
+
72
+ We can do this by putting the snippet below on the relevant point:
73
+
74
+ ```ruby
75
+ # Send action to SuggestGrid.
76
+ begin
77
+ SuggestGridClient.action.post_action(SuggestGrid::Action.new('views',user.id, self.id))
78
+ rescue Exception => e
79
+ puts "Exception while sending action #{e}"
80
+ end
81
+ ```
82
+
83
+
84
+ The more actions SuggestGrid gets, more relevant and diverse its responses are.
85
+
86
+
87
+ ### 3. Get recommendations
88
+
89
+ Finally, let's show "movies similar users viewed" on movie pages.
90
+
91
+ SuggestGrid needs *recommendation models* for returning recommendations.
92
+ Model generation is scheduled in every 24 hours.
93
+ In addition, instant model generations can be triggered on the dashboard.
94
+
95
+ Once the first model generated for 'views' type, recommendations could be get using a snippet like the following:
96
+
97
+ ```ruby
98
+ begin
99
+ items_response = SuggestGridClient.recommendation.get_recommended_items({type: 'view', user_id: user.id, size: size})
100
+ items_response.items
101
+ rescue Exception => e
102
+ puts "Exception while getting recommendations #{e}"
103
+ end
104
+ ```
@@ -0,0 +1,151 @@
1
+ # This file was automatically generated for SuggestGrid by APIMATIC v2.0 ( https://apimatic.io ).
2
+
3
+ module SuggestGrid
4
+ class APIHelper
5
+ # Replaces template parameters in the given url
6
+ # @param [String] The query string builder to replace the template parameters
7
+ # @param [Array] The parameters to replace in the url
8
+ def self.append_url_with_template_parameters(query_builder, parameters)
9
+ # perform parameter validation
10
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
11
+
12
+ # return if there are no parameters to replace
13
+ if parameters.nil?
14
+ query_builder
15
+ else
16
+ # iterate and append parameters
17
+ parameters.each do |key, value|
18
+ replace_value = ''
19
+
20
+ if value.nil?
21
+ replace_value = ''
22
+ elsif value.instance_of? Array
23
+ value.map!{|element| CGI.escape(element.to_s)}
24
+ replace_value = value.join('/')
25
+ else
26
+ replace_value = CGI.escape(value.to_s)
27
+ end
28
+
29
+ # find the template parameter and replace it with its value
30
+ query_builder = query_builder.gsub('{' + key.to_s + '}', replace_value)
31
+ end
32
+ end
33
+ return query_builder
34
+ end
35
+
36
+ # Appends the given set of parameters to the given query string
37
+ # @param [String] The query string builder to replace the template parameters
38
+ # @param [Array] The parameters to append
39
+ def self.append_url_with_query_parameters(query_builder, parameters)
40
+ # perform parameter validation
41
+ raise ArgumentError, 'Given value for parameter \"query_builder\" is invalid.' unless query_builder.instance_of? String
42
+
43
+ # return if there are no parameters to replace
44
+ if parameters.nil?
45
+ return query_builder
46
+ else
47
+ # remove any nil values
48
+ parameters = parameters.reject { |_key, value| value.nil? }
49
+
50
+ # does the query string already has parameters
51
+ has_params = query_builder.include? '?'
52
+ separator = has_params ? '&' : '?'
53
+
54
+ # append query with separator and parameters and return
55
+ return query_builder << separator << URI.encode_www_form(parameters)
56
+ end
57
+ end
58
+
59
+ # Validates and processes the given Url
60
+ # @param [String] The given Url to process
61
+ # @return [String] Pre-processed Url as string
62
+ def self.clean_url(url)
63
+ # perform parameter validation
64
+ raise ArgumentError, 'Invalid Url.' unless url.instance_of? String
65
+
66
+ # ensure that the urls are absolute
67
+ matches = url.match(%r{^(https?:\/\/[^\/]+)})
68
+ raise ArgumentError, 'Invalid Url format.' if matches.nil?
69
+
70
+ # get the http protocol match
71
+ protocol = matches[1]
72
+
73
+ # check if parameters exist
74
+ index = url.index('?')
75
+
76
+ # remove redundant forward slashes
77
+ query = url[protocol.length...(index != nil ? index : url.length)]
78
+ query.gsub!(%r{\/\/+}, '/')
79
+
80
+ # get the parameters
81
+ parameters = index != nil ? url[url.index('?')...url.length] : ""
82
+
83
+ # return processed url
84
+ return protocol + query + parameters
85
+ end
86
+
87
+ # Parses JSON string.
88
+ # @param [String] A JSON string.
89
+ def self.json_deserialize(json)
90
+ begin
91
+ return JSON.parse(json)
92
+ rescue
93
+ raise TypeError, "Server responded with invalid JSON."
94
+ end
95
+ end
96
+
97
+ # Form encodes a hash of parameters.
98
+ # @param [Hash] The hash of parameters to encode.
99
+ # @return [Hash] A hash with the same parameters form encoded.
100
+ def self.form_encode_parameters(form_parameters)
101
+ encoded = Hash.new
102
+ form_parameters.each do |key, value|
103
+ encoded.merge!(APIHelper.form_encode value, key)
104
+ end
105
+ return encoded
106
+ end
107
+
108
+ # Form encodes an object.
109
+ # @param [Dynamic] An object to form encode.
110
+ # @param [String] The name of the object.
111
+ # @return [Hash] A form encoded representation of the object in the form of a hash.
112
+ def self.form_encode(obj, instance_name)
113
+ retval = Hash.new
114
+
115
+ # If this is a structure, resolve it's field names.
116
+ if obj.respond_to? :key_map
117
+ obj = obj.key_map
118
+ end
119
+
120
+ # Create a form encoded hash for this object.
121
+ if obj == nil
122
+ nil
123
+ elsif obj.instance_of? Array
124
+ obj.each_with_index do |value, index|
125
+ retval.merge!(APIHelper.form_encode(value, instance_name + "[" + index.to_s + "]"))
126
+ end
127
+ elsif obj.instance_of? Hash
128
+ obj.each do |key, value|
129
+ retval.merge!(APIHelper.form_encode(value, instance_name + "[" + key + "]"))
130
+ end
131
+ else
132
+ retval[instance_name] = obj
133
+ end
134
+ return retval
135
+ end
136
+ end
137
+ end
138
+
139
+ # extend types to support to_bool
140
+ module ToBoolean
141
+ def to_bool
142
+ return true if self == true || self.to_s.strip =~ /^(true|yes|y|1)$/i
143
+ return false
144
+ end
145
+ end
146
+
147
+ class NilClass; include ToBoolean; end
148
+ class TrueClass; include ToBoolean; end
149
+ class FalseClass; include ToBoolean; end
150
+ class Numeric; include ToBoolean; end
151
+ class String; include ToBoolean; end
@@ -0,0 +1,21 @@
1
+ # This file was automatically generated for SuggestGrid by APIMATIC v2.0 ( https://apimatic.io ).
2
+
3
+ module SuggestGrid
4
+ class Configuration
5
+ # The base Uri for API calls
6
+ @base_uri = 'http://localhost:9090'
7
+
8
+ # The username to use with basic authentication
9
+ @basic_auth_user_name = 'TODO: Replace'
10
+
11
+ # The password to use with basic authentication
12
+ @basic_auth_password = 'TODO: Replace'
13
+
14
+ # create the getters and setters
15
+ class << self
16
+ attr_accessor :base_uri
17
+ attr_accessor :basic_auth_user_name
18
+ attr_accessor :basic_auth_password
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,257 @@
1
+ # This file was automatically generated for SuggestGrid by APIMATIC v2.0 ( https://apimatic.io ).
2
+
3
+ module SuggestGrid
4
+ class ActionController < BaseController
5
+ @@instance = ActionController.new
6
+ # Singleton instance of the controller class
7
+ def self.instance
8
+ @@instance
9
+ end
10
+
11
+ # Get Actions
12
+ # @param [String] type Optional parameter: The type of the actions.
13
+ # @param [String] user_id Optional parameter: The user id of the actions.
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
17
+ def get_actions(type = nil,
18
+ user_id = nil,
19
+ item_id = nil,
20
+ older_than = nil)
21
+ # the base uri for api requests
22
+ _query_builder = Configuration.base_uri.dup
23
+
24
+ # prepare query string for API call
25
+ _query_builder << '/v1/actions'
26
+
27
+ # process optional query parameters
28
+ _query_builder = APIHelper.append_url_with_query_parameters _query_builder, {
29
+ 'type' => type,
30
+ 'user_id' => user_id,
31
+ 'item_id' => item_id,
32
+ 'older_than' => older_than
33
+ }
34
+
35
+ # validate and preprocess url
36
+ _query_url = APIHelper.clean_url _query_builder
37
+
38
+ # prepare headers
39
+ _headers = {
40
+ 'user-agent' => 'SUGGESTGRID',
41
+ 'accept' => 'application/json'
42
+ }
43
+
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)
52
+
53
+ # Wrap the request and response in an HttpContext object
54
+ _context = HttpContext.new(_request, _response)
55
+
56
+ # Call the on_after_response callback
57
+ @http_call_back.on_after_response(_context) if @http_call_back
58
+
59
+ # Endpoint error handling using HTTP status codes.
60
+ if _response.status_code == 400
61
+ raise APIException.new '400 - Required `user_id` or `item_id` parameters are missing from the request body.', _context
62
+ elsif _response.status_code == 429
63
+ raise APIException.new '429 - Too many requests.', _context
64
+ elsif _response.status_code == 500
65
+ raise APIException.new '500 - Unexpected internal error.', _context
66
+ end
67
+
68
+ # Global error handling using HTTP status codes.
69
+ validate_response(_context)
70
+
71
+ # Return appropriate response type
72
+ decoded = APIHelper.json_deserialize(_response.raw_body)
73
+ return CountResponse.from_hash(decoded)
74
+ end
75
+
76
+ # Post an Action
77
+ # @param [Action] body Required parameter: The action to be posted.
78
+ # @return MessageResponse response from the API call
79
+ def post_action(body)
80
+ # the base uri for api requests
81
+ _query_builder = Configuration.base_uri.dup
82
+
83
+ # prepare query string for API call
84
+ _query_builder << '/v1/actions'
85
+
86
+ # validate and preprocess url
87
+ _query_url = APIHelper.clean_url _query_builder
88
+
89
+ # prepare headers
90
+ _headers = {
91
+ 'user-agent' => 'SUGGESTGRID',
92
+ 'accept' => 'application/json',
93
+ 'content-type' => 'application/json; charset=utf-8'
94
+ }
95
+
96
+ # Create the HttpRequest object for the call
97
+ _request = @http_client.post _query_url, headers: _headers, parameters: body.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
101
+
102
+ # Invoke the API call and get the response
103
+ _response = @http_client.execute_as_string(_request)
104
+
105
+ # Wrap the request and response in an HttpContext object
106
+ _context = HttpContext.new(_request, _response)
107
+
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
113
+ raise APIException.new '400 - Required `user_id` or `item_id` parameters are missing from the request body.', _context
114
+ elsif _response.status_code == 402
115
+ raise APIException.new '402 - Action limit exceeded.', _context
116
+ elsif _response.status_code == 404
117
+ raise APIException.new '404 - Type does not exists.', _context
118
+ elsif _response.status_code == 429
119
+ raise APIException.new '429 - Too many requests.', _context
120
+ elsif _response.status_code == 500
121
+ raise APIException.new '500 - Unexpected internal error.', _context
122
+ end
123
+
124
+ # Global error handling using HTTP status codes.
125
+ validate_response(_context)
126
+
127
+ # Return appropriate response type
128
+ decoded = APIHelper.json_deserialize(_response.raw_body)
129
+ return MessageResponse.from_hash(decoded)
130
+ end
131
+
132
+ # Delete Actions
133
+ # @param [String] type Optional parameter: The type of the actions.
134
+ # @param [String] user_id Optional parameter: The user id of the actions.
135
+ # @param [String] item_id Optional parameter: The item id of the actions.
136
+ # @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
138
+ def delete_actions(type = nil,
139
+ user_id = nil,
140
+ item_id = nil,
141
+ older_than = nil)
142
+ # the base uri for api requests
143
+ _query_builder = Configuration.base_uri.dup
144
+
145
+ # prepare query string for API call
146
+ _query_builder << '/v1/actions'
147
+
148
+ # process optional query parameters
149
+ _query_builder = APIHelper.append_url_with_query_parameters _query_builder, {
150
+ 'type' => type,
151
+ 'user_id' => user_id,
152
+ 'item_id' => item_id,
153
+ 'older_than' => older_than
154
+ }
155
+
156
+ # validate and preprocess url
157
+ _query_url = APIHelper.clean_url _query_builder
158
+
159
+ # prepare headers
160
+ _headers = {
161
+ 'user-agent' => 'SUGGESTGRID',
162
+ 'accept' => 'application/json'
163
+ }
164
+
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)
173
+
174
+ # Wrap the request and response in an HttpContext object
175
+ _context = HttpContext.new(_request, _response)
176
+
177
+ # Call the on_after_response callback
178
+ @http_call_back.on_after_response(_context) if @http_call_back
179
+
180
+ # Endpoint error handling using HTTP status codes.
181
+ if _response.status_code == 400
182
+ raise APIException.new '400 - Required `user_id` or `item_id` parameters are missing from the request body.', _context
183
+ elsif _response.status_code == 404
184
+ raise APIException.new '404 - Type does not exists.', _context
185
+ elsif _response.status_code == 422
186
+ raise APIException.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
188
+ raise APIException.new '429 - Too many requests.', _context
189
+ elsif _response.status_code == 500
190
+ raise APIException.new '500 - Unexpected internal error.', _context
191
+ end
192
+
193
+ # Global error handling using HTTP status codes.
194
+ validate_response(_context)
195
+
196
+ # Return appropriate response type
197
+ decoded = APIHelper.json_deserialize(_response.raw_body)
198
+ return MessageResponse.from_hash(decoded)
199
+ end
200
+
201
+ # Post Bulk Actions
202
+ # @param [Collection] actions Required parameter: List of actions to be posted.
203
+ # @return MessageResponse response from the API call
204
+ def post_bulk_actions(actions)
205
+ body = ''
206
+ actions.each do |action|
207
+ body += "#{action.to_json}\n"
208
+ end
209
+ # the base uri for api requests
210
+ _query_builder = Configuration.base_uri.dup
211
+
212
+ # prepare query string for API call
213
+ _query_builder << '/v1/actions/_bulk'
214
+
215
+ # validate and preprocess url
216
+ _query_url = APIHelper.clean_url _query_builder
217
+
218
+ # prepare headers
219
+ _headers = {
220
+ 'user-agent' => 'SUGGESTGRID',
221
+ 'accept' => 'application/json',
222
+ 'content-type' => 'text/plain; charset=utf-8'
223
+ }
224
+
225
+ # Create the HttpRequest object for the call
226
+ _request = @http_client.post _query_url, headers: _headers, parameters: body, username: Configuration.basic_auth_user_name, password: Configuration.basic_auth_password
227
+
228
+ # Call the on_before_request callback
229
+ @http_call_back.on_before_request(_request) if @http_call_back
230
+
231
+ # Invoke the API call and get the response
232
+ _response = @http_client.execute_as_string(_request)
233
+
234
+ # Wrap the request and response in an HttpContext object
235
+ _context = HttpContext.new(_request, _response)
236
+
237
+ # Call the on_after_response callback
238
+ @http_call_back.on_after_response(_context) if @http_call_back
239
+
240
+ # Endpoint error handling using HTTP status codes.
241
+ if _response.status_code == 402
242
+ raise APIException.new '402 - Action limit exceeded.', _context
243
+ elsif _response.status_code == 429
244
+ raise APIException.new '429 - Too many requests.', _context
245
+ elsif _response.status_code == 500
246
+ raise APIException.new '500 - Unexpected internal error.', _context
247
+ end
248
+
249
+ # Global error handling using HTTP status codes.
250
+ validate_response(_context)
251
+
252
+ # Return appropriate response type
253
+ decoded = APIHelper.json_deserialize(_response.raw_body)
254
+ return MessageResponse.from_hash(decoded)
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,20 @@
1
+ # This file was automatically generated for SuggestGrid by APIMATIC v2.0 ( https://apimatic.io ).
2
+
3
+ module SuggestGrid
4
+ class BaseController
5
+ attr_accessor :http_client, :http_call_back
6
+
7
+ @@http_client = UnirestClient.new
8
+
9
+ def initialize(http_client: nil, http_call_back: nil)
10
+ @http_client = http_client ||= @@http_client
11
+ @http_call_back = http_call_back
12
+ end
13
+
14
+ def validate_response(context)
15
+ if !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK
16
+ raise APIException.new 'HTTP Response Not OK', context
17
+ end
18
+ end
19
+ end
20
+ end