moesif_rack 1.2.6 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +133 -5
- data/lib/moesif_rack/moesif_middleware.rb +22 -49
- data/lib/moesif_rack/update_company.rb +56 -0
- data/lib/moesif_rack/update_user.rb +56 -0
- data/moesif_capture_outgoing/httplog.rb +2 -0
- data/moesif_capture_outgoing/httplog/adapters/net_http.rb +29 -0
- data/moesif_capture_outgoing/httplog/http_log.rb +164 -0
- data/test/moesif_rack_test.rb +66 -2
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45a13b3f4b85664a0147ef4abe30bd73f1b11d21a96ce30b21d32f15185217e6
|
4
|
+
data.tar.gz: 02c92b07b397124108dd0eba645b830b9da5b222a4058e9048c8b7bc380b6dd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ccf6066387ea409cc74f7910bae06a33f2419442ddeba773b29329888e78ca61cd5ee8934168f2e38ea4ebc43fe58dbea8680716de3517d8d2a555ac52a690f7
|
7
|
+
data.tar.gz: a53a8a807d6f4e2c244e9466743e9fbe2d8949d462a50cc9d82d85fbff6c1b89c852ddb088c4ca74042e3f3775ddbdfdf9955762341943821053aabe95e094af
|
data/README.md
CHANGED
@@ -163,7 +163,7 @@ moesif_options['mask_data'] = Proc.new { |event_model|
|
|
163
163
|
|
164
164
|
```
|
165
165
|
|
166
|
-
For details for the spec of event model, please see the [moesifapi-ruby
|
166
|
+
For details for the spec of event model, please see the [moesifapi-ruby](https://github.com/Moesif/moesifapi-ruby)
|
167
167
|
|
168
168
|
#### __`skip`__
|
169
169
|
|
@@ -187,10 +187,97 @@ For details for the spec of event model, please see the [Moesif Ruby API Documen
|
|
187
187
|
|
188
188
|
Optional. Boolean. Default false. If true, it will print out debug messages. In debug mode, the processing is not done in backend thread.
|
189
189
|
|
190
|
+
#### __`capture_outoing_requests`__
|
191
|
+
Optional. boolean, Default `false`. Set to `true` to capture all outgoing API calls from your app to third parties like Stripe, Github or to your own dependencies while using [Net::HTTP](https://ruby-doc.org/stdlib-2.6.3/libdoc/net/http/rdoc/Net/HTTP.html) package. The options below is applied to outgoing API calls. When the request is outgoing, for options functions that take request and response as input arguments, the request and response objects passed in are [Request](https://www.rubydoc.info/stdlib/net/Net/HTTPRequest) request and [Response](https://www.rubydoc.info/stdlib/net/Net/HTTPResponse) response objects.
|
192
|
+
|
193
|
+
|
194
|
+
##### __`identify_user_outgoing`__
|
195
|
+
|
196
|
+
Optional.
|
197
|
+
identify_user_outgoing is a Proc that takes request and response as arguments and returns a user_id string. This helps us attribute requests to unique users. Even though Moesif can automatically retrieve the user_id without this, this is highly recommended to ensure accurate attribution.
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
|
201
|
+
moesif_options['identify_user_outgoing'] = Proc.new { |request, response|
|
202
|
+
|
203
|
+
#snip
|
204
|
+
|
205
|
+
'the_user_id'
|
206
|
+
}
|
207
|
+
|
208
|
+
```
|
209
|
+
|
210
|
+
##### __`get_metadata_outgoing`__
|
211
|
+
|
212
|
+
Optional.
|
213
|
+
get_metadata_outgoing is a Proc that takes request and response as arguments and returns a Hash that is
|
214
|
+
representation of a JSON object. This allows you to attach any
|
215
|
+
metadata to this event.
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
|
219
|
+
moesif_options['get_metadata_outgoing'] = Proc.new { |request, response|
|
220
|
+
|
221
|
+
#snip
|
222
|
+
value = {
|
223
|
+
'foo' => 'abc',
|
224
|
+
'bar' => '123'
|
225
|
+
}
|
226
|
+
|
227
|
+
value
|
228
|
+
}
|
229
|
+
```
|
230
|
+
|
231
|
+
##### __`identify_session_outgoing`__
|
232
|
+
|
233
|
+
Optional. A Proc that takes request, response and returns a string.
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
|
237
|
+
moesif_options['identify_session_outgoing'] = Proc.new { |request, response|
|
238
|
+
|
239
|
+
#snip
|
240
|
+
|
241
|
+
'the_session_token'
|
242
|
+
}
|
243
|
+
|
244
|
+
```
|
245
|
+
|
246
|
+
##### __`skip_outgoing`__
|
247
|
+
|
248
|
+
Optional. A Proc that takes request, response and returns a boolean. If `true` would skip sending the particular event.
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
|
252
|
+
moesif_options['skip_outgoing'] = Proc.new{ |request, response|
|
253
|
+
|
254
|
+
#snip
|
255
|
+
|
256
|
+
false
|
257
|
+
}
|
258
|
+
|
259
|
+
```
|
260
|
+
|
261
|
+
##### __`mask_data_outgoing`__
|
262
|
+
|
263
|
+
Optional. A Proc that takes event_model as an argument and returns event_model.
|
264
|
+
With mask_data_outgoing, you can make modifications to headers or body of the event before it is sent to Moesif.
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
|
268
|
+
moesif_options['mask_data_outgoing'] = Proc.new { |event_model|
|
269
|
+
|
270
|
+
#snip
|
271
|
+
|
272
|
+
event_model
|
273
|
+
}
|
274
|
+
|
275
|
+
```
|
276
|
+
|
190
277
|
## Update User
|
191
278
|
|
192
279
|
### update_user method
|
193
|
-
A method is attached to the moesif middleware object to update the
|
280
|
+
A method is attached to the moesif middleware object to update the user profile or metadata.
|
194
281
|
The metadata field can be any custom data you want to set on the user. The `user_id` field is required.
|
195
282
|
|
196
283
|
```ruby
|
@@ -232,6 +319,48 @@ user_models << user_model_A << user_model_B
|
|
232
319
|
response = MoesifRack::MoesifMiddleware.new(@app, @options).update_users_batch(user_models)
|
233
320
|
```
|
234
321
|
|
322
|
+
## Update Company
|
323
|
+
|
324
|
+
### update_company method
|
325
|
+
A method is attached to the moesif middleware object to update the company profile or metadata.
|
326
|
+
The metadata field can be any custom data you want to set on the company. The `company_id` field is required.
|
327
|
+
|
328
|
+
```ruby
|
329
|
+
metadata = JSON.parse('{'\
|
330
|
+
'"email": "testrubyapi@company.com",'\
|
331
|
+
'"name": "ruby api company",'\
|
332
|
+
'"custom": "testdata"'\
|
333
|
+
'}')
|
334
|
+
|
335
|
+
company_model = { "company_id" => "testrubyapicompany",
|
336
|
+
"metadata" => metadata }
|
337
|
+
|
338
|
+
update_company = MoesifRack::MoesifMiddleware.new(@app, @options).update_company(company_model)
|
339
|
+
```
|
340
|
+
|
341
|
+
### update_companies_batch method
|
342
|
+
A method is attached to the moesif middleware object to update the companies profile or metadata in batch.
|
343
|
+
The metadata field can be any custom data you want to set on the company. The `company_id` field is required.
|
344
|
+
|
345
|
+
```ruby
|
346
|
+
metadata = JSON.parse('{'\
|
347
|
+
'"email": "testrubyapi@user.com",'\
|
348
|
+
'"name": "ruby api user",'\
|
349
|
+
'"custom": "testdata"'\
|
350
|
+
'}')
|
351
|
+
|
352
|
+
company_models = []
|
353
|
+
|
354
|
+
company_model_A = { "company_id" => "testrubyapicompany",
|
355
|
+
"metadata" => metadata }
|
356
|
+
|
357
|
+
company_model_B = { "company_id" => "testrubyapicompany1",
|
358
|
+
"metadata" => metadata }
|
359
|
+
|
360
|
+
company_models << company_model_A << company_model_B
|
361
|
+
response = MoesifRack::MoesifMiddleware.new(@app, @options).update_companies_batch(company_models)
|
362
|
+
```
|
363
|
+
|
235
364
|
## How to test
|
236
365
|
|
237
366
|
1. Manually clone the git repo
|
@@ -239,12 +368,11 @@ response = MoesifRack::MoesifMiddleware.new(@app, @options).update_users_batch(u
|
|
239
368
|
3. Invoke 'gem install moesif_rack'
|
240
369
|
4. Add your own application id to 'test/moesif_rack_test.rb'. You can find your Application Id from [_Moesif Dashboard_](https://www.moesif.com/) -> _Top Right Menu_ -> _Installation_
|
241
370
|
5. Invoke 'ruby test/moesif_rack_test.rb'
|
371
|
+
6. Invoke 'ruby -I test test/moesif_rack_test.rb -n test_capture_outgoing' to test capturing outgoing API calls from your app to third parties like Stripe, Github or to your own dependencies.
|
242
372
|
|
243
373
|
## Example Code
|
244
374
|
|
245
|
-
[Moesif Rack Example](https://github.com/Moesif/moesif-rack-example) is an
|
246
|
-
example of Moesif Rack applied to an Rail application. Please check it out
|
247
|
-
for reference.
|
375
|
+
[Moesif Rack Example](https://github.com/Moesif/moesif-rack-example) is an example of Moesif Rack applied to an Rail application. Please check it out for reference.
|
248
376
|
|
249
377
|
## Other integrations
|
250
378
|
|
@@ -3,6 +3,8 @@ require 'json'
|
|
3
3
|
require 'time'
|
4
4
|
require 'base64'
|
5
5
|
require_relative './client_ip.rb'
|
6
|
+
require_relative './update_user.rb'
|
7
|
+
require_relative './update_company.rb'
|
6
8
|
|
7
9
|
module MoesifRack
|
8
10
|
|
@@ -28,6 +30,14 @@ module MoesifRack
|
|
28
30
|
if not @sampling_percentage.is_a? Numeric
|
29
31
|
raise "Sampling Percentage should be a number"
|
30
32
|
end
|
33
|
+
@capture_outoing_requests = options['capture_outoing_requests']
|
34
|
+
if @capture_outoing_requests
|
35
|
+
if @debug
|
36
|
+
puts 'Start Capturing outgoing requests'
|
37
|
+
end
|
38
|
+
require_relative '../../moesif_capture_outgoing/httplog.rb'
|
39
|
+
MoesifCaptureOutgoing.start_capture_outgoing(options)
|
40
|
+
end
|
31
41
|
end
|
32
42
|
|
33
43
|
def get_config(cached_config_etag)
|
@@ -60,58 +70,19 @@ module MoesifRack
|
|
60
70
|
end
|
61
71
|
|
62
72
|
def update_user(user_profile)
|
63
|
-
|
64
|
-
if user_profile.key?("user_id")
|
65
|
-
begin
|
66
|
-
@api_controller.update_user(MoesifApi::UserModel.from_hash(user_profile))
|
67
|
-
if @debug
|
68
|
-
puts "Update User Successfully"
|
69
|
-
end
|
70
|
-
rescue MoesifApi::APIException => e
|
71
|
-
if e.response_code.between?(401, 403)
|
72
|
-
puts "Unathorized accesss updating user to Moesif. Please verify your Application Id."
|
73
|
-
end
|
74
|
-
if @debug
|
75
|
-
puts "Error updating user to Moesif, with status code: "
|
76
|
-
puts e.response_code
|
77
|
-
end
|
78
|
-
end
|
79
|
-
else
|
80
|
-
puts "To update an user, an user_id field is required"
|
81
|
-
end
|
82
|
-
else
|
83
|
-
puts "Expecting the input to be of the type - dictionary while updating user"
|
84
|
-
end
|
73
|
+
UserHelper.new.update_user(@api_controller, @debug, user_profile)
|
85
74
|
end
|
86
75
|
|
87
76
|
def update_users_batch(user_profiles)
|
88
|
-
|
89
|
-
|
90
|
-
if user.key?("user_id")
|
91
|
-
userModels << MoesifApi::UserModel.from_hash(user)
|
92
|
-
else
|
93
|
-
puts "To update an user, an user_id field is required"
|
94
|
-
end
|
95
|
-
}
|
77
|
+
UserHelper.new.update_users_batch(@api_controller, @debug, user_profiles)
|
78
|
+
end
|
96
79
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
rescue MoesifApi::APIException => e
|
104
|
-
if e.response_code.between?(401, 403)
|
105
|
-
puts "Unathorized accesss updating user to Moesif. Please verify your Application Id."
|
106
|
-
end
|
107
|
-
if @debug
|
108
|
-
puts "Error updating user to Moesif, with status code: "
|
109
|
-
puts e.response_code
|
110
|
-
end
|
111
|
-
end
|
112
|
-
else
|
113
|
-
puts "Expecting the input to be of the type - Array of hashes while updating users in batch"
|
114
|
-
end
|
80
|
+
def update_company(company_profile)
|
81
|
+
CompanyHelper.new.update_company(@api_controller, @debug, company_profile)
|
82
|
+
end
|
83
|
+
|
84
|
+
def update_companies_batch(company_profiles)
|
85
|
+
CompanyHelper.new.update_companies_batch(@api_controller, @debug, company_profiles)
|
115
86
|
end
|
116
87
|
|
117
88
|
def call env
|
@@ -185,7 +156,9 @@ module MoesifRack
|
|
185
156
|
# Add Transaction Id to Request Header
|
186
157
|
req_headers["X-Moesif-Transaction-Id"] = transaction_id
|
187
158
|
# Filter out the old key as HTTP Headers case are not preserved
|
188
|
-
|
159
|
+
if req_headers.key?("X-MOESIF_TRANSACTION_ID")
|
160
|
+
req_headers = req_headers.except("X-MOESIF_TRANSACTION_ID")
|
161
|
+
end
|
189
162
|
end
|
190
163
|
|
191
164
|
# Add Transaction Id to the Response Header
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class CompanyHelper
|
2
|
+
def update_company(api_controller, debug, company_profile)
|
3
|
+
if company_profile.any?
|
4
|
+
if company_profile.key?("company_id")
|
5
|
+
begin
|
6
|
+
api_controller.update_company(MoesifApi::CompanyModel.from_hash(company_profile))
|
7
|
+
if debug
|
8
|
+
puts "Update Company Successfully"
|
9
|
+
end
|
10
|
+
rescue MoesifApi::APIException => e
|
11
|
+
if e.response_code.between?(401, 403)
|
12
|
+
puts "Unathorized accesss updating company to Moesif. Please verify your Application Id."
|
13
|
+
end
|
14
|
+
if debug
|
15
|
+
puts "Error updating company to Moesif, with status code: "
|
16
|
+
puts e.response_code
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
puts "To update a company, a company_id field is required"
|
21
|
+
end
|
22
|
+
else
|
23
|
+
puts "Expecting the input to be of the type - dictionary while updating user"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_companies_batch(api_controller, debug, company_profiles)
|
28
|
+
companyModels = []
|
29
|
+
company_profiles.each { |company|
|
30
|
+
if company.key?("company_id")
|
31
|
+
companyModels << MoesifApi::CompanyModel.from_hash(company)
|
32
|
+
else
|
33
|
+
puts "To update a company, a company_id field is required"
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
if companyModels.any?
|
38
|
+
begin
|
39
|
+
api_controller.update_companies_batch(companyModels)
|
40
|
+
if debug
|
41
|
+
puts "Update Companies Successfully"
|
42
|
+
end
|
43
|
+
rescue MoesifApi::APIException => e
|
44
|
+
if e.response_code.between?(401, 403)
|
45
|
+
puts "Unathorized accesss updating companies to Moesif. Please verify your Application Id."
|
46
|
+
end
|
47
|
+
if debug
|
48
|
+
puts "Error updating companies to Moesif, with status code: "
|
49
|
+
puts e.response_code
|
50
|
+
end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
puts "Expecting the input to be of the type - Array of hashes while updating companies in batch"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class UserHelper
|
2
|
+
def update_user(api_controller, debug, user_profile)
|
3
|
+
if user_profile.any?
|
4
|
+
if user_profile.key?("user_id")
|
5
|
+
begin
|
6
|
+
api_controller.update_user(MoesifApi::UserModel.from_hash(user_profile))
|
7
|
+
if debug
|
8
|
+
puts "Update User Successfully"
|
9
|
+
end
|
10
|
+
rescue MoesifApi::APIException => e
|
11
|
+
if e.response_code.between?(401, 403)
|
12
|
+
puts "Unathorized accesss updating user to Moesif. Please verify your Application Id."
|
13
|
+
end
|
14
|
+
if debug
|
15
|
+
puts "Error updating user to Moesif, with status code: "
|
16
|
+
puts e.response_code
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
puts "To update an user, an user_id field is required"
|
21
|
+
end
|
22
|
+
else
|
23
|
+
puts "Expecting the input to be of the type - dictionary while updating user"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_users_batch(api_controller, debug, user_profiles)
|
28
|
+
userModels = []
|
29
|
+
user_profiles.each { |user|
|
30
|
+
if user.key?("user_id")
|
31
|
+
userModels << MoesifApi::UserModel.from_hash(user)
|
32
|
+
else
|
33
|
+
puts "To update an user, an user_id field is required"
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
if userModels.any?
|
38
|
+
begin
|
39
|
+
api_controller.update_users_batch(userModels)
|
40
|
+
if debug
|
41
|
+
puts "Update Users Successfully"
|
42
|
+
end
|
43
|
+
rescue MoesifApi::APIException => e
|
44
|
+
if e.response_code.between?(401, 403)
|
45
|
+
puts "Unathorized accesss updating user to Moesif. Please verify your Application Id."
|
46
|
+
end
|
47
|
+
if debug
|
48
|
+
puts "Error updating user to Moesif, with status code: "
|
49
|
+
puts e.response_code
|
50
|
+
end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
puts "Expecting the input to be of the type - Array of hashes while updating users in batch"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module Net
|
4
|
+
class HTTP
|
5
|
+
alias orig_request request unless method_defined?(:orig_request)
|
6
|
+
|
7
|
+
def request(request, body = nil, &block)
|
8
|
+
|
9
|
+
# Request Start Time
|
10
|
+
request_time = Time.now.utc.iso8601
|
11
|
+
|
12
|
+
# URL
|
13
|
+
url = "https://#{@address}#{request.path}"
|
14
|
+
|
15
|
+
# Response
|
16
|
+
@response = orig_request(request, body, &block)
|
17
|
+
|
18
|
+
# Response Time
|
19
|
+
response_time = Time.now.utc.iso8601
|
20
|
+
|
21
|
+
# Log Event to Moesif
|
22
|
+
if started?
|
23
|
+
MoesifCaptureOutgoing.call(url, request, request_time, @response, response_time)
|
24
|
+
end
|
25
|
+
|
26
|
+
@response
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'rack'
|
3
|
+
require 'moesif_api'
|
4
|
+
require 'json'
|
5
|
+
require 'base64'
|
6
|
+
|
7
|
+
module MoesifCaptureOutgoing
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def start_capture_outgoing(options)
|
12
|
+
@moesif_options = options
|
13
|
+
if not @moesif_options['application_id']
|
14
|
+
raise 'application_id required for Moesif Middleware'
|
15
|
+
end
|
16
|
+
@api_client = MoesifApi::MoesifAPIClient.new(@moesif_options['application_id'])
|
17
|
+
@api_controller = @api_client.api
|
18
|
+
@debug = @moesif_options['debug']
|
19
|
+
@get_metadata_outgoing = @moesif_options['get_metadata_outgoing']
|
20
|
+
@identify_user_outgoing = @moesif_options['identify_user_outgoing']
|
21
|
+
@identify_session_outgoing = @moesif_options['identify_session_outgoing']
|
22
|
+
@skip_outgoing = options['skip_outgoing']
|
23
|
+
@mask_data_outgoing = options['mask_data_outgoing']
|
24
|
+
end
|
25
|
+
|
26
|
+
def call (url, request, request_time, response, response_time)
|
27
|
+
send_moesif_event(url, request, request_time, response, response_time)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_response_body(response)
|
31
|
+
body = response.respond_to?(:body) ? response.body : response
|
32
|
+
body = body.inject("") { |i, a| i << a } if body.respond_to?(:each)
|
33
|
+
body.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def transform_response_code(response_code_name)
|
37
|
+
Rack::Utils::HTTP_STATUS_CODES.detect { |_k, v| v.to_s.casecmp(response_code_name.to_s).zero? }.first
|
38
|
+
end
|
39
|
+
|
40
|
+
def send_moesif_event(url, request, request_time, response, response_time)
|
41
|
+
|
42
|
+
if url.downcase.include? "moesif"
|
43
|
+
if @debug
|
44
|
+
puts "Skip sending as it is moesif Event"
|
45
|
+
end
|
46
|
+
else
|
47
|
+
response.code = transform_response_code(response.code) if response.code.is_a?(Symbol)
|
48
|
+
|
49
|
+
# Request Body
|
50
|
+
req_body_string = request.body.nil? || request.body.empty? ? nil : request.body
|
51
|
+
req_body_transfer_encoding = nil
|
52
|
+
if req_body_string && req_body_string.length != 0
|
53
|
+
begin
|
54
|
+
req_body = JSON.parse(req_body_string)
|
55
|
+
rescue
|
56
|
+
req_body = Base64.encode64(req_body_string)
|
57
|
+
req_body_transfer_encoding = 'base64'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Response Body and encoding
|
62
|
+
rsp_body_string = get_response_body(response.body)
|
63
|
+
rsp_body_transfer_encoding = nil
|
64
|
+
|
65
|
+
if rsp_body_string && rsp_body_string.length != 0
|
66
|
+
begin
|
67
|
+
rsp_body = JSON.parse(rsp_body_string)
|
68
|
+
rescue
|
69
|
+
rsp_body = Base64.encode64(rsp_body_string)
|
70
|
+
rsp_body_transfer_encoding = 'base64'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Event Request
|
75
|
+
event_req = MoesifApi::EventRequestModel.new()
|
76
|
+
event_req.time = request_time
|
77
|
+
event_req.uri = url
|
78
|
+
event_req.verb = request.method.to_s.upcase
|
79
|
+
event_req.headers = request.each_header.collect.to_h
|
80
|
+
event_req.api_version = nil
|
81
|
+
event_req.body = req_body_string
|
82
|
+
event_req.transfer_encoding = req_body_transfer_encoding
|
83
|
+
|
84
|
+
# Event Response
|
85
|
+
event_rsp = MoesifApi::EventResponseModel.new()
|
86
|
+
event_rsp.time = response_time
|
87
|
+
event_rsp.status = response.code.to_i
|
88
|
+
event_rsp.headers = response.each_header.collect.to_h
|
89
|
+
event_rsp.body = rsp_body_string
|
90
|
+
event_rsp.transfer_encoding = rsp_body_transfer_encoding
|
91
|
+
|
92
|
+
# Prepare Event Model
|
93
|
+
event_model = MoesifApi::EventModel.new()
|
94
|
+
event_model.request = event_req
|
95
|
+
event_model.response = event_rsp
|
96
|
+
|
97
|
+
# Metadata for Outgoing Request
|
98
|
+
if @get_metadata_outgoing
|
99
|
+
if @debug
|
100
|
+
puts "calling get_metadata_outgoing proc"
|
101
|
+
end
|
102
|
+
event_model.metadata = @get_metadata_outgoing.call(request, response)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Identify User
|
106
|
+
if @identify_user_outgoing
|
107
|
+
if @debug
|
108
|
+
puts "calling identify_user_outgoing proc"
|
109
|
+
end
|
110
|
+
event_model.user_id = @identify_user_outgoing.call(request, response)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Session Token
|
114
|
+
if @identify_session_outgoing
|
115
|
+
if @debug
|
116
|
+
puts "calling identify_session_outgoing proc"
|
117
|
+
end
|
118
|
+
event_model.session_token = @identify_session_outgoing.call(request, response)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Skip Outgoing Request
|
122
|
+
should_skip = false
|
123
|
+
|
124
|
+
if @skip_outgoing
|
125
|
+
if @skip_outgoing.call(request, response)
|
126
|
+
should_skip = true;
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
if !should_skip
|
131
|
+
|
132
|
+
# Mask outgoing Event
|
133
|
+
if @mask_data_outgoing
|
134
|
+
if @debug
|
135
|
+
puts "calling mask_data_outgoing proc"
|
136
|
+
end
|
137
|
+
event_model = @mask_data_outgoing.call(event_model)
|
138
|
+
end
|
139
|
+
|
140
|
+
# Send Event to Moesif
|
141
|
+
begin
|
142
|
+
if @debug
|
143
|
+
puts 'Sending Outgoing Request Data to Moesif'
|
144
|
+
puts event_model.to_json
|
145
|
+
end
|
146
|
+
@api_controller.create_event(event_model)
|
147
|
+
rescue MoesifApi::APIException => e
|
148
|
+
if e.response_code.between?(401, 403)
|
149
|
+
puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
|
150
|
+
end
|
151
|
+
if @debug
|
152
|
+
puts "Error sending event to Moesif, with status code: "
|
153
|
+
puts e.response_code
|
154
|
+
end
|
155
|
+
end
|
156
|
+
else
|
157
|
+
if @debug
|
158
|
+
puts 'Skip sending outgoing request'
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data/test/moesif_rack_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'rack'
|
3
|
+
require 'net/http'
|
3
4
|
require_relative '../lib/moesif_rack'
|
4
5
|
|
5
6
|
class MoesifRackTest < Test::Unit::TestCase
|
@@ -7,10 +8,39 @@ class MoesifRackTest < Test::Unit::TestCase
|
|
7
8
|
@app = ->(env) { [200, { "Content-Type" => "application/json" }, ["{ \"key\": \"value\"}"]]}
|
8
9
|
@options = { 'application_id' => 'Your Application Id',
|
9
10
|
'debug' => true,
|
10
|
-
'disable_transaction_id' => true
|
11
|
+
'disable_transaction_id' => true,
|
12
|
+
'capture_outoing_requests' => true,
|
13
|
+
'get_metadata_outgoing' => Proc.new {|request, response|
|
14
|
+
{
|
15
|
+
'foo' => 'abc',
|
16
|
+
'bar' => '123'
|
17
|
+
}
|
18
|
+
},
|
19
|
+
'identify_user_outgoing' => Proc.new{|request, response|
|
20
|
+
'outgoing_user'
|
21
|
+
},
|
22
|
+
'identify_session_outgoing' => Proc.new{|request, response|
|
23
|
+
'outgoing_session'
|
24
|
+
},
|
25
|
+
'skip_outgoing' => Proc.new{|request, response|
|
26
|
+
false
|
27
|
+
},
|
28
|
+
'mask_data_outgoing' => Proc.new{|event_model|
|
29
|
+
event_model
|
30
|
+
}
|
31
|
+
}
|
11
32
|
@moesif_rack_app = MoesifRack::MoesifMiddleware.new(@app, @options)
|
12
33
|
end
|
13
34
|
|
35
|
+
def test_capture_outgoing
|
36
|
+
url = URI.parse('https://api.github.com')
|
37
|
+
req = Net::HTTP::Get.new(url.to_s)
|
38
|
+
res = Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https') {|http|
|
39
|
+
http.request(req)
|
40
|
+
}
|
41
|
+
assert_not_equal res, nil
|
42
|
+
end
|
43
|
+
|
14
44
|
def test_new_calls_to_middleware
|
15
45
|
assert_instance_of MoesifRack::MoesifMiddleware, @moesif_rack_app
|
16
46
|
end
|
@@ -30,7 +60,7 @@ class MoesifRackTest < Test::Unit::TestCase
|
|
30
60
|
assert_equal response, nil
|
31
61
|
end
|
32
62
|
|
33
|
-
def test_update_users_batch
|
63
|
+
def test_update_users_batch
|
34
64
|
metadata = JSON.parse('{'\
|
35
65
|
'"email": "testrubyapi@user.com",'\
|
36
66
|
'"name": "ruby api user",'\
|
@@ -61,4 +91,38 @@ def test_update_users_batch
|
|
61
91
|
assert_operator 100, :>=, @moesif_rack_app.get_config(nil)
|
62
92
|
end
|
63
93
|
|
94
|
+
def test_update_company
|
95
|
+
metadata = JSON.parse('{'\
|
96
|
+
'"email": "testrubyapi@company.com",'\
|
97
|
+
'"name": "ruby api company",'\
|
98
|
+
'"custom": "testdata"'\
|
99
|
+
'}')
|
100
|
+
|
101
|
+
company_model = { "company_id" => "testrubyapicompany",
|
102
|
+
"metadata" => metadata }
|
103
|
+
|
104
|
+
response = @moesif_rack_app.update_company(company_model)
|
105
|
+
assert_equal response, nil
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_update_companies_batch
|
109
|
+
metadata = JSON.parse('{'\
|
110
|
+
'"email": "testrubyapi@company.com",'\
|
111
|
+
'"name": "ruby api company",'\
|
112
|
+
'"custom": "testdata"'\
|
113
|
+
'}')
|
114
|
+
|
115
|
+
company_models = []
|
116
|
+
|
117
|
+
company_model_A = { "company_id" => "testrubyapicompany",
|
118
|
+
"metadata" => metadata }
|
119
|
+
|
120
|
+
company_model_B = { "company_id" => "testrubyapicompany1",
|
121
|
+
"metadata" => metadata }
|
122
|
+
|
123
|
+
company_models << company_model_A << company_model_B
|
124
|
+
response = @moesif_rack_app.update_companies_batch(company_models)
|
125
|
+
assert_equal response, nil
|
126
|
+
end
|
127
|
+
|
64
128
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moesif_rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moesif, Inc
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-05-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test-unit
|
@@ -31,14 +31,14 @@ dependencies:
|
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: 1.2.
|
34
|
+
version: 1.2.7
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: 1.2.
|
41
|
+
version: 1.2.7
|
42
42
|
description: Collection/Data Ingestion SDK for Rack (also Rails) Middleware / RoR
|
43
43
|
email: xing@moesif.com
|
44
44
|
executables: []
|
@@ -50,6 +50,11 @@ files:
|
|
50
50
|
- lib/moesif_rack.rb
|
51
51
|
- lib/moesif_rack/client_ip.rb
|
52
52
|
- lib/moesif_rack/moesif_middleware.rb
|
53
|
+
- lib/moesif_rack/update_company.rb
|
54
|
+
- lib/moesif_rack/update_user.rb
|
55
|
+
- moesif_capture_outgoing/httplog.rb
|
56
|
+
- moesif_capture_outgoing/httplog/adapters/net_http.rb
|
57
|
+
- moesif_capture_outgoing/httplog/http_log.rb
|
53
58
|
- test/moesif_rack_test.rb
|
54
59
|
homepage: https://moesif.com
|
55
60
|
licenses:
|
@@ -70,8 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
75
|
- !ruby/object:Gem::Version
|
71
76
|
version: '0'
|
72
77
|
requirements: []
|
73
|
-
|
74
|
-
rubygems_version: 2.7.7
|
78
|
+
rubygems_version: 3.0.1
|
75
79
|
signing_key:
|
76
80
|
specification_version: 4
|
77
81
|
summary: moesif_rack
|