moesif_rack 1.4.1 → 1.4.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +5 -5
- data/lib/moesif_rack/app_config.rb +36 -39
- data/lib/moesif_rack/client_ip.rb +2 -2
- data/lib/moesif_rack/helpers.rb +14 -0
- data/lib/moesif_rack/moesif_middleware.rb +97 -70
- data/moesif_capture_outgoing/httplog/http_log.rb +4 -0
- data/test/moesif_rack_test.rb +2 -0
- metadata +28 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4bc6206e2ea8335d66262ad33e0d6ea90a908e60
|
4
|
+
data.tar.gz: f64dedab2b3c2d34fd24a4106417a5610e2bf429
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31dfb4d45baec6e9aa896cb995ed811806b349faa27a79aad737d5974bc5123e43429fb6cae5db96cc5b5f569989d63cb98af26738e8907bf3ac88c965918261
|
7
|
+
data.tar.gz: f25cf1e4ddbbe87e37d8a58a0863d03dbe4e5e2111ae768395d1d2d8a4d58a162016d30f2fcb158e93f95409626be37608d73f76949795fbe583da931bce6afa
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -22,7 +22,7 @@ gem install moesif_rack
|
|
22
22
|
and if you have a `Gemfile` in your project, please add this line to
|
23
23
|
|
24
24
|
```
|
25
|
-
gem 'moesif_rack'
|
25
|
+
gem 'moesif_rack'
|
26
26
|
|
27
27
|
```
|
28
28
|
|
@@ -500,11 +500,11 @@ response = MoesifRack::MoesifMiddleware.new(@app, @options).update_companies_bat
|
|
500
500
|
5. Invoke 'ruby test/moesif_rack_test.rb'
|
501
501
|
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.
|
502
502
|
|
503
|
-
## Example
|
503
|
+
## Example Projects
|
504
504
|
|
505
|
-
[Moesif
|
506
|
-
|
507
|
-
[Moesif Rack Example](https://github.com/Moesif/moesif-rack-example) is an example of Moesif
|
505
|
+
- [Moesif Rails 5 Example](https://github.com/Moesif/moesif-rails5-example) is an example of Moesif with a Ruby on Rails 5 application.
|
506
|
+
- [Moesif Rails 4 Example](https://github.com/Moesif/moesif-rails4-example) is an example of Moesif with a Ruby on Rails 4 application.
|
507
|
+
- [Moesif Rack Example](https://github.com/Moesif/moesif-rack-example) is an example of Moesif applied to a Rack application.
|
508
508
|
|
509
509
|
## Other integrations
|
510
510
|
|
@@ -3,66 +3,71 @@ require 'json'
|
|
3
3
|
require 'time'
|
4
4
|
require 'zlib'
|
5
5
|
require 'stringio'
|
6
|
+
require_relative './helpers.rb'
|
6
7
|
|
7
8
|
class AppConfig
|
8
9
|
|
9
|
-
def
|
10
|
+
def initialize debug
|
11
|
+
@debug = debug
|
12
|
+
@helpers = Helpers.new(debug)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_config(api_controller)
|
10
16
|
# Get Application Config
|
11
17
|
begin
|
12
18
|
config_api_response = api_controller.get_app_config()
|
19
|
+
@helpers.log_debug("new config downloaded")
|
20
|
+
@helpers.log_debug(config_api_response.to_s)
|
13
21
|
return config_api_response
|
14
22
|
rescue MoesifApi::APIException => e
|
15
23
|
if e.response_code.between?(401, 403)
|
16
|
-
|
17
|
-
end
|
18
|
-
if debug
|
19
|
-
puts 'Error getting application configuration, with status code:'
|
20
|
-
puts e.response_code
|
24
|
+
@helpers.log_debug 'Unauthorized access getting application configuration. Please check your Appplication Id.'
|
21
25
|
end
|
26
|
+
@helpers.log_debug 'Error getting application configuration, with status code:'
|
27
|
+
@helpers.log_debug e.response_code
|
28
|
+
rescue => e
|
29
|
+
@helpers.log_debug e.to_s
|
22
30
|
end
|
31
|
+
rescue
|
23
32
|
end
|
24
33
|
|
25
|
-
def parse_configuration(config_api_response
|
34
|
+
def parse_configuration(config_api_response)
|
26
35
|
# Parse configuration object and return Etag, sample rate and last updated time
|
27
36
|
begin
|
28
37
|
# Rails return gzipped compressed response body, so decompressing it and getting JSON response body
|
29
|
-
response_body = decompress_gzip_body(config_api_response
|
38
|
+
response_body = decompress_gzip_body(config_api_response)
|
39
|
+
@helpers.log_debug(response_body.to_s)
|
30
40
|
|
31
41
|
# Check if response body is not nil
|
32
42
|
if !response_body.nil? then
|
33
43
|
# Return Etag, sample rate and last updated time
|
34
|
-
return config_api_response.headers[:x_moesif_config_etag],
|
44
|
+
return response_body, config_api_response.headers[:x_moesif_config_etag], Time.now.utc
|
35
45
|
else
|
36
|
-
|
37
|
-
puts 'Response body is nil, assuming default behavior'
|
38
|
-
end
|
46
|
+
@helpers.log_debug 'Response body is nil, assuming default behavior'
|
39
47
|
# Response body is nil, so assuming default behavior
|
40
48
|
return nil, 100, Time.now.utc
|
41
49
|
end
|
42
50
|
rescue => exception
|
43
|
-
|
44
|
-
|
45
|
-
puts exception.to_s
|
46
|
-
end
|
51
|
+
@helpers.log_debug 'Error while parsing the configuration object, assuming default behavior'
|
52
|
+
@helpers.log_debug exception.to_s
|
47
53
|
# Assuming default behavior
|
48
54
|
return nil, 100, Time.now.utc
|
49
55
|
end
|
50
56
|
end
|
51
57
|
|
52
|
-
def get_sampling_percentage(config_api_response, user_id, company_id
|
58
|
+
def get_sampling_percentage(config_api_response, user_id, company_id)
|
53
59
|
# Get sampling percentage
|
54
60
|
begin
|
55
|
-
# Rails return gzipped compressed response body, so decompressing it and getting JSON response body
|
56
|
-
response_body = decompress_gzip_body(config_api_response, debug)
|
57
|
-
|
58
61
|
# Check if response body is not nil
|
59
|
-
if !
|
60
|
-
|
62
|
+
if !config_api_response.nil? then
|
63
|
+
@helpers.log_debug("Getting sample rate for user #{user_id} company #{company_id}")
|
64
|
+
@helpers.log_debug(config_api_response.to_s)
|
65
|
+
|
61
66
|
# Get user sample rate object
|
62
|
-
user_sample_rate =
|
67
|
+
user_sample_rate = config_api_response.fetch('user_sample_rate', nil)
|
63
68
|
|
64
69
|
# Get company sample rate object
|
65
|
-
company_sample_rate =
|
70
|
+
company_sample_rate = config_api_response.fetch('company_sample_rate', nil)
|
66
71
|
|
67
72
|
# Get sample rate for the user if exist
|
68
73
|
if !user_id.nil? && !user_sample_rate.nil? && user_sample_rate.key?(user_id)
|
@@ -75,22 +80,18 @@ class AppConfig
|
|
75
80
|
end
|
76
81
|
|
77
82
|
# Return sample rate
|
78
|
-
return
|
83
|
+
return config_api_response.fetch('sample_rate', 100)
|
79
84
|
else
|
80
|
-
|
81
|
-
puts 'Assuming default behavior as response body is nil - '
|
82
|
-
end
|
85
|
+
@helpers.log_debug 'Assuming default behavior as response body is nil - '
|
83
86
|
return 100
|
84
87
|
end
|
85
88
|
rescue => exception
|
86
|
-
|
87
|
-
puts 'Error while geting sampling percentage, assuming default behavior'
|
88
|
-
end
|
89
|
+
@helpers.log_debug 'Error while geting sampling percentage, assuming default behavior'
|
89
90
|
return 100
|
90
91
|
end
|
91
92
|
end
|
92
93
|
|
93
|
-
def decompress_gzip_body(config_api_response
|
94
|
+
def decompress_gzip_body(config_api_response)
|
94
95
|
# Decompress gzip response body
|
95
96
|
begin
|
96
97
|
# Check if the content-encoding header exist and is of type zip
|
@@ -105,16 +106,12 @@ class AppConfig
|
|
105
106
|
# Return the parsed body
|
106
107
|
return JSON.parse( uncompressed_string )
|
107
108
|
else
|
108
|
-
|
109
|
-
puts 'Content Encoding is of type other than gzip, returning nil'
|
110
|
-
end
|
109
|
+
@helpers.log_debug 'Content Encoding is of type other than gzip, returning nil'
|
111
110
|
return nil
|
112
111
|
end
|
113
112
|
rescue => exception
|
114
|
-
|
115
|
-
|
116
|
-
puts exception.to_s
|
117
|
-
end
|
113
|
+
@helpers.log_debug 'Error while decompressing the response body'
|
114
|
+
@helpers.log_debug exception.to_s
|
118
115
|
return nil
|
119
116
|
end
|
120
117
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
|
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A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
|
6
6
|
# We use !! to convert the return value to a boolean
|
7
7
|
!!(value =~ ipv4 or value=~ ipv6)
|
8
8
|
end
|
@@ -6,6 +6,7 @@ require_relative './client_ip.rb'
|
|
6
6
|
require_relative './app_config.rb'
|
7
7
|
require_relative './update_user.rb'
|
8
8
|
require_relative './update_company.rb'
|
9
|
+
require_relative './helpers.rb'
|
9
10
|
require 'zlib'
|
10
11
|
require 'stringio'
|
11
12
|
|
@@ -28,30 +29,34 @@ module MoesifRack
|
|
28
29
|
@mask_data = options['mask_data']
|
29
30
|
@skip = options['skip']
|
30
31
|
@debug = options['debug']
|
31
|
-
@app_config = AppConfig.new
|
32
|
-
@
|
32
|
+
@app_config = AppConfig.new(@debug)
|
33
|
+
@helpers = Helpers.new(@debug)
|
34
|
+
@config = @app_config.get_config(@api_controller)
|
33
35
|
@config_etag = nil
|
34
|
-
@
|
35
|
-
@
|
36
|
+
@last_config_download_time = Time.now.utc
|
37
|
+
@last_worker_run = Time.now.utc
|
36
38
|
@config_dict = Hash.new
|
37
39
|
@disable_transaction_id = options['disable_transaction_id'] || false
|
38
40
|
@log_body = options.fetch('log_body', true)
|
41
|
+
@batch_size = options['batch_size'] || 25
|
42
|
+
@batch_max_time = options['batch_max_time'] || 2
|
43
|
+
@events_queue = Queue.new
|
44
|
+
@event_response_config_etag = nil
|
45
|
+
start_worker()
|
46
|
+
|
39
47
|
begin
|
40
|
-
|
41
|
-
|
48
|
+
new_config = @app_config.get_config(@api_controller)
|
49
|
+
if !new_config.nil?
|
50
|
+
@config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
|
42
51
|
end
|
43
52
|
rescue => exception
|
44
|
-
|
45
|
-
|
46
|
-
puts exception.to_s
|
47
|
-
end
|
53
|
+
@helpers.log_debug 'Error while parsing application configuration on initialization'
|
54
|
+
@helpers.log_debug exception.to_s
|
48
55
|
end
|
49
56
|
@capture_outoing_requests = options['capture_outoing_requests']
|
50
57
|
@capture_outgoing_requests = options['capture_outgoing_requests']
|
51
58
|
if @capture_outoing_requests || @capture_outgoing_requests
|
52
|
-
|
53
|
-
puts 'Start Capturing outgoing requests'
|
54
|
-
end
|
59
|
+
@helpers.log_debug 'Start Capturing outgoing requests'
|
55
60
|
require_relative '../../moesif_capture_outgoing/httplog.rb'
|
56
61
|
MoesifCaptureOutgoing.start_capture_outgoing(options)
|
57
62
|
end
|
@@ -89,6 +94,12 @@ module MoesifRack
|
|
89
94
|
return Base64.encode64(body), 'base64'
|
90
95
|
end
|
91
96
|
|
97
|
+
def @helpers.log_debug(message)
|
98
|
+
if @debug
|
99
|
+
puts("#{Time.now.to_s} [Moesif Middleware] PID #{Process.pid} TID #{Thread.current.object_id} #{message}")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
92
103
|
def parse_body(body, headers)
|
93
104
|
begin
|
94
105
|
if start_with_json(body)
|
@@ -106,12 +117,45 @@ module MoesifRack
|
|
106
117
|
return parsed_body, transfer_encoding
|
107
118
|
end
|
108
119
|
|
120
|
+
def start_worker
|
121
|
+
Thread::new do
|
122
|
+
@last_worker_run = Time.now.utc
|
123
|
+
loop do
|
124
|
+
begin
|
125
|
+
until @events_queue.empty? do
|
126
|
+
batch_events = []
|
127
|
+
until batch_events.size == @batch_size || @events_queue.empty? do
|
128
|
+
batch_events << @events_queue.pop
|
129
|
+
end
|
130
|
+
@helpers.log_debug("Sending #{batch_events.size.to_s} events to Moesif")
|
131
|
+
event_api_response = @api_controller.create_events_batch(batch_events)
|
132
|
+
@event_response_config_etag = event_api_response[:x_moesif_config_etag]
|
133
|
+
@helpers.log_debug(event_api_response.to_s)
|
134
|
+
@helpers.log_debug("Events successfully sent to Moesif")
|
135
|
+
end
|
136
|
+
|
137
|
+
if @events_queue.empty?
|
138
|
+
@helpers.log_debug("No events to read from the queue")
|
139
|
+
end
|
140
|
+
|
141
|
+
sleep @batch_max_time
|
142
|
+
rescue MoesifApi::APIException => e
|
143
|
+
if e.response_code.between?(401, 403)
|
144
|
+
puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
|
145
|
+
@helpers.log_debug(e.to_s)
|
146
|
+
end
|
147
|
+
@helpers.log_debug("Error sending event to Moesif, with status code #{e.response_code.to_s}")
|
148
|
+
rescue => e
|
149
|
+
@helpers.log_debug(e.to_s)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
109
155
|
def call env
|
110
156
|
start_time = Time.now.utc.iso8601
|
111
157
|
|
112
|
-
|
113
|
-
puts 'inside moesif middleware'
|
114
|
-
end
|
158
|
+
@helpers.log_debug('Calling Moesif middleware')
|
115
159
|
|
116
160
|
status, headers, body = @app.call env
|
117
161
|
end_time = Time.now.utc.iso8601
|
@@ -206,89 +250,71 @@ module MoesifRack
|
|
206
250
|
event_model.direction = "Incoming"
|
207
251
|
|
208
252
|
if @identify_user
|
209
|
-
|
210
|
-
puts "calling identify user proc"
|
211
|
-
end
|
253
|
+
@helpers.log_debug "calling identify user proc"
|
212
254
|
event_model.user_id = @identify_user.call(env, headers, body)
|
213
255
|
end
|
214
256
|
|
215
257
|
if @identify_company
|
216
|
-
|
217
|
-
puts "calling identify company proc"
|
218
|
-
end
|
258
|
+
@helpers.log_debug "calling identify company proc"
|
219
259
|
event_model.company_id = @identify_company.call(env, headers, body)
|
220
260
|
end
|
221
261
|
|
222
262
|
if @get_metadata
|
223
|
-
|
224
|
-
puts "calling get_metadata proc"
|
225
|
-
end
|
263
|
+
@helpers.log_debug "calling get_metadata proc"
|
226
264
|
event_model.metadata = @get_metadata.call(env, headers, body)
|
227
265
|
end
|
228
266
|
|
229
267
|
if @identify_session
|
230
|
-
|
231
|
-
puts "calling identify session proc"
|
232
|
-
end
|
268
|
+
@helpers.log_debug "calling identify session proc"
|
233
269
|
event_model.session_token = @identify_session.call(env, headers, body)
|
234
270
|
end
|
235
271
|
if @mask_data
|
236
|
-
|
237
|
-
puts "calling mask_data proc"
|
238
|
-
end
|
272
|
+
@helpers.log_debug "calling mask_data proc"
|
239
273
|
event_model = @mask_data.call(event_model)
|
240
274
|
end
|
241
275
|
|
242
|
-
|
243
|
-
|
244
|
-
puts event_model.to_json
|
245
|
-
end
|
276
|
+
@helpers.log_debug "sending data to moesif"
|
277
|
+
@helpers.log_debug event_model.to_json
|
246
278
|
# Perform the API call through the SDK function
|
247
279
|
begin
|
248
|
-
|
280
|
+
random_percentage = Random.rand(0.00..100.00)
|
249
281
|
|
250
282
|
begin
|
251
|
-
|
283
|
+
sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id)
|
284
|
+
@helpers.log_debug "Using sample rate #{sampling_percentage}"
|
252
285
|
rescue => exception
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
end
|
257
|
-
@sampling_percentage = 100
|
286
|
+
@helpers.log_debug 'Error while getting sampling percentage, assuming default behavior'
|
287
|
+
@helpers.log_debug exception.to_s
|
288
|
+
sampling_percentage = 100
|
258
289
|
end
|
259
290
|
|
260
|
-
if
|
261
|
-
event_model.weight = @app_config.calculate_weight(
|
262
|
-
|
263
|
-
|
291
|
+
if sampling_percentage > random_percentage
|
292
|
+
event_model.weight = @app_config.calculate_weight(sampling_percentage)
|
293
|
+
# Add Event to the queue
|
294
|
+
@events_queue << event_model
|
295
|
+
@helpers.log_debug("Event added to the queue ")
|
296
|
+
if Time.now.utc > (@last_config_download_time + 60)
|
297
|
+
start_worker()
|
298
|
+
end
|
264
299
|
|
265
|
-
if
|
300
|
+
if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > (@last_config_download_time + 300)
|
266
301
|
begin
|
267
|
-
|
268
|
-
|
302
|
+
new_config = @app_config.get_config(@api_controller)
|
303
|
+
if !new_config.nil?
|
304
|
+
@config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
|
305
|
+
end
|
306
|
+
|
269
307
|
rescue => exception
|
270
|
-
|
271
|
-
|
272
|
-
puts exception.to_s
|
273
|
-
end
|
308
|
+
@helpers.log_debug 'Error while updating the application configuration'
|
309
|
+
@helpers.log_debug exception.to_s
|
274
310
|
end
|
275
311
|
end
|
276
|
-
if @debug
|
277
|
-
puts("Event successfully sent to Moesif")
|
278
|
-
end
|
279
312
|
else
|
280
|
-
|
281
|
-
puts("Skipped Event due to sampling percentage: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
|
282
|
-
end
|
283
|
-
end
|
284
|
-
rescue MoesifApi::APIException => e
|
285
|
-
if e.response_code.between?(401, 403)
|
286
|
-
puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
|
287
|
-
end
|
288
|
-
if @debug
|
289
|
-
puts "Error sending event to Moesif, with status code: "
|
290
|
-
puts e.response_code
|
313
|
+
@helpers.log_debug("Skipped Event due to sampling percentage: " + sampling_percentage.to_s + " and random percentage: " + random_percentage .to_s)
|
291
314
|
end
|
315
|
+
rescue => exception
|
316
|
+
@helpers.log_debug "Error adding event to the queue "
|
317
|
+
@helpers.log_debug exception.to_s
|
292
318
|
end
|
293
319
|
|
294
320
|
end
|
@@ -302,10 +328,11 @@ module MoesifRack
|
|
302
328
|
end
|
303
329
|
|
304
330
|
if !should_skip
|
305
|
-
|
331
|
+
begin
|
306
332
|
process_send.call
|
307
|
-
|
308
|
-
|
333
|
+
rescue => exception
|
334
|
+
@helpers.log_debug 'Error while logging event - '
|
335
|
+
@helpers.log_debug exception.to_s
|
309
336
|
end
|
310
337
|
end
|
311
338
|
|
data/test/moesif_rack_test.rb
CHANGED
@@ -108,6 +108,8 @@ class MoesifRackTest < Test::Unit::TestCase
|
|
108
108
|
|
109
109
|
def test_log_event
|
110
110
|
response = @moesif_rack_app.call(Rack::MockRequest.env_for("https://acmeinc.com/items/42752/reviews"))
|
111
|
+
# Sleep to allow queue to flush for testing purpose
|
112
|
+
sleep 5
|
111
113
|
assert_equal response, @app.call(nil)
|
112
114
|
end
|
113
115
|
|
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.
|
4
|
+
version: 1.4.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moesif, Inc
|
@@ -9,20 +9,26 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-
|
12
|
+
date: 2020-07-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test-unit
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '3.1'
|
21
|
+
- - ">="
|
19
22
|
- !ruby/object:Gem::Version
|
20
23
|
version: 3.1.5
|
21
|
-
type: :
|
24
|
+
type: :development
|
22
25
|
prerelease: false
|
23
26
|
version_requirements: !ruby/object:Gem::Requirement
|
24
27
|
requirements:
|
25
28
|
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '3.1'
|
31
|
+
- - ">="
|
26
32
|
- !ruby/object:Gem::Version
|
27
33
|
version: 3.1.5
|
28
34
|
- !ruby/object:Gem::Dependency
|
@@ -31,15 +37,21 @@ dependencies:
|
|
31
37
|
requirements:
|
32
38
|
- - "~>"
|
33
39
|
- !ruby/object:Gem::Version
|
34
|
-
version: 1.2
|
40
|
+
version: '1.2'
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.2.12
|
35
44
|
type: :runtime
|
36
45
|
prerelease: false
|
37
46
|
version_requirements: !ruby/object:Gem::Requirement
|
38
47
|
requirements:
|
39
48
|
- - "~>"
|
40
49
|
- !ruby/object:Gem::Version
|
41
|
-
version: 1.2
|
42
|
-
|
50
|
+
version: '1.2'
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.2.12
|
54
|
+
description: Rack/Rails middleware to log API calls to Moesif API analytics and monitoring
|
43
55
|
email: xing@moesif.com
|
44
56
|
executables: []
|
45
57
|
extensions: []
|
@@ -50,6 +62,7 @@ files:
|
|
50
62
|
- lib/moesif_rack.rb
|
51
63
|
- lib/moesif_rack/app_config.rb
|
52
64
|
- lib/moesif_rack/client_ip.rb
|
65
|
+
- lib/moesif_rack/helpers.rb
|
53
66
|
- lib/moesif_rack/moesif_middleware.rb
|
54
67
|
- lib/moesif_rack/update_company.rb
|
55
68
|
- lib/moesif_rack/update_user.rb
|
@@ -60,7 +73,14 @@ files:
|
|
60
73
|
homepage: https://moesif.com
|
61
74
|
licenses:
|
62
75
|
- Apache-2.0
|
63
|
-
metadata:
|
76
|
+
metadata:
|
77
|
+
bug_tracker_uri: https://github.com/Moesif/moesif-rack/issues
|
78
|
+
changelog_uri: https://github.com/Moesif/moesif-rack/releases
|
79
|
+
documentation_uri: https://www.moesif.com/docs/server-integration/rack/
|
80
|
+
homepage_uri: https://www.moesif.com
|
81
|
+
mailing_list_uri: https://github.com/Moesif/moesif-rack
|
82
|
+
source_code_uri: https://github.com/Moesif/moesif-rack
|
83
|
+
wiki_uri: https://github.com/Moesif/moesif-rack
|
64
84
|
post_install_message:
|
65
85
|
rdoc_options: []
|
66
86
|
require_paths:
|
@@ -77,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
97
|
version: '0'
|
78
98
|
requirements: []
|
79
99
|
rubyforge_project:
|
80
|
-
rubygems_version: 2.
|
100
|
+
rubygems_version: 2.5.2.3
|
81
101
|
signing_key:
|
82
102
|
specification_version: 4
|
83
103
|
summary: moesif_rack
|