moesif_rack 1.4.2 → 1.4.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d8635baa7838edc611616b3fcd19d30c6a8c9034f985b1a48df744eae3fc252
4
- data.tar.gz: bf138ca57419057dafdc088f076e3b632a623cd5d268012e6e67a9ce60372ea9
3
+ metadata.gz: 4433b35813e25a0c20462510ff87fb2bb9849dff3edce0c5857884e1535dadb7
4
+ data.tar.gz: 9710968ac3ddfb6cb075e9d0ab5403d93a2e9b7830f5abb1da434ae7e11f907c
5
5
  SHA512:
6
- metadata.gz: 25e43ffa1f612ed39c3ddcee9a55b29ff8f81ed0f9e7d36651d605232aec4ef3ba55cf2e072bd99e72ce46fccc565591872bb58ddd982d65da86a7305f0f4784
7
- data.tar.gz: '095a38e7f8dc9e4ba2fbf9ffd40dfd2ac01fcd72b5475915266274a807c53b70054f823109648e73319557efc07c61ddfb95f99a5d4586855b3db1f0fed7573a'
6
+ metadata.gz: f3139d61e5b5e086e6543e40dd09ac9e2c4703f92b611ade3525a559589a9b7070afef1421c92da4ef39a653814c5c0b306dc818c43092c11352b933b74ee19f
7
+ data.tar.gz: 83529f4a1bebb1e28a096fd43897e3ebd5357892d64961da7208ba1d0862ddad3ca6f1baaaff5384b0003a25dabc220958fa2a5ccec80905f2e5e87e3b87a774
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2019 Moesif, Inc
1
+ Copyright (c) 2020 Moesif, Inc
2
2
 
3
3
  Licensed under the Apache License, Version 2.0 (the "License");
4
4
  you may not use this file except in compliance with the License.
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', '~> 1.4.2'
25
+ gem 'moesif_rack'
26
26
 
27
27
  ```
28
28
 
@@ -206,7 +206,12 @@ Optional. A Proc that takes env, headers, body and returns a boolean.
206
206
 
207
207
  moesif_options['skip'] = Proc.new { |env, headers, body|
208
208
  # Add your custom code that returns true to skip logging the API call
209
- false
209
+ if env.key?("REQUEST_URI")
210
+ # Skip probes to health page
211
+ env["REQUEST_URI"].include? "/health"
212
+ else
213
+ false
214
+ end
210
215
  }
211
216
 
212
217
  ```
@@ -500,11 +505,11 @@ response = MoesifRack::MoesifMiddleware.new(@app, @options).update_companies_bat
500
505
  5. Invoke 'ruby test/moesif_rack_test.rb'
501
506
  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
507
 
503
- ## Example Code
504
-
505
- [Moesif Rack Example with Rails](https://github.com/Moesif/moesif-rails-example) is an example of Moesif Rack applied to a Rails application.
508
+ ## Example Projects
506
509
 
507
- [Moesif Rack Example](https://github.com/Moesif/moesif-rack-example) is an example of Moesif Rack applied to a Rack application.
510
+ - [Moesif Rails 5 Example](https://github.com/Moesif/moesif-rails5-example) is an example of Moesif with a Ruby on Rails 5 application.
511
+ - [Moesif Rails 4 Example](https://github.com/Moesif/moesif-rails4-example) is an example of Moesif with a Ruby on Rails 4 application.
512
+ - [Moesif Rack Example](https://github.com/Moesif/moesif-rack-example) is an example of Moesif applied to a Rack application.
508
513
 
509
514
  ## Other integrations
510
515
 
@@ -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 get_config(api_controller, debug)
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
- puts 'Unauthorized access getting application configuration. Please check your Appplication Id.'
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, debug)
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, debug)
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], response_body.fetch("sample_rate", 100), Time.now.utc
44
+ return response_body, config_api_response.headers[:x_moesif_config_etag], Time.now.utc
35
45
  else
36
- if debug
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
- if debug
44
- puts 'Error while parsing the configuration object, assuming default behavior'
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, debug)
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 !response_body.nil? then
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 = response_body.fetch('user_sample_rate', nil)
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 = response_body.fetch('company_sample_rate', nil)
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 response_body.fetch('sample_rate', 100)
83
+ return config_api_response.fetch('sample_rate', 100)
79
84
  else
80
- if debug
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
- if debug
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, debug)
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
- if debug
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
- if debug
115
- puts 'Error while decompressing the response body'
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 +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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
@@ -0,0 +1,14 @@
1
+ require 'time'
2
+
3
+ class Helpers
4
+
5
+ def initialize debug
6
+ @debug = debug
7
+ end
8
+
9
+ def log_debug(message)
10
+ if @debug
11
+ puts("#{Time.now.to_s} [Moesif Middleware] PID #{Process.pid} TID #{Thread.current.object_id} #{message}")
12
+ end
13
+ end
14
+ 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,69 +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
- @config = @app_config.get_config(@api_controller, @debug)
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
- @sampling_percentage = 100
35
- @last_updated_time = Time.now.utc
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)
39
41
  @batch_size = options['batch_size'] || 25
