nice_http 1.7.2 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,526 +1,526 @@
1
- module NiceHttpHttpMethods
2
-
3
- ######################################################
4
- # Get data from path
5
- #
6
- # @param arg [Hash] containing at least key :path
7
- # @param arg [String] the path
8
- #
9
- # @return [Hash] response
10
- # Including at least the symbol keys:
11
- # :data = the response data body.
12
- # :message = plain text response.
13
- # :code = code response (200=ok,500=wrong...).
14
- # All keys in response are lowercase.
15
- # data, message and code can also be accessed as attributes like .message .code .data.
16
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
17
- #
18
- # @example
19
- # resp = @http.get(Requests::Customer.get_profile)
20
- # assert resp.code == 200
21
- # @example
22
- # resp = @http.get("/customers/1223")
23
- # assert resp.message == "OK"
24
- ######################################################
25
- def get(arg)
26
- begin
27
- path, data, headers_t = manage_request(arg)
28
-
29
- @start_time = Time.now if @start_time.nil?
30
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
31
- data = ""
32
- if arg[:mock_response].keys.include?(:data)
33
- data = arg[:mock_response][:data]
34
- if data.kind_of?(Hash) #to json
35
- begin
36
- require "json"
37
- data = data.to_json
38
- rescue
39
- @logger.fatal "There was a problem converting to json: #{data}"
40
- end
41
- end
42
- end
43
- @logger.warn "Pay attention!!! This is a mock response:"
44
- @start_time_net = Time.now if @start_time_net.nil?
45
- manage_response(arg[:mock_response], data.to_s)
46
- return @response
47
- end
48
- begin
49
- if path.start_with?("http:") or path.start_with?("https:") #server included on path problably because of a redirection to a different server
50
- require "uri"
51
- uri = URI.parse(path)
52
- ssl = false
53
- ssl = true if path.include?("https:")
54
-
55
- server = "http://"
56
- server = "https://" if path.start_with?("https:")
57
- if uri.port != 443
58
- server += "#{uri.host}:#{uri.port}"
59
- else
60
- server += "#{uri.host}"
61
- end
62
-
63
- http_redir = nil
64
- self.class.connections.each { |conn|
65
- if conn.host == uri.host and conn.port == uri.port
66
- http_redir = conn
67
- break
68
- end
69
- }
70
-
71
- if !http_redir.nil?
72
- path, data, headers_t = manage_request(arg)
73
- http_redir.cookies.merge!(@cookies)
74
- http_redir.headers.merge!(headers_t)
75
- #todo: remove only the server at the begining in case in query is the server it will be replaced when it should not be
76
- resp = http_redir.get(path.gsub(server, ""))
77
- @response = http_redir.response
78
- else
79
- @logger.warn "It seems like the http connection cannot redirect to #{server} because there is no active connection for that server. You need to create previously one."
80
- end
81
- else
82
- @start_time_net = Time.now if @start_time_net.nil?
83
- resp = @http.get(path, headers_t)
84
- data = resp.body
85
- manage_response(resp, data)
86
- end
87
- rescue Exception => stack
88
- @logger.warn stack
89
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
90
- @http.finish()
91
- @http.start()
92
- @start_time_net = Time.now if @start_time_net.nil?
93
- resp = @http.get(path)
94
- data = resp.body
95
- manage_response(resp, data)
96
- end
97
- if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
98
- if @num_redirects <= 30
99
- @num_redirects += 1
100
- current_server = "http"
101
- current_server += "s" if @ssl == true
102
- current_server += "://#{@host}"
103
- location = @response[:location].gsub(current_server, "")
104
- @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
105
- get(location)
106
- else
107
- @logger.fatal "(#{@num_redirects}) Maximum number of redirections for a single request reached. Be sure everything is correct, it seems there is a non ending loop"
108
- @num_redirects = 0
109
- end
110
- else
111
- @num_redirects = 0
112
- end
113
- return @response
114
- rescue Exception => stack
115
- @logger.fatal stack
116
- return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
117
- end
118
- end
119
-
120
- ######################################################
121
- # Post data to path
122
- # @param arguments [Hash] containing at least keys :data and :path.
123
- # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
124
- # @param arguments [Array<path, data, additional_headers>]
125
- # path (string).
126
- # data (json data for example).
127
- # additional_headers (Hash key=>value).
128
- # @return [Hash] response
129
- # Including at least the symbol keys:
130
- # :data = the response data body.
131
- # :message = plain text response.
132
- # :code = code response (200=ok,500=wrong...).
133
- # All keys in response are lowercase.
134
- # data, message and code can also be accessed as attributes like .message .code .data.
135
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
136
- # @example
137
- # resp = @http.post(Requests::Customer.update_customer)
138
- # assert resp.code == 201
139
- # @example
140
- # resp = http.post( {
141
- # path: "/api/users",
142
- # data: {name: "morpheus", job: "leader"}
143
- # } )
144
- # pp resp.data.json
145
- ######################################################
146
- def post(*arguments)
147
- begin
148
- path, data, headers_t = manage_request(*arguments)
149
- @start_time = Time.now if @start_time.nil?
150
- if arguments.size > 0 and arguments[0].kind_of?(Hash)
151
- arg = arguments[0]
152
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
153
- data = ""
154
- if arg[:mock_response].keys.include?(:data)
155
- data = arg[:mock_response][:data]
156
- if data.kind_of?(Hash) #to json
157
- begin
158
- require "json"
159
- data = data.to_json
160
- rescue
161
- @logger.fatal "There was a problem converting to json: #{data}"
162
- end
163
- end
164
- end
165
- @logger.warn "Pay attention!!! This is a mock response:"
166
- @start_time_net = Time.now if @start_time_net.nil?
167
- manage_response(arg[:mock_response], data.to_s)
168
- return @response
169
- end
170
- end
171
-
172
- begin
173
- @start_time_net = Time.now if @start_time_net.nil?
174
- if headers_t["Content-Type"] == "multipart/form-data"
175
- require "net/http/post/multipart"
176
- headers_t.each { |key, value|
177
- arguments[0][:data].add_field(key, value) #add to Headers
178
- }
179
- resp = @http.request(arguments[0][:data])
180
- else
181
- resp = @http.post(path, data, headers_t)
182
- data = resp.body
183
- end
184
- rescue Exception => stack
185
- @logger.warn stack
186
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
187
- @http.finish()
188
- @http.start()
189
- @start_time_net = Time.now if @start_time_net.nil?
190
- resp, data = @http.post(path, data, headers_t)
191
- end
192
- manage_response(resp, data)
193
- if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
194
- if @num_redirects <= 30
195
- @num_redirects += 1
196
- current_server = "http"
197
- current_server += "s" if @ssl == true
198
- current_server += "://#{@host}"
199
- location = @response[:location].gsub(current_server, "")
200
- @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
201
- get(location)
202
- else
203
- @logger.fatal "(#{@num_redirects}) Maximum number of redirections for a single request reached. Be sure everything is correct, it seems there is a non ending loop"
204
- @num_redirects = 0
205
- end
206
- else
207
- @num_redirects = 0
208
- end
209
- return @response
210
- rescue Exception => stack
211
- @logger.fatal stack
212
- return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
213
- end
214
- end
215
-
216
- ######################################################
217
- # Put data to path
218
- # @param arguments [Hash] containing at least keys :data and :path.
219
- # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
220
- # @param arguments [Array<path, data, additional_headers>]
221
- # path (string).
222
- # data (json data for example).
223
- # additional_headers (Hash key=>value).
224
- # @return [Hash] response
225
- # Including at least the symbol keys:
226
- # :data = the response data body.
227
- # :message = plain text response.
228
- # :code = code response (200=ok,500=wrong...).
229
- # All keys in response are lowercase.
230
- # data, message and code can also be accessed as attributes like .message .code .data.
231
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
232
- # @example
233
- # resp = @http.put(Requests::Customer.remove_phone)
234
- ######################################################
235
- def put(*arguments)
236
- begin
237
- path, data, headers_t = manage_request(*arguments)
238
- @start_time = Time.now if @start_time.nil?
239
- if arguments.size > 0 and arguments[0].kind_of?(Hash)
240
- arg = arguments[0]
241
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
242
- data = ""
243
- if arg[:mock_response].keys.include?(:data)
244
- data = arg[:mock_response][:data]
245
- if data.kind_of?(Hash) #to json
246
- begin
247
- require "json"
248
- data = data.to_json
249
- rescue
250
- @logger.fatal "There was a problem converting to json: #{data}"
251
- end
252
- end
253
- end
254
- @logger.warn "Pay attention!!! This is a mock response:"
255
- @start_time_net = Time.now if @start_time_net.nil?
256
- manage_response(arg[:mock_response], data.to_s)
257
- return @response
258
- end
259
- end
260
-
261
- begin
262
- @start_time_net = Time.now if @start_time_net.nil?
263
- resp = @http.send_request("PUT", path, data, headers_t)
264
- data = resp.body
265
- rescue Exception => stack
266
- @logger.warn stack
267
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
268
- @http.finish()
269
- @http.start()
270
- @start_time_net = Time.now if @start_time_net.nil?
271
- resp, data = @http.send_request("PUT", path, data, headers_t)
272
- end
273
- manage_response(resp, data)
274
-
275
- return @response
276
- rescue Exception => stack
277
- @logger.fatal stack
278
- return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
279
- end
280
- end
281
-
282
- ######################################################
283
- # Patch data to path
284
- # @param arguments [Hash] containing at least keys :data and :path.
285
- # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
286
- # @param arguments [Array<path, data, additional_headers>]
287
- # path (string).
288
- # data (json data for example).
289
- # additional_headers (Hash key=>value).
290
- # @return [Hash] response
291
- # Including at least the symbol keys:
292
- # :data = the response data body.
293
- # :message = plain text response.
294
- # :code = code response (200=ok,500=wrong...).
295
- # All keys in response are lowercase.
296
- # data, message and code can also be accessed as attributes like .message .code .data.
297
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
298
- # @example
299
- # resp = @http.patch(Requests::Customer.unrelease_account)
300
- ######################################################
301
- def patch(*arguments)
302
- begin
303
- path, data, headers_t = manage_request(*arguments)
304
- @start_time = Time.now if @start_time.nil?
305
- if arguments.size > 0 and arguments[0].kind_of?(Hash)
306
- arg = arguments[0]
307
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
308
- data = ""
309
- if arg[:mock_response].keys.include?(:data)
310
- data = arg[:mock_response][:data]
311
- if data.kind_of?(Hash) #to json
312
- begin
313
- require "json"
314
- data = data.to_json
315
- rescue
316
- @logger.fatal "There was a problem converting to json: #{data}"
317
- end
318
- end
319
- end
320
- @logger.warn "Pay attention!!! This is a mock response:"
321
- @start_time_net = Time.now if @start_time_net.nil?
322
- manage_response(arg[:mock_response], data.to_s)
323
- return @response
324
- end
325
- end
326
-
327
- begin
328
- @start_time_net = Time.now if @start_time_net.nil?
329
- resp = @http.patch(path, data, headers_t)
330
- data = resp.body
331
- rescue Exception => stack
332
- @logger.warn stack
333
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
334
- @http.finish()
335
- @http.start()
336
- @start_time_net = Time.now if @start_time_net.nil?
337
- resp, data = @http.patch(path, data, headers_t)
338
- end
339
- manage_response(resp, data)
340
- if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
341
- if @num_redirects <= 30
342
- @num_redirects += 1
343
- current_server = "http"
344
- current_server += "s" if @ssl == true
345
- current_server += "://#{@host}"
346
- location = @response[:location].gsub(current_server, "")
347
- @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
348
- get(location)
349
- else
350
- @logger.fatal "(#{@num_redirects}) Maximum number of redirections for a single request reached. Be sure everything is correct, it seems there is a non ending loop"
351
- @num_redirects = 0
352
- end
353
- else
354
- @num_redirects = 0
355
- end
356
- return @response
357
- rescue Exception => stack
358
- @logger.fatal stack
359
- return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
360
- end
361
- end
362
-
363
- ######################################################
364
- # Delete an existing resource
365
- # @param arg [Hash] containing at least key :path
366
- # @param arg [String] the path
367
- #
368
- # @return [Hash] response
369
- # Including at least the symbol keys:
370
- # :data = the response data body.
371
- # :message = plain text response.
372
- # :code = code response (200=ok,500=wrong...).
373
- # All keys in response are lowercase.
374
- # data, message and code can also be accessed as attributes like .message .code .data.
375
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
376
- # @example
377
- # resp = @http.delete(Requests::Customer.remove_session)
378
- # assert resp.code == 204
379
- ######################################################
380
- def delete(argument)
381
- begin
382
- if argument.kind_of?(String)
383
- argument = {:path => argument}
384
- end
385
- path, data, headers_t = manage_request(argument)
386
- @start_time = Time.now if @start_time.nil?
387
- if argument.kind_of?(Hash)
388
- arg = argument
389
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
390
- data = ""
391
- if arg[:mock_response].keys.include?(:data)
392
- data = arg[:mock_response][:data]
393
- if data.kind_of?(Hash) #to json
394
- begin
395
- require "json"
396
- data = data.to_json
397
- rescue
398
- @logger.fatal "There was a problem converting to json: #{data}"
399
- end
400
- end
401
- end
402
- @logger.warn "Pay attention!!! This is a mock response:"
403
- @start_time_net = Time.now if @start_time_net.nil?
404
- manage_response(arg[:mock_response], data.to_s)
405
- return @response
406
- end
407
- end
408
-
409
- begin
410
- @start_time_net = Time.now if @start_time_net.nil?
411
- resp = @http.delete(path, headers_t)
412
- data = resp.body
413
- rescue Exception => stack
414
- @logger.warn stack
415
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
416
- @http.finish()
417
- @http.start()
418
- @start_time_net = Time.now if @start_time_net.nil?
419
- resp, data = @http.delete(path)
420
- end
421
- manage_response(resp, data)
422
-
423
- return @response
424
- rescue Exception => stack
425
- @logger.fatal stack
426
- return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
427
- end
428
- end
429
-
430
- ######################################################
431
- # Implementation of the http HEAD method.
432
- # Asks for the response identical to the one that would correspond to a GET request, but without the response body.
433
- # This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
434
- # @param arg [Hash] containing at least key :path
435
- # @param arg [String] the path
436
- #
437
- # @return [Hash] response
438
- # Including at least the symbol keys:
439
- # :message = plain text response.
440
- # :code = code response (200=ok,500=wrong...).
441
- # All keys in response are lowercase.
442
- # message and code can also be accessed as attributes like .message .code.
443
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil }
444
- ######################################################
445
- def head(argument)
446
- begin
447
- if argument.kind_of?(String)
448
- argument = {:path => argument}
449
- end
450
- path, data, headers_t = manage_request(argument)
451
- @start_time = Time.now if @start_time.nil?
452
- if argument.kind_of?(Hash)
453
- arg = argument
454
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
455
- @logger.warn "Pay attention!!! This is a mock response:"
456
- @start_time_net = Time.now if @start_time_net.nil?
457
- manage_response(arg[:mock_response], "")
458
- return @response
459
- end
460
- end
461
-
462
- begin
463
- @start_time_net = Time.now if @start_time_net.nil?
464
- resp = @http.head(path, headers_t)
465
- data = resp.body
466
- rescue Exception => stack
467
- @logger.warn stack
468
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
469
- @http.finish()
470
- @http.start()
471
- @start_time_net = Time.now if @start_time_net.nil?
472
- resp, data = @http.head(path)
473
- end
474
- manage_response(resp, data)
475
- return @response
476
- rescue Exception => stack
477
- @logger.fatal stack
478
- return {fatal_error: stack.to_s, code: nil, message: nil}
479
- end
480
- end
481
-
482
- ######################################################
483
- # It will send the request depending on the :method declared on the request hash
484
- # Take a look at https://github.com/MarioRuiz/Request-Hash
485
- #
486
- # @param request_hash [Hash] containing at least key :path and :method. The methods that are accepted are: :get, :head, :post, :put, :delete, :patch
487
- #
488
- # @return [Hash] response
489
- # Including at least the symbol keys:
490
- # :data = the response data body.
491
- # :message = plain text response.
492
- # :code = code response (200=ok,500=wrong...).
493
- # All keys in response are lowercase.
494
- # data, message and code can also be accessed as attributes like .message .code .data.
495
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
496
- # @example
497
- # resp = @http.send_request Requests::Customer.remove_session
498
- # assert resp.code == 204
499
- ######################################################
500
- def send_request(request_hash)
501
- unless request_hash.is_a?(Hash) and request_hash.key?(:method) and request_hash.key?(:path) and
502
- request_hash[:method].is_a?(Symbol) and
503
- [:get, :head, :post, :put, :delete, :patch].include?(request_hash[:method])
504
- message = "send_request: it needs to be supplied a Request Hash that includes a :method and :path. "
505
- message += "Supported methods: :get, :head, :post, :put, :delete, :patch"
506
- @logger.fatal message
507
- return {fatal_error: message, code: nil, message: nil}
508
- else
509
- case request_hash[:method]
510
- when :get
511
- resp = get request_hash
512
- when :post
513
- resp = post request_hash
514
- when :head
515
- resp = head request_hash
516
- when :put
517
- resp = put request_hash
518
- when :delete
519
- resp = delete request_hash
520
- when :patch
521
- resp = patch request_hash
522
- end
523
- return resp
524
- end
525
- end
526
- end
1
+ module NiceHttpHttpMethods
2
+
3
+ ######################################################
4
+ # Get data from path
5
+ #
6
+ # @param arg [Hash] containing at least key :path
7
+ # @param arg [String] the path
8
+ #
9
+ # @return [Hash] response
10
+ # Including at least the symbol keys:
11
+ # :data = the response data body.
12
+ # :message = plain text response.
13
+ # :code = code response (200=ok,500=wrong...).
14
+ # All keys in response are lowercase.
15
+ # data, message and code can also be accessed as attributes like .message .code .data.
16
+ # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
17
+ #
18
+ # @example
19
+ # resp = @http.get(Requests::Customer.get_profile)
20
+ # assert resp.code == 200
21
+ # @example
22
+ # resp = @http.get("/customers/1223")
23
+ # assert resp.message == "OK"
24
+ ######################################################
25
+ def get(arg)
26
+ begin
27
+ path, data, headers_t = manage_request(arg)
28
+
29
+ @start_time = Time.now if @start_time.nil?
30
+ if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
31
+ data = ""
32
+ if arg[:mock_response].keys.include?(:data)
33
+ data = arg[:mock_response][:data]
34
+ if data.kind_of?(Hash) #to json
35
+ begin
36
+ require "json"
37
+ data = data.to_json
38
+ rescue
39
+ @logger.fatal "There was a problem converting to json: #{data}"
40
+ end
41
+ end
42
+ end
43
+ @logger.warn "Pay attention!!! This is a mock response:"
44
+ @start_time_net = Time.now if @start_time_net.nil?
45
+ manage_response(arg[:mock_response], data.to_s)
46
+ return @response
47
+ end
48
+ begin
49
+ if path.start_with?("http:") or path.start_with?("https:") #server included on path problably because of a redirection to a different server
50
+ require "uri"
51
+ uri = URI.parse(path)
52
+ ssl = false
53
+ ssl = true if path.include?("https:")
54
+
55
+ server = "http://"
56
+ server = "https://" if path.start_with?("https:")
57
+ if uri.port != 443
58
+ server += "#{uri.host}:#{uri.port}"
59
+ else
60
+ server += "#{uri.host}"
61
+ end
62
+
63
+ http_redir = nil
64
+ self.class.connections.each { |conn|
65
+ if conn.host == uri.host and conn.port == uri.port
66
+ http_redir = conn
67
+ break
68
+ end
69
+ }
70
+
71
+ if !http_redir.nil?
72
+ path, data, headers_t = manage_request(arg)
73
+ http_redir.cookies.merge!(@cookies)
74
+ http_redir.headers.merge!(headers_t)
75
+ #todo: remove only the server at the begining in case in query is the server it will be replaced when it should not be
76
+ resp = http_redir.get(path.gsub(server, ""))
77
+ @response = http_redir.response
78
+ else
79
+ @logger.warn "It seems like the http connection cannot redirect to #{server} because there is no active connection for that server. You need to create previously one."
80
+ end
81
+ else
82
+ @start_time_net = Time.now if @start_time_net.nil?
83
+ resp = @http.get(path, headers_t)
84
+ data = resp.body
85
+ manage_response(resp, data)
86
+ end
87
+ rescue Exception => stack
88
+ @logger.warn stack
89
+ @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
90
+ @http.finish()
91
+ @http.start()
92
+ @start_time_net = Time.now if @start_time_net.nil?
93
+ resp = @http.get(path)
94
+ data = resp.body
95
+ manage_response(resp, data)
96
+ end
97
+ if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
98
+ if @num_redirects <= 30
99
+ @num_redirects += 1
100
+ current_server = "http"
101
+ current_server += "s" if @ssl == true
102
+ current_server += "://#{@host}"
103
+ location = @response[:location].gsub(current_server, "")
104
+ @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
105
+ get(location)
106
+ else
107
+ @logger.fatal "(#{@num_redirects}) Maximum number of redirections for a single request reached. Be sure everything is correct, it seems there is a non ending loop"
108
+ @num_redirects = 0
109
+ end
110
+ else
111
+ @num_redirects = 0
112
+ end
113
+ return @response
114
+ rescue Exception => stack
115
+ @logger.fatal stack
116
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
117
+ end
118
+ end
119
+
120
+ ######################################################
121
+ # Post data to path
122
+ # @param arguments [Hash] containing at least keys :data and :path.
123
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
124
+ # @param arguments [Array<path, data, additional_headers>]
125
+ # path (string).
126
+ # data (json data for example).
127
+ # additional_headers (Hash key=>value).
128
+ # @return [Hash] response
129
+ # Including at least the symbol keys:
130
+ # :data = the response data body.
131
+ # :message = plain text response.
132
+ # :code = code response (200=ok,500=wrong...).
133
+ # All keys in response are lowercase.
134
+ # data, message and code can also be accessed as attributes like .message .code .data.
135
+ # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
136
+ # @example
137
+ # resp = @http.post(Requests::Customer.update_customer)
138
+ # assert resp.code == 201
139
+ # @example
140
+ # resp = http.post( {
141
+ # path: "/api/users",
142
+ # data: {name: "morpheus", job: "leader"}
143
+ # } )
144
+ # pp resp.data.json
145
+ ######################################################
146
+ def post(*arguments)
147
+ begin
148
+ path, data, headers_t = manage_request(*arguments)
149
+ @start_time = Time.now if @start_time.nil?
150
+ if arguments.size > 0 and arguments[0].kind_of?(Hash)
151
+ arg = arguments[0]
152
+ if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
153
+ data = ""
154
+ if arg[:mock_response].keys.include?(:data)
155
+ data = arg[:mock_response][:data]
156
+ if data.kind_of?(Hash) #to json
157
+ begin
158
+ require "json"
159
+ data = data.to_json
160
+ rescue
161
+ @logger.fatal "There was a problem converting to json: #{data}"
162
+ end
163
+ end
164
+ end
165
+ @logger.warn "Pay attention!!! This is a mock response:"
166
+ @start_time_net = Time.now if @start_time_net.nil?
167
+ manage_response(arg[:mock_response], data.to_s)
168
+ return @response
169
+ end
170
+ end
171
+
172
+ begin
173
+ @start_time_net = Time.now if @start_time_net.nil?
174
+ if headers_t["Content-Type"] == "multipart/form-data"
175
+ require "net/http/post/multipart"
176
+ headers_t.each { |key, value|
177
+ arguments[0][:data].add_field(key, value) #add to Headers
178
+ }
179
+ resp = @http.request(arguments[0][:data])
180
+ else
181
+ resp = @http.post(path, data, headers_t)
182
+ data = resp.body
183
+ end
184
+ rescue Exception => stack
185
+ @logger.warn stack
186
+ @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
187
+ @http.finish()
188
+ @http.start()
189
+ @start_time_net = Time.now if @start_time_net.nil?
190
+ resp, data = @http.post(path, data, headers_t)
191
+ end
192
+ manage_response(resp, data)
193
+ if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
194
+ if @num_redirects <= 30
195
+ @num_redirects += 1
196
+ current_server = "http"
197
+ current_server += "s" if @ssl == true
198
+ current_server += "://#{@host}"
199
+ location = @response[:location].gsub(current_server, "")
200
+ @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
201
+ get(location)
202
+ else
203
+ @logger.fatal "(#{@num_redirects}) Maximum number of redirections for a single request reached. Be sure everything is correct, it seems there is a non ending loop"
204
+ @num_redirects = 0
205
+ end
206
+ else
207
+ @num_redirects = 0
208
+ end
209
+ return @response
210
+ rescue Exception => stack
211
+ @logger.fatal stack
212
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
213
+ end
214
+ end
215
+
216
+ ######################################################
217
+ # Put data to path
218
+ # @param arguments [Hash] containing at least keys :data and :path.
219
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
220
+ # @param arguments [Array<path, data, additional_headers>]
221
+ # path (string).
222
+ # data (json data for example).
223
+ # additional_headers (Hash key=>value).
224
+ # @return [Hash] response
225
+ # Including at least the symbol keys:
226
+ # :data = the response data body.
227
+ # :message = plain text response.
228
+ # :code = code response (200=ok,500=wrong...).
229
+ # All keys in response are lowercase.
230
+ # data, message and code can also be accessed as attributes like .message .code .data.
231
+ # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
232
+ # @example
233
+ # resp = @http.put(Requests::Customer.remove_phone)
234
+ ######################################################
235
+ def put(*arguments)
236
+ begin
237
+ path, data, headers_t = manage_request(*arguments)
238
+ @start_time = Time.now if @start_time.nil?
239
+ if arguments.size > 0 and arguments[0].kind_of?(Hash)
240
+ arg = arguments[0]
241
+ if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
242
+ data = ""
243
+ if arg[:mock_response].keys.include?(:data)
244
+ data = arg[:mock_response][:data]
245
+ if data.kind_of?(Hash) #to json
246
+ begin
247
+ require "json"
248
+ data = data.to_json
249
+ rescue
250
+ @logger.fatal "There was a problem converting to json: #{data}"
251
+ end
252
+ end
253
+ end
254
+ @logger.warn "Pay attention!!! This is a mock response:"
255
+ @start_time_net = Time.now if @start_time_net.nil?
256
+ manage_response(arg[:mock_response], data.to_s)
257
+ return @response
258
+ end
259
+ end
260
+
261
+ begin
262
+ @start_time_net = Time.now if @start_time_net.nil?
263
+ resp = @http.send_request("PUT", path, data, headers_t)
264
+ data = resp.body
265
+ rescue Exception => stack
266
+ @logger.warn stack
267
+ @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
268
+ @http.finish()
269
+ @http.start()
270
+ @start_time_net = Time.now if @start_time_net.nil?
271
+ resp, data = @http.send_request("PUT", path, data, headers_t)
272
+ end
273
+ manage_response(resp, data)
274
+
275
+ return @response
276
+ rescue Exception => stack
277
+ @logger.fatal stack
278
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
279
+ end
280
+ end
281
+
282
+ ######################################################
283
+ # Patch data to path
284
+ # @param arguments [Hash] containing at least keys :data and :path.
285
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
286
+ # @param arguments [Array<path, data, additional_headers>]
287
+ # path (string).
288
+ # data (json data for example).
289
+ # additional_headers (Hash key=>value).
290
+ # @return [Hash] response
291
+ # Including at least the symbol keys:
292
+ # :data = the response data body.
293
+ # :message = plain text response.
294
+ # :code = code response (200=ok,500=wrong...).
295
+ # All keys in response are lowercase.
296
+ # data, message and code can also be accessed as attributes like .message .code .data.
297
+ # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
298
+ # @example
299
+ # resp = @http.patch(Requests::Customer.unrelease_account)
300
+ ######################################################
301
+ def patch(*arguments)
302
+ begin
303
+ path, data, headers_t = manage_request(*arguments)
304
+ @start_time = Time.now if @start_time.nil?
305
+ if arguments.size > 0 and arguments[0].kind_of?(Hash)
306
+ arg = arguments[0]
307
+ if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
308
+ data = ""
309
+ if arg[:mock_response].keys.include?(:data)
310
+ data = arg[:mock_response][:data]
311
+ if data.kind_of?(Hash) #to json
312
+ begin
313
+ require "json"
314
+ data = data.to_json
315
+ rescue
316
+ @logger.fatal "There was a problem converting to json: #{data}"
317
+ end
318
+ end
319
+ end
320
+ @logger.warn "Pay attention!!! This is a mock response:"
321
+ @start_time_net = Time.now if @start_time_net.nil?
322
+ manage_response(arg[:mock_response], data.to_s)
323
+ return @response
324
+ end
325
+ end
326
+
327
+ begin
328
+ @start_time_net = Time.now if @start_time_net.nil?
329
+ resp = @http.patch(path, data, headers_t)
330
+ data = resp.body
331
+ rescue Exception => stack
332
+ @logger.warn stack
333
+ @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
334
+ @http.finish()
335
+ @http.start()
336
+ @start_time_net = Time.now if @start_time_net.nil?
337
+ resp, data = @http.patch(path, data, headers_t)
338
+ end
339
+ manage_response(resp, data)
340
+ if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
341
+ if @num_redirects <= 30
342
+ @num_redirects += 1
343
+ current_server = "http"
344
+ current_server += "s" if @ssl == true
345
+ current_server += "://#{@host}"
346
+ location = @response[:location].gsub(current_server, "")
347
+ @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
348
+ get(location)
349
+ else
350
+ @logger.fatal "(#{@num_redirects}) Maximum number of redirections for a single request reached. Be sure everything is correct, it seems there is a non ending loop"
351
+ @num_redirects = 0
352
+ end
353
+ else
354
+ @num_redirects = 0
355
+ end
356
+ return @response
357
+ rescue Exception => stack
358
+ @logger.fatal stack
359
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
360
+ end
361
+ end
362
+
363
+ ######################################################
364
+ # Delete an existing resource
365
+ # @param arg [Hash] containing at least key :path
366
+ # @param arg [String] the path
367
+ #
368
+ # @return [Hash] response
369
+ # Including at least the symbol keys:
370
+ # :data = the response data body.
371
+ # :message = plain text response.
372
+ # :code = code response (200=ok,500=wrong...).
373
+ # All keys in response are lowercase.
374
+ # data, message and code can also be accessed as attributes like .message .code .data.
375
+ # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
376
+ # @example
377
+ # resp = @http.delete(Requests::Customer.remove_session)
378
+ # assert resp.code == 204
379
+ ######################################################
380
+ def delete(argument)
381
+ begin
382
+ if argument.kind_of?(String)
383
+ argument = {:path => argument}
384
+ end
385
+ path, data, headers_t = manage_request(argument)
386
+ @start_time = Time.now if @start_time.nil?
387
+ if argument.kind_of?(Hash)
388
+ arg = argument
389
+ if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
390
+ data = ""
391
+ if arg[:mock_response].keys.include?(:data)
392
+ data = arg[:mock_response][:data]
393
+ if data.kind_of?(Hash) #to json
394
+ begin
395
+ require "json"
396
+ data = data.to_json
397
+ rescue
398
+ @logger.fatal "There was a problem converting to json: #{data}"
399
+ end
400
+ end
401
+ end
402
+ @logger.warn "Pay attention!!! This is a mock response:"
403
+ @start_time_net = Time.now if @start_time_net.nil?
404
+ manage_response(arg[:mock_response], data.to_s)
405
+ return @response
406
+ end
407
+ end
408
+
409
+ begin
410
+ @start_time_net = Time.now if @start_time_net.nil?
411
+ resp = @http.delete(path, headers_t)
412
+ data = resp.body
413
+ rescue Exception => stack
414
+ @logger.warn stack
415
+ @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
416
+ @http.finish()
417
+ @http.start()
418
+ @start_time_net = Time.now if @start_time_net.nil?
419
+ resp, data = @http.delete(path)
420
+ end
421
+ manage_response(resp, data)
422
+
423
+ return @response
424
+ rescue Exception => stack
425
+ @logger.fatal stack
426
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ""}
427
+ end
428
+ end
429
+
430
+ ######################################################
431
+ # Implementation of the http HEAD method.
432
+ # Asks for the response identical to the one that would correspond to a GET request, but without the response body.
433
+ # This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
434
+ # @param arg [Hash] containing at least key :path
435
+ # @param arg [String] the path
436
+ #
437
+ # @return [Hash] response
438
+ # Including at least the symbol keys:
439
+ # :message = plain text response.
440
+ # :code = code response (200=ok,500=wrong...).
441
+ # All keys in response are lowercase.
442
+ # message and code can also be accessed as attributes like .message .code.
443
+ # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil }
444
+ ######################################################
445
+ def head(argument)
446
+ begin
447
+ if argument.kind_of?(String)
448
+ argument = {:path => argument}
449
+ end
450
+ path, data, headers_t = manage_request(argument)
451
+ @start_time = Time.now if @start_time.nil?
452
+ if argument.kind_of?(Hash)
453
+ arg = argument
454
+ if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
455
+ @logger.warn "Pay attention!!! This is a mock response:"
456
+ @start_time_net = Time.now if @start_time_net.nil?
457
+ manage_response(arg[:mock_response], "")
458
+ return @response
459
+ end
460
+ end
461
+
462
+ begin
463
+ @start_time_net = Time.now if @start_time_net.nil?
464
+ resp = @http.head(path, headers_t)
465
+ data = resp.body
466
+ rescue Exception => stack
467
+ @logger.warn stack
468
+ @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
469
+ @http.finish()
470
+ @http.start()
471
+ @start_time_net = Time.now if @start_time_net.nil?
472
+ resp, data = @http.head(path)
473
+ end
474
+ manage_response(resp, data)
475
+ return @response
476
+ rescue Exception => stack
477
+ @logger.fatal stack
478
+ return {fatal_error: stack.to_s, code: nil, message: nil}
479
+ end
480
+ end
481
+
482
+ ######################################################
483
+ # It will send the request depending on the :method declared on the request hash
484
+ # Take a look at https://github.com/MarioRuiz/Request-Hash
485
+ #
486
+ # @param request_hash [Hash] containing at least key :path and :method. The methods that are accepted are: :get, :head, :post, :put, :delete, :patch
487
+ #
488
+ # @return [Hash] response
489
+ # Including at least the symbol keys:
490
+ # :data = the response data body.
491
+ # :message = plain text response.
492
+ # :code = code response (200=ok,500=wrong...).
493
+ # All keys in response are lowercase.
494
+ # data, message and code can also be accessed as attributes like .message .code .data.
495
+ # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
496
+ # @example
497
+ # resp = @http.send_request Requests::Customer.remove_session
498
+ # assert resp.code == 204
499
+ ######################################################
500
+ def send_request(request_hash)
501
+ unless request_hash.is_a?(Hash) and request_hash.key?(:method) and request_hash.key?(:path) and
502
+ request_hash[:method].is_a?(Symbol) and
503
+ [:get, :head, :post, :put, :delete, :patch].include?(request_hash[:method])
504
+ message = "send_request: it needs to be supplied a Request Hash that includes a :method and :path. "
505
+ message += "Supported methods: :get, :head, :post, :put, :delete, :patch"
506
+ @logger.fatal message
507
+ return {fatal_error: message, code: nil, message: nil}
508
+ else
509
+ case request_hash[:method]
510
+ when :get
511
+ resp = get request_hash
512
+ when :post
513
+ resp = post request_hash
514
+ when :head
515
+ resp = head request_hash
516
+ when :put
517
+ resp = put request_hash
518
+ when :delete
519
+ resp = delete request_hash
520
+ when :patch
521
+ resp = patch request_hash
522
+ end
523
+ return resp
524
+ end
525
+ end
526
+ end