Class: MoesifApi::ApiController

Inherits:
BaseController show all
Defined in:
lib/moesif_api/controllers/api_controller.rb

Constant Summary collapse

@@instance =
ApiController.new

Instance Attribute Summary

Attributes inherited from BaseController

#http_call_back, #http_client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#build_request_headers, #initialize, #validate_response

Constructor Details

This class inherits a constructor from MoesifApi::BaseController

Class Method Details

.instanceObject

Singleton instance of the controller class



8
9
10
# File 'lib/moesif_api/controllers/api_controller.rb', line 8

def self.instance
  @@instance
end

Instance Method Details

#create_event(body) ⇒ Object

Add Single API Event Call

Parameters:

  • body (EventModel)

    Required parameter: Example:

Returns:

  • void response from the API call



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
77
# File 'lib/moesif_api/controllers/api_controller.rb', line 52

def create_event(body)
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/events'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = {
    'content-type' => 'application/json; charset=utf-8',
    'X-Moesif-Application-Id' => Configuration.application_id,
    'User-Agent' => 'moesifapi-ruby/' +  Configuration.version
  }

  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
  _response, _context = send_moesif(_query_url, _headers, body)

  # Global error handling using HTTP status codes.
  validate_response(_context)

  # Return response headers
  return _response.headers
end

#create_events_batch(body) ⇒ Object

Add multiple API Events in a single batch (batch size must be less than 250kb)

Parameters:

  • body (List of EventModel)

    Required parameter: Example:

Returns:

  • void 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
# File 'lib/moesif_api/controllers/api_controller.rb', line 82

def create_events_batch(body)
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/events/batch'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = build_request_headers()

  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
  _response, _context = send_moesif(_query_url, _headers, body)

  # Global error handling using HTTP status codes.
  validate_response(_context)

  # Return response headers
  return _response.headers
end

#get_app_configObject

Get Application configuration

Parameters:

  • void

    Required parameter.

Returns:

  • response from the API call



154
155
156
157
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
# File 'lib/moesif_api/controllers/api_controller.rb', line 154

def get_app_config()
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/config'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = build_request_headers()

  # Create the HttpRequest object for the call
  _request = @http_client.get _query_url, headers: _headers

  # Call the on_before_request callback
  @http_call_back.on_before_request(_request) if @http_call_back

  # Invoke the API call and get the response
  _response = @http_client.execute_as_string(_request)

  # Wrap the request and response in an HttpContext object
  _context = HttpContext.new(_request, _response)

  # Call the on_after_response callback
  @http_call_back.on_after_response(_context) if @http_call_back

  # Global error handling using HTTP status codes.
  validate_response(_context)

  return _response.json_body, _context
end

#get_rulesObject

Get App Governance Rules

Parameters:

  • void

    Required parameter.

Returns:

  • response from the API call



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
219
220
221
222
223
224
# File 'lib/moesif_api/controllers/api_controller.rb', line 191

def get_rules()
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/rules'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = build_request_headers()

  # Create the HttpRequest object for the call
  _request = @http_client.get _query_url, headers: _headers

  # Call the on_before_request callback
  @http_call_back.on_before_request(_request) if @http_call_back

  # Invoke the API call and get the response
  _response = @http_client.execute_as_string(_request)

  # Wrap the request and response in an HttpContext object
  _context = HttpContext.new(_request, _response)

  # Call the on_after_response callback
  @http_call_back.on_after_response(_context) if @http_call_back

  # Global error handling using HTTP status codes.
  validate_response(_context)

  # Return the response
  return _response.json_body, _context
end

#send_moesif(url, headers, body) ⇒ Object

API Call to send data to Moesif

Parameters:

  • body (EventModel)

    Required [Hash] headers Required [String] url Required

Returns:

  • response and context from the API call



15
16
17
18
19
20
21
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
# File 'lib/moesif_api/controllers/api_controller.rb', line 15

def send_moesif(url, headers, body)
  begin
    # Gzip the body
    gzip = Zlib::GzipWriter.new(StringIO.new)
    gzip << body.to_json
    gzip_body = gzip.close.string

    # Add Content-Encoding header
    headers['Content-Encoding'] = 'gzip'
     # Gzip payload
     _request_body = gzip_body
  rescue => e
     # Json payload
     _request_body = body.to_json
  end

  # Create the HttpRequest object for the call
  _request = @http_client.post url, headers: headers, parameters: _request_body

  # Call the on_before_request callback
  @http_call_back.on_before_request(_request) if @http_call_back

  # Invoke the API call and get the response
  _response = @http_client.execute_as_string(_request)

  # Wrap the request and response in an HttpContext object
  _context = HttpContext.new(_request, _response)

  # Call the on_after_response callback
  @http_call_back.on_after_response(_context) if @http_call_back

  return _response, _context
end

#update_companies_batch(body) ⇒ Object

Update Data for multiple Companies in a single batch (batch size must be less than 250kb)

Parameters:

Returns:

  • void response from the API call



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/moesif_api/controllers/api_controller.rb', line 252

def update_companies_batch(body)
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/companies/batch'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = {
    'content-type' => 'application/json; charset=utf-8',
    'X-Moesif-Application-Id' => Configuration.application_id,
    'User-Agent' => 'moesifapi-ruby/' +  Configuration.version
  }

  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
  _response, _context = send_moesif(_query_url, _headers, body)

  # Global error handling using HTTP status codes.
  validate_response(_context)
end

#update_company(body) ⇒ Object

Update Data for a Single Company

Parameters:

Returns:

  • void response from the API call



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/moesif_api/controllers/api_controller.rb', line 229

def update_company(body)
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/companies'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = build_request_headers()

  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
  _response, _context = send_moesif(_query_url, _headers, body)

  # Global error handling using HTTP status codes.
  validate_response(_context)
end

#update_user(body) ⇒ Object

Update Data for a Single User

Parameters:

Returns:

  • void response from the API call



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/moesif_api/controllers/api_controller.rb', line 108

def update_user(body)
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/users'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = build_request_headers()
  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
  _response, _context = send_moesif(_query_url, _headers, body)

  # Global error handling using HTTP status codes.
  validate_response(_context)
end

#update_users_batch(body) ⇒ Object

Update Data for multiple Users in a single batch (batch size must be less than 250kb)

Parameters:

  • body (list of UserModel)

    Required parameter.

Returns:

  • void response from the API call



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/moesif_api/controllers/api_controller.rb', line 131

def update_users_batch(body)
  # the base uri for api requests
  _query_builder = Configuration.base_uri.dup

  # prepare query string for API call
  _query_builder << '/v1/users/batch'

  # validate and preprocess url
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = build_request_headers()

  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
  _response, _context = send_moesif(_query_url, _headers, body)

  # Global error handling using HTTP status codes.
  validate_response(_context)
end