42
+ @batch_max_time = options['batch_max_time'] || 2
40
43
  @events_queue = Queue.new
41
44
  @event_response_config_etag = nil
42
-
43
- Thread::new do
44
- loop do
45
- begin
46
- until @events_queue.empty? do
47
- batch_events = []
48
- until batch_events.size == @batch_size || @events_queue.empty? do
49
- batch_events << @events_queue.pop
50
- end
51
- event_api_response = @api_controller.create_events_batch(batch_events)
52
- @event_response_config_etag = event_api_response[:x_moesif_config_etag]
53
- if @debug
54
- puts("Events successfully sent to Moesif")
55
- end
56
- end
57
-
58
- if @events_queue.empty?
59
- if @debug
60
- puts("No events to read from the queue")
61
- end
62
- end
63
-
64
- # Sleep for 5 seconds
65
- sleep 5
66
- rescue MoesifApi::APIException => e
67
- if e.response_code.between?(401, 403)
68
- puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
69
- end
70
- if @debug
71
- puts "Error sending event to Moesif, with status code: "
72
- puts e.response_code
73
- end
74
- end
75
- end
76
- end
45
+ start_worker()
77
46
 
78
47
  begin
79
- if !@config.nil?
80
- @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
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)
81
51
  end
82
52
  rescue => exception
83
- if @debug
84
- puts 'Error while parsing application configuration on initialization'
85
- puts exception.to_s
86
- end
53
+ @helpers.log_debug 'Error while parsing application configuration on initialization'
54
+ @helpers.log_debug exception.to_s
87
55
  end
88
56
  @capture_outoing_requests = options['capture_outoing_requests']
89
57
  @capture_outgoing_requests = options['capture_outgoing_requests']
90
58
  if @capture_outoing_requests || @capture_outgoing_requests
91
- if @debug
92
- puts 'Start Capturing outgoing requests'
93
- end
59
+ @helpers.log_debug 'Start Capturing outgoing requests'
94
60
  require_relative '../../moesif_capture_outgoing/httplog.rb'
95
61
  MoesifCaptureOutgoing.start_capture_outgoing(options)
96
62
  end
@@ -128,9 +94,18 @@ module MoesifRack
128
94
  return Base64.encode64(body), 'base64'
129
95
  end
130
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
+
131
103
  def parse_body(body, headers)
132
104
  begin
133
- if start_with_json(body)
105
+ if (body.instance_of?(Hash) || body.instance_of?(Array))
106
+ parsed_body = body
107
+ transfer_encoding = 'json'
108
+ elsif start_with_json(body)
134
109
  parsed_body = JSON.parse(body)
135
110
  transfer_encoding = 'json'
136
111
  elsif headers.key?('content-encoding') && ((headers['content-encoding'].downcase).include? "gzip")
@@ -145,12 +120,45 @@ module MoesifRack
145
120
  return parsed_body, transfer_encoding
146
121
  end
147
122
 
123
+ def start_worker
124
+ Thread::new do
125
+ @last_worker_run = Time.now.utc
126
+ loop do
127
+ begin
128
+ until @events_queue.empty? do
129
+ batch_events = []
130
+ until batch_events.size == @batch_size || @events_queue.empty? do
131
+ batch_events << @events_queue.pop
132
+ end
133
+ @helpers.log_debug("Sending #{batch_events.size.to_s} events to Moesif")
134
+ event_api_response = @api_controller.create_events_batch(batch_events)
135
+ @event_response_config_etag = event_api_response[:x_moesif_config_etag]
136
+ @helpers.log_debug(event_api_response.to_s)
137
+ @helpers.log_debug("Events successfully sent to Moesif")
138
+ end
139
+
140
+ if @events_queue.empty?
141
+ @helpers.log_debug("No events to read from the queue")
142
+ end
143
+
144
+ sleep @batch_max_time
145
+ rescue MoesifApi::APIException => e
146
+ if e.response_code.between?(401, 403)
147
+ puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
148
+ @helpers.log_debug(e.to_s)
149
+ end
150
+ @helpers.log_debug("Error sending event to Moesif, with status code #{e.response_code.to_s}")
151
+ rescue => e
152
+ @helpers.log_debug(e.to_s)
153
+ end
154
+ end
155
+ end
156
+ end
157
+
148
158
  def call env
149
159
  start_time = Time.now.utc.iso8601
150
160
 
151
- if @debug
152
- puts 'inside moesif middleware'
153
- end
161
+ @helpers.log_debug('Calling Moesif middleware')
154
162
 
155
163
  status, headers, body = @app.call env
156
164
  end_time = Time.now.utc.iso8601
@@ -245,86 +253,71 @@ module MoesifRack
245
253
  event_model.direction = "Incoming"
246
254
 
247
255
  if @identify_user
248
- if @debug
249
- puts "calling identify user proc"
250
- end
256
+ @helpers.log_debug "calling identify user proc"
251
257
  event_model.user_id = @identify_user.call(env, headers, body)
252
258
  end
253
259
 
254
260
  if @identify_company
255
- if @debug
256
- puts "calling identify company proc"
257
- end
261
+ @helpers.log_debug "calling identify company proc"
258
262
  event_model.company_id = @identify_company.call(env, headers, body)
259
263
  end
260
264
 
261
265
  if @get_metadata
262
- if @debug
263
- puts "calling get_metadata proc"
264
- end
266
+ @helpers.log_debug "calling get_metadata proc"
265
267
  event_model.metadata = @get_metadata.call(env, headers, body)
266
268
  end
267
269
 
268
270
  if @identify_session
269
- if @debug
270
- puts "calling identify session proc"
271
- end
271
+ @helpers.log_debug "calling identify session proc"
272
272
  event_model.session_token = @identify_session.call(env, headers, body)
273
273
  end
274
274
  if @mask_data
275
- if @debug
276
- puts "calling mask_data proc"
277
- end
275
+ @helpers.log_debug "calling mask_data proc"
278
276
  event_model = @mask_data.call(event_model)
279
277
  end
280
278
 
281
- if @debug
282
- puts "sending data to moesif"
283
- puts event_model.to_json
284
- end
279
+ @helpers.log_debug "sending data to moesif"
280
+ @helpers.log_debug event_model.to_json
285
281
  # Perform the API call through the SDK function
286
282
  begin
287
- @random_percentage = Random.rand(0.00..100.00)
283
+ random_percentage = Random.rand(0.00..100.00)
288
284
 
289
285
  begin
290
- @sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id, @debug)
286
+ sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id)
287
+ @helpers.log_debug "Using sample rate #{sampling_percentage}"
291
288
  rescue => exception
292
- if @debug
293
- puts 'Error while getting sampling percentage, assuming default behavior'
294
- puts exception.to_s
295
- end
296
- @sampling_percentage = 100
289
+ @helpers.log_debug 'Error while getting sampling percentage, assuming default behavior'
290
+ @helpers.log_debug exception.to_s
291
+ sampling_percentage = 100
297
292
  end
298
293
 
299
- if @sampling_percentage > @random_percentage
300
- event_model.weight = @app_config.calculate_weight(@sampling_percentage)
294
+ if sampling_percentage > random_percentage
295
+ event_model.weight = @app_config.calculate_weight(sampling_percentage)
301
296
  # Add Event to the queue
302
297
  @events_queue << event_model
303
- if @debug
304
- puts("Event added to the queue ")
298
+ @helpers.log_debug("Event added to the queue ")
299
+ if Time.now.utc > (@last_config_download_time + 60)
300
+ start_worker()
305
301
  end
306
302
 
307
- if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > @last_updated_time + 300
303
+ if !@event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != @event_response_config_etag && Time.now.utc > (@last_config_download_time + 300)
308
304
  begin
309
- @config = @app_config.get_config(@api_controller, @debug)
310
- @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
305
+ new_config = @app_config.get_config(@api_controller)
306
+ if !new_config.nil?
307
+ @config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
308
+ end
309
+
311
310
  rescue => exception
312
- if @debug
313
- puts 'Error while updating the application configuration'
314
- puts exception.to_s
315
- end
311
+ @helpers.log_debug 'Error while updating the application configuration'
312
+ @helpers.log_debug exception.to_s
316
313
  end
317
314
  end
318
315
  else
319
- if @debug
320
- puts("Skipped Event due to sampling percentage: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
321
- end
316
+ @helpers.log_debug("Skipped Event due to sampling percentage: " + sampling_percentage.to_s + " and random percentage: " + random_percentage .to_s)
322
317
  end
323
318
  rescue => exception
324
- if @debug
325
- puts "Error adding event to the queue "
326
- puts exception.to_s
327
- end
319
+ @helpers.log_debug "Error adding event to the queue "
320
+ @helpers.log_debug exception.to_s
328
321
  end
329
322
 
330
323
  end
@@ -338,10 +331,11 @@ module MoesifRack
338
331
  end
339
332
 
340
333
  if !should_skip
341
- if @debug
334
+ begin
342
335
  process_send.call
343
- else
344
- Thread.start(&process_send)
336
+ rescue => exception
337
+ @helpers.log_debug 'Error while logging event - '
338
+ @helpers.log_debug exception.to_s
345
339
  end
346
340
  end
347
341
 
@@ -350,6 +344,9 @@ module MoesifRack
350
344
 
351
345
  def get_response_body(response)
352
346
  body = response.respond_to?(:body) ? response.body : response
347
+ if (body.instance_of?(Hash) || body.instance_of?(Array))
348
+ return body
349
+ end
353
350
  body = body.inject("") { |i, a| i << a } if (body.respond_to?(:each) && body.respond_to?(:inject))
354
351
  body.to_s
355
352
  end
@@ -221,6 +221,10 @@ module MoesifCaptureOutgoing
221
221
  puts "Error sending event to Moesif, with status code: "
222
222
  puts e.response_code
223
223
  end
224
+ rescue => e
225
+ if debug
226
+ puts e.to_s
227
+ end
224
228
  end
225
229
  else
226
230
  if @debug
metadata CHANGED
@@ -1,28 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moesif_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
8
8
  - Xing Wang
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-05-28 00:00:00.000000000 Z
12
+ date: 2020-12-22 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: :runtime
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
@@ -30,6 +36,9 @@ dependencies:
30
36
  requirement: !ruby/object:Gem::Requirement
31
37
  requirements:
32
38
  - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - - ">="
33
42
  - !ruby/object:Gem::Version
34
43
  version: 1.2.12
35
44
  type: :runtime
@@ -37,9 +46,12 @@ dependencies:
37
46
  version_requirements: !ruby/object:Gem::Requirement
38
47
  requirements:
39
48
  - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '1.2'
51
+ - - ">="
40
52
  - !ruby/object:Gem::Version
41
53
  version: 1.2.12
42
- description: Collection/Data Ingestion SDK for Rack (also Rails) Middleware / RoR
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,8 +73,15 @@ files:
60
73
  homepage: https://moesif.com
61
74
  licenses:
62
75
  - Apache-2.0
63
- metadata: {}
64
- post_install_message:
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
84
+ post_install_message:
65
85
  rdoc_options: []
66
86
  require_paths:
67
87
  - lib
@@ -76,8 +96,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
96
  - !ruby/object:Gem::Version
77
97
  version: '0'
78
98
  requirements: []
79
- rubygems_version: 3.0.4
80
- signing_key:
99
+ rubygems_version: 3.1.4
100
+ signing_key:
81
101
  specification_version: 4
82
102
  summary: moesif_rack
83
103
  test_files: []