moesif_rack 1.4.19 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,17 +3,14 @@ require 'rack'
3
3
  require 'moesif_api'
4
4
  require 'json'
5
5
  require 'base64'
6
- require_relative '../../lib/moesif_rack/app_config.rb'
6
+ require_relative '../../lib/moesif_rack/app_config'
7
7
 
8
8
  module MoesifCaptureOutgoing
9
-
10
9
  class << self
11
-
12
10
  def start_capture_outgoing(options)
13
11
  @moesif_options = options
14
- if not @moesif_options['application_id']
15
- raise 'application_id required for Moesif Middleware'
16
- end
12
+ raise 'application_id required for Moesif Middleware' unless @moesif_options['application_id']
13
+
17
14
  @api_client = MoesifApi::MoesifAPIClient.new(@moesif_options['application_id'])
18
15
  @api_controller = @api_client.api
19
16
  @debug = @moesif_options['debug']
@@ -28,27 +25,27 @@ module MoesifCaptureOutgoing
28
25
  @config_etag = nil
29
26
  @sampling_percentage = 100
30
27
  @last_updated_time = Time.now.utc
31
- @config_dict = Hash.new
28
+ @config_dict = {}
32
29
  begin
33
30
  new_config = @app_config.get_config(@api_controller)
34
- if !new_config.nil?
31
+ unless new_config.nil?
35
32
  @config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
36
33
  end
37
- rescue => exception
34
+ rescue StandardError => e
38
35
  if @debug
39
36
  puts 'Error while parsing application configuration on initialization'
40
- puts exception.to_s
37
+ puts e
41
38
  end
42
39
  end
43
40
  end
44
41
 
45
- def call (url, request, request_time, response, response_time)
42
+ def call(url, request, request_time, response, response_time)
46
43
  send_moesif_event(url, request, request_time, response, response_time)
47
44
  end
48
-
45
+
49
46
  def get_response_body(response)
50
47
  body = response.respond_to?(:body) ? response.body : response
51
- body = body.inject("") { |i, a| i << a } if body.respond_to?(:each)
48
+ body = body.inject('') { |i, a| i << a } if body.respond_to?(:each)
52
49
  body.to_s
53
50
  end
54
51
 
@@ -57,12 +54,9 @@ module MoesifCaptureOutgoing
57
54
  end
58
55
 
59
56
  def send_moesif_event(url, request, request_time, response, response_time)
60
-
61
- if url.downcase.include? "moesif"
62
- if @debug
63
- puts "Skip sending as it is moesif Event"
64
- end
65
- else
57
+ if url.downcase.include? 'moesif'
58
+ puts 'Skip sending as it is moesif Event' if @debug
59
+ else
66
60
  response.code = transform_response_code(response.code) if response.code.is_a?(Symbol)
67
61
 
68
62
  # Request Body
@@ -70,14 +64,12 @@ module MoesifCaptureOutgoing
70
64
  req_body_transfer_encoding = nil
71
65
  req_body = nil
72
66
 
73
- if @log_body_outgoing
74
- if req_body_string && req_body_string.length != 0
75
- begin
76
- req_body = JSON.parse(req_body_string)
77
- rescue
78
- req_body = Base64.encode64(req_body_string)
79
- req_body_transfer_encoding = 'base64'
80
- end
67
+ if @log_body_outgoing && (req_body_string && req_body_string.length != 0)
68
+ begin
69
+ req_body = JSON.parse(req_body_string)
70
+ rescue StandardError
71
+ req_body = Base64.encode64(req_body_string)
72
+ req_body_transfer_encoding = 'base64'
81
73
  end
82
74
  end
83
75
 
@@ -86,19 +78,17 @@ module MoesifCaptureOutgoing
86
78
  rsp_body_transfer_encoding = nil
87
79
  rsp_body = nil
88
80
 
89
- if @log_body_outgoing
90
- if rsp_body_string && rsp_body_string.length != 0
91
- begin
92
- rsp_body = JSON.parse(rsp_body_string)
93
- rescue
94
- rsp_body = Base64.encode64(rsp_body_string)
95
- rsp_body_transfer_encoding = 'base64'
96
- end
81
+ if @log_body_outgoing && (rsp_body_string && rsp_body_string.length != 0)
82
+ begin
83
+ rsp_body = JSON.parse(rsp_body_string)
84
+ rescue StandardError
85
+ rsp_body = Base64.encode64(rsp_body_string)
86
+ rsp_body_transfer_encoding = 'base64'
97
87
  end
98
88
  end
99
89
 
100
90
  # Event Request
101
- event_req = MoesifApi::EventRequestModel.new()
91
+ event_req = MoesifApi::EventRequestModel.new
102
92
  event_req.time = request_time
103
93
  event_req.uri = url
104
94
  event_req.verb = request.method.to_s.upcase
@@ -107,8 +97,8 @@ module MoesifCaptureOutgoing
107
97
  event_req.body = req_body
108
98
  event_req.transfer_encoding = req_body_transfer_encoding
109
99
 
110
- # Event Response
111
- event_rsp = MoesifApi::EventResponseModel.new()
100
+ # Event Response
101
+ event_rsp = MoesifApi::EventResponseModel.new
112
102
  event_rsp.time = response_time
113
103
  event_rsp.status = response.code.to_i
114
104
  event_rsp.headers = response.each_header.collect.to_h
@@ -116,71 +106,58 @@ module MoesifCaptureOutgoing
116
106
  event_rsp.transfer_encoding = rsp_body_transfer_encoding
117
107
 
118
108
  # Prepare Event Model
119
- event_model = MoesifApi::EventModel.new()
109
+ event_model = MoesifApi::EventModel.new
120
110
  event_model.request = event_req
121
111
  event_model.response = event_rsp
122
- event_model.direction = "Outgoing"
112
+ event_model.direction = 'Outgoing'
123
113
 
124
114
  # Metadata for Outgoing Request
125
115
  if @get_metadata_outgoing
126
- if @debug
127
- puts "calling get_metadata_outgoing proc"
128
- end
116
+ puts 'calling get_metadata_outgoing proc' if @debug
129
117
  event_model.metadata = @get_metadata_outgoing.call(request, response)
130
118
  end
131
119
 
132
120
  # Identify User
133
121
  if @identify_user_outgoing
134
- if @debug
135
- puts "calling identify_user_outgoing proc"
136
- end
122
+ puts 'calling identify_user_outgoing proc' if @debug
137
123
  event_model.user_id = @identify_user_outgoing.call(request, response)
138
124
  end
139
125
 
140
126
  # Identify Company
141
127
  if @identify_company_outgoing
142
- if @debug
143
- puts "calling identify_company_outgoing proc"
144
- end
128
+ puts 'calling identify_company_outgoing proc' if @debug
145
129
  event_model.company_id = @identify_company_outgoing.call(request, response)
146
130
  end
147
131
 
148
132
  # Session Token
149
133
  if @identify_session_outgoing
150
- if @debug
151
- puts "calling identify_session_outgoing proc"
152
- end
134
+ puts 'calling identify_session_outgoing proc' if @debug
153
135
  event_model.session_token = @identify_session_outgoing.call(request, response)
154
136
  end
155
137
 
156
138
  # Skip Outgoing Request
157
139
  should_skip = false
158
140
 
159
- if @skip_outgoing
160
- if @skip_outgoing.call(request, response)
161
- should_skip = true;
162
- end
163
- end
141
+ should_skip = true if @skip_outgoing && @skip_outgoing.call(request, response)
164
142
 
165
143
  if !should_skip
166
144
 
167
145
  # Mask outgoing Event
168
146
  if @mask_data_outgoing
169
- if @debug
170
- puts "calling mask_data_outgoing proc"
171
- end
147
+ puts 'calling mask_data_outgoing proc' if @debug
172
148
  event_model = @mask_data_outgoing.call(event_model)
173
149
  end
174
150
 
175
151
  # Send Event to Moesif
176
152
  begin
177
153
  @random_percentage = Random.rand(0.00..100.00)
178
- begin
179
- @sampling_percentage = @app_config.get_sampling_percentage(event_model, @config, event_model.user_id, event_model.company_id)
180
- rescue => exception
154
+ begin
155
+ @sampling_percentage = @app_config.get_sampling_percentage(event_model, @config, event_model.user_id,
156
+ event_model.company_id)
157
+ rescue StandardError => e
181
158
  if @debug
182
159
  puts 'Error while getting sampling percentage, assuming default behavior'
183
- puts exception.to_s
160
+ puts e
184
161
  end
185
162
  @sampling_percentage = 100
186
163
  end
@@ -195,43 +172,35 @@ module MoesifCaptureOutgoing
195
172
  event_response_config_etag = event_api_response[:x_moesif_config_etag]
196
173
 
197
174
  if !event_response_config_etag.nil? && !@config_etag.nil? && @config_etag != event_response_config_etag && Time.now.utc > @last_updated_time + 300
198
- begin
175
+ begin
199
176
  new_config = @app_config.get_config(@api_controller)
200
- if !new_config.nil?
177
+ unless new_config.nil?
201
178
  @config, @config_etag, @last_config_download_time = @app_config.parse_configuration(new_config)
202
179
  end
203
- rescue => exception
180
+ rescue StandardError => e
204
181
  if @debug
205
182
  puts 'Error while updating the application configuration'
206
- puts exception.to_s
183
+ puts e
207
184
  end
208
185
  end
209
186
  end
210
- if @debug
211
- puts("Event successfully sent to Moesif")
212
- end
213
- else
214
- if @debug
215
- puts("Skipped outgoing Event due to sampling percentage: " + @sampling_percentage.to_s + " and random percentage: " + @random_percentage.to_s)
216
- end
187
+ puts('Event successfully sent to Moesif') if @debug
188
+ elsif @debug
189
+ puts('Skipped outgoing Event due to sampling percentage: ' + @sampling_percentage.to_s + ' and random percentage: ' + @random_percentage.to_s)
217
190
  end
218
191
  rescue MoesifApi::APIException => e
219
192
  if e.response_code.between?(401, 403)
220
- puts "Unathorized accesss sending event to Moesif. Please verify your Application Id."
193
+ puts 'Unathorized accesss sending event to Moesif. Please verify your Application Id.'
221
194
  end
222
195
  if @debug
223
- puts "Error sending event to Moesif, with status code: "
196
+ puts 'Error sending event to Moesif, with status code: '
224
197
  puts e.response_code
225
198
  end
226
- rescue => e
227
- if @debug
228
- puts e.to_s
229
- end
199
+ rescue StandardError => e
200
+ puts e if @debug
230
201
  end
231
- else
232
- if @debug
233
- puts 'Skip sending outgoing request'
234
- end
202
+ elsif @debug
203
+ puts 'Skip sending outgoing request'
235
204
  end
236
205
  end
237
206
  end
@@ -1,2 +1,2 @@
1
- require_relative 'httplog/http_log.rb'
2
- require_relative 'httplog/adapters/net_http.rb'
1
+ require_relative 'httplog/http_log'
2
+ require_relative 'httplog/adapters/net_http'