nice_http 1.8.10 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,686 +0,0 @@
1
- module NiceHttpHttpMethods
2
-
3
- ######################################################
4
- # Get data from path
5
- #
6
- # @param arg [Hash, String] hash containing at least key :path or a string with the path
7
- # @param save_data [String] the path or path and file name where we want to save the response data
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
- # @example
25
- # resp = @http.get("/assets/images/logo.png", save_data: './tmp/')
26
- # @example
27
- # resp = @http.get("/assets/images/logo.png", save_data: './tmp/example.png')
28
- ######################################################
29
- def get(arg, save_data: '')
30
- begin
31
- path, data, headers_t = manage_request(arg)
32
-
33
- @start_time = Time.now if @start_time.nil?
34
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
35
- data = ""
36
- if arg[:mock_response].keys.include?(:data)
37
- data = arg[:mock_response][:data]
38
- if data.kind_of?(Hash) #to json
39
- begin
40
- require "json"
41
- data = data.to_json
42
- rescue
43
- @logger.fatal "There was a problem converting to json: #{data}"
44
- end
45
- end
46
- end
47
- @logger.warn "Pay attention!!! This is a mock response:"
48
- @start_time_net = Time.now if @start_time_net.nil?
49
- manage_response(arg[:mock_response], data.to_s)
50
- return @response
51
- end
52
- begin
53
- if path.start_with?("http:") or path.start_with?("https:") #server included on path problably because of a redirection to a different server
54
- require "uri"
55
- uri = URI.parse(path)
56
- ssl = false
57
- ssl = true if path.include?("https:")
58
-
59
- server = "http://"
60
- server = "https://" if path.start_with?("https:")
61
- if uri.port != 443
62
- server += "#{uri.host}:#{uri.port}"
63
- else
64
- server += "#{uri.host}"
65
- end
66
-
67
- http_redir = nil
68
- self.class.connections.each { |conn|
69
- if conn.host == uri.host and conn.port == uri.port
70
- http_redir = conn
71
- break
72
- end
73
- }
74
-
75
- if !http_redir.nil?
76
- path, data, headers_t = manage_request(arg)
77
- http_redir.cookies.merge!(@cookies)
78
- http_redir.headers.merge!(headers_t)
79
- #todo: remove only the server at the begining in case in query is the server it will be replaced when it should not be
80
- resp = http_redir.get(path.gsub(server, ""))
81
- @response = http_redir.response
82
- else
83
- @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."
84
- end
85
- else
86
- @start_time_net = Time.now if @start_time_net.nil?
87
- resp = @http.get(path, headers_t)
88
- if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
89
- try = false
90
- @headers_orig.each do |k,v|
91
- if v.is_a?(Proc) and headers_t.key?(k)
92
- try = true
93
- headers_t[k] = v.call
94
- end
95
- end
96
- if try
97
- @logger.warn "Not authorized. Trying to generate a new token."
98
- resp = @http.get(path, headers_t)
99
- end
100
- end
101
- data = resp.body
102
- manage_response(resp, data)
103
- end
104
- rescue Exception => stack
105
- @logger.warn stack
106
- if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
107
- @logger.warn "The connection seems to be closed in the host machine. Timeout."
108
- return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
109
- else
110
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
111
- @http.finish()
112
- @http.start()
113
- @start_time_net = Time.now if @start_time_net.nil?
114
- @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
115
- resp = @http.get(path, headers_t)
116
- data = resp.body
117
- manage_response(resp, data)
118
- end
119
- end
120
- if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
121
- if @num_redirects <= 30
122
- @num_redirects += 1
123
- current_server = "http"
124
- current_server += "s" if @ssl == true
125
- current_server += "://#{@host}"
126
- location = @response[:location].gsub(current_server, "")
127
- @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
128
- get(location)
129
- else
130
- @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"
131
- @num_redirects = 0
132
- end
133
- else
134
- @num_redirects = 0
135
- end
136
- if save_data!=''
137
- require 'pathname'
138
- pn_get = Pathname.new(path)
139
-
140
- if Dir.exist?(save_data)
141
- save = save_data + "/" + pn_get.basename.to_s
142
- elsif save_data[-1]=="/"
143
- save = save_data + pn_get.basename.to_s
144
- else
145
- save = save_data
146
- end
147
- if Dir.exist?(Pathname.new(save).dirname)
148
- File.open(save, 'wb') { |fp| fp.write(@response.data) }
149
- else
150
- @logger.fatal "The folder #{Pathname.new(save).dirname} doesn't exist"
151
- end
152
- end
153
- return @response
154
- rescue Exception => stack
155
- @logger.fatal stack
156
- return { fatal_error: stack.to_s, code: nil, message: nil, data: "" }
157
- end
158
- end
159
-
160
- ######################################################
161
- # Post data to path
162
- # @param arguments [Hash] containing at least keys :data and :path.
163
- # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
164
- # @param arguments [Array<path, data, additional_headers>]
165
- # path (string).
166
- # data (json data for example).
167
- # additional_headers (Hash key=>value).
168
- # @return [Hash] response
169
- # Including at least the symbol keys:
170
- # :data = the response data body.
171
- # :message = plain text response.
172
- # :code = code response (200=ok,500=wrong...).
173
- # All keys in response are lowercase.
174
- # data, message and code can also be accessed as attributes like .message .code .data.
175
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
176
- # @example
177
- # resp = @http.post(Requests::Customer.update_customer)
178
- # assert resp.code == 201
179
- # @example
180
- # resp = http.post( {
181
- # path: "/api/users",
182
- # data: {name: "morpheus", job: "leader"}
183
- # } )
184
- # pp resp.data.json
185
- ######################################################
186
- def post(*arguments)
187
- begin
188
- path, data, headers_t = manage_request(*arguments)
189
- @start_time = Time.now if @start_time.nil?
190
- if arguments.size > 0 and arguments[0].kind_of?(Hash)
191
- arg = arguments[0]
192
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
193
- data = ""
194
- if arg[:mock_response].keys.include?(:data)
195
- data = arg[:mock_response][:data]
196
- if data.kind_of?(Hash) #to json
197
- begin
198
- require "json"
199
- data = data.to_json
200
- rescue
201
- @logger.fatal "There was a problem converting to json: #{data}"
202
- end
203
- end
204
- end
205
- @logger.warn "Pay attention!!! This is a mock response:"
206
- @start_time_net = Time.now if @start_time_net.nil?
207
- manage_response(arg[:mock_response], data.to_s)
208
- return @response
209
- end
210
- end
211
-
212
- begin
213
- @start_time_net = Time.now if @start_time_net.nil?
214
- if headers_t["Content-Type"] == "multipart/form-data"
215
- require "net/http/post/multipart"
216
- headers_t.each { |key, value|
217
- arguments[0][:data].add_field(key, value) #add to Headers
218
- }
219
- resp = @http.request(arguments[0][:data])
220
- elsif headers_t["Content-Type"].to_s.include?("application/x-www-form-urlencoded")
221
- encoded_form = URI.encode_www_form(arguments[0][:data])
222
- resp = @http.request_post(path, encoded_form, headers_t)
223
- data = resp.body
224
- else
225
- resp = @http.post(path, data, headers_t)
226
- #todo: do it also for forms and multipart
227
- if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
228
- try = false
229
- @headers_orig.each do |k,v|
230
- if v.is_a?(Proc) and headers_t.key?(k)
231
- try = true
232
- headers_t[k] = v.call
233
- end
234
- end
235
- if try
236
- @logger.warn "Not authorized. Trying to generate a new token."
237
- resp = @http.post(path, data, headers_t)
238
- end
239
- end
240
- data = resp.body
241
- end
242
- rescue Exception => stack
243
- @logger.warn stack
244
- if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
245
- @logger.warn "The connection seems to be closed in the host machine. Timeout."
246
- return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
247
- else
248
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
249
- @http.finish()
250
- @http.start()
251
- @start_time_net = Time.now if @start_time_net.nil?
252
- @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
253
- resp, data = @http.post(path, data, headers_t)
254
- end
255
- end
256
- manage_response(resp, data)
257
- if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
258
- if @num_redirects <= 30
259
- @num_redirects += 1
260
- current_server = "http"
261
- current_server += "s" if @ssl == true
262
- current_server += "://#{@host}"
263
- location = @response[:location].gsub(current_server, "")
264
- @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
265
- get(location)
266
- else
267
- @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"
268
- @num_redirects = 0
269
- end
270
- else
271
- @num_redirects = 0
272
- end
273
- return @response
274
- rescue Exception => stack
275
- @logger.fatal stack
276
- return { fatal_error: stack.to_s, code: nil, message: nil, data: "" }
277
- end
278
- end
279
-
280
- ######################################################
281
- # Put data to path
282
- # @param arguments [Hash] containing at least keys :data and :path.
283
- # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
284
- # @param arguments [Array<path, data, additional_headers>]
285
- # path (string).
286
- # data (json data for example).
287
- # additional_headers (Hash key=>value).
288
- # @return [Hash] response
289
- # Including at least the symbol keys:
290
- # :data = the response data body.
291
- # :message = plain text response.
292
- # :code = code response (200=ok,500=wrong...).
293
- # All keys in response are lowercase.
294
- # data, message and code can also be accessed as attributes like .message .code .data.
295
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
296
- # @example
297
- # resp = @http.put(Requests::Customer.remove_phone)
298
- ######################################################
299
- def put(*arguments)
300
- begin
301
- path, data, headers_t = manage_request(*arguments)
302
- @start_time = Time.now if @start_time.nil?
303
- if arguments.size > 0 and arguments[0].kind_of?(Hash)
304
- arg = arguments[0]
305
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
306
- data = ""
307
- if arg[:mock_response].keys.include?(:data)
308
- data = arg[:mock_response][:data]
309
- if data.kind_of?(Hash) #to json
310
- begin
311
- require "json"
312
- data = data.to_json
313
- rescue
314
- @logger.fatal "There was a problem converting to json: #{data}"
315
- end
316
- end
317
- end
318
- @logger.warn "Pay attention!!! This is a mock response:"
319
- @start_time_net = Time.now if @start_time_net.nil?
320
- manage_response(arg[:mock_response], data.to_s)
321
- return @response
322
- end
323
- end
324
-
325
- begin
326
- @start_time_net = Time.now if @start_time_net.nil?
327
- resp = @http.send_request("PUT", path, data, headers_t)
328
- if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
329
- try = false
330
- @headers_orig.each do |k,v|
331
- if v.is_a?(Proc) and headers_t.key?(k)
332
- try = true
333
- headers_t[k] = v.call
334
- end
335
- end
336
- if try
337
- @logger.warn "Not authorized. Trying to generate a new token."
338
- resp = @http.send_request("PUT", path, data, headers_t)
339
- end
340
- end
341
- data = resp.body
342
- rescue Exception => stack
343
- @logger.warn stack
344
- if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
345
- @logger.warn "The connection seems to be closed in the host machine. Timeout."
346
- return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
347
- else
348
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
349
- @http.finish()
350
- @http.start()
351
- @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
352
- @start_time_net = Time.now if @start_time_net.nil?
353
- resp, data = @http.send_request("PUT", path, data, headers_t)
354
- end
355
- end
356
- manage_response(resp, data)
357
-
358
- return @response
359
- rescue Exception => stack
360
- @logger.fatal stack
361
- return { fatal_error: stack.to_s, code: nil, message: nil, data: "" }
362
- end
363
- end
364
-
365
- ######################################################
366
- # Patch data to path
367
- #
368
- # @param arguments [Hash] containing at least keys :data and :path.
369
- # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
370
- # @param arguments [Array<path, data, additional_headers>]
371
- # path (string).
372
- # data (json data for example).
373
- # additional_headers (Hash key=>value).
374
- # @return [Hash] response
375
- # Including at least the symbol keys:
376
- # :data = the response data body.
377
- # :message = plain text response.
378
- # :code = code response (200=ok,500=wrong...).
379
- # All keys in response are lowercase.
380
- # data, message and code can also be accessed as attributes like .message .code .data.
381
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
382
- # @example
383
- # resp = @http.patch(Requests::Customer.unrelease_account)
384
- ######################################################
385
- def patch(*arguments)
386
- begin
387
- path, data, headers_t = manage_request(*arguments)
388
- @start_time = Time.now if @start_time.nil?
389
- if arguments.size > 0 and arguments[0].kind_of?(Hash)
390
- arg = arguments[0]
391
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
392
- data = ""
393
- if arg[:mock_response].keys.include?(:data)
394
- data = arg[:mock_response][:data]
395
- if data.kind_of?(Hash) #to json
396
- begin
397
- require "json"
398
- data = data.to_json
399
- rescue
400
- @logger.fatal "There was a problem converting to json: #{data}"
401
- end
402
- end
403
- end
404
- @logger.warn "Pay attention!!! This is a mock response:"
405
- @start_time_net = Time.now if @start_time_net.nil?
406
- manage_response(arg[:mock_response], data.to_s)
407
- return @response
408
- end
409
- end
410
-
411
- begin
412
- @start_time_net = Time.now if @start_time_net.nil?
413
- resp = @http.patch(path, data, headers_t)
414
- if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
415
- try = false
416
- @headers_orig.each do |k,v|
417
- if v.is_a?(Proc) and headers_t.key?(k)
418
- try = true
419
- headers_t[k] = v.call
420
- end
421
- end
422
- if try
423
- @logger.warn "Not authorized. Trying to generate a new token."
424
- resp = @http.patch(path, data, headers_t)
425
- end
426
- end
427
- data = resp.body
428
- rescue Exception => stack
429
- @logger.warn stack
430
- if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
431
- @logger.warn "The connection seems to be closed in the host machine. Timeout."
432
- return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
433
- else
434
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
435
- @http.finish()
436
- @http.start()
437
- @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
438
- @start_time_net = Time.now if @start_time_net.nil?
439
- resp, data = @http.patch(path, data, headers_t)
440
- end
441
- end
442
- manage_response(resp, data)
443
- if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location)
444
- if @num_redirects <= 30
445
- @num_redirects += 1
446
- current_server = "http"
447
- current_server += "s" if @ssl == true
448
- current_server += "://#{@host}"
449
- location = @response[:location].gsub(current_server, "")
450
- @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}"
451
- get(location)
452
- else
453
- @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"
454
- @num_redirects = 0
455
- end
456
- else
457
- @num_redirects = 0
458
- end
459
- return @response
460
- rescue Exception => stack
461
- @logger.fatal stack
462
- return { fatal_error: stack.to_s, code: nil, message: nil, data: "" }
463
- end
464
- end
465
-
466
- ######################################################
467
- # Delete an existing resource
468
- # @param argument [Hash, String] hash containing at least key :path or a string with the path
469
- #
470
- # @return [Hash] response
471
- # Including at least the symbol keys:
472
- # :data = the response data body.
473
- # :message = plain text response.
474
- # :code = code response (200=ok,500=wrong...).
475
- # All keys in response are lowercase.
476
- # data, message and code can also be accessed as attributes like .message .code .data.
477
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
478
- # @example
479
- # resp = @http.delete(Requests::Customer.remove_session)
480
- # assert resp.code == 204
481
- ######################################################
482
- def delete(argument)
483
- begin
484
- if argument.kind_of?(String)
485
- argument = { :path => argument }
486
- end
487
- path, data, headers_t = manage_request(argument)
488
- @start_time = Time.now if @start_time.nil?
489
- if argument.kind_of?(Hash)
490
- arg = argument
491
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
492
- data = ""
493
- if arg[:mock_response].keys.include?(:data)
494
- data = arg[:mock_response][:data]
495
- if data.kind_of?(Hash) #to json
496
- begin
497
- require "json"
498
- data = data.to_json
499
- rescue
500
- @logger.fatal "There was a problem converting to json: #{data}"
501
- end
502
- end
503
- end
504
- @logger.warn "Pay attention!!! This is a mock response:"
505
- @start_time_net = Time.now if @start_time_net.nil?
506
- manage_response(arg[:mock_response], data.to_s)
507
- return @response
508
- end
509
- end
510
-
511
- begin
512
- @start_time_net = Time.now if @start_time_net.nil?
513
- if data.to_s == ""
514
- resp = @http.delete(path, headers_t)
515
- if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
516
- try = false
517
- @headers_orig.each do |k,v|
518
- if v.is_a?(Proc) and headers_t.key?(k)
519
- try = true
520
- headers_t[k] = v.call
521
- end
522
- end
523
- if try
524
- @logger.warn "Not authorized. Trying to generate a new token."
525
- resp = @http.delete(path, headers_t)
526
- end
527
- end
528
- else
529
- request = Net::HTTP::Delete.new(path, headers_t)
530
- request.body = data
531
- resp = @http.request(request)
532
- if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
533
- try = false
534
- @headers_orig.each do |k,v|
535
- if v.is_a?(Proc) and headers_t.key?(k)
536
- try = true
537
- headers_t[k] = v.call
538
- end
539
- end
540
- if try
541
- @logger.warn "Not authorized. Trying to generate a new token."
542
- request = Net::HTTP::Delete.new(path, headers_t)
543
- request.body = data
544
- resp = @http.request(request)
545
- end
546
- end
547
- end
548
- data = resp.body
549
- rescue Exception => stack
550
- @logger.warn stack
551
- if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
552
- @logger.warn "The connection seems to be closed in the host machine. Timeout."
553
- return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
554
- else
555
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
556
- @http.finish()
557
- @http.start()
558
- @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
559
- @start_time_net = Time.now if @start_time_net.nil?
560
- resp, data = @http.delete(path, headers_t)
561
- end
562
- end
563
- manage_response(resp, data)
564
-
565
- return @response
566
- rescue Exception => stack
567
- @logger.fatal stack
568
- return { fatal_error: stack.to_s, code: nil, message: nil, data: "" }
569
- end
570
- end
571
-
572
- ######################################################
573
- # Implementation of the http HEAD method.
574
- # Asks for the response identical to the one that would correspond to a GET request, but without the response body.
575
- # This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
576
- # @param argument [Hash, String] hash containing at least key :path or directly an string with the path
577
- #
578
- # @return [Hash] response
579
- # Including at least the symbol keys:
580
- # :message = plain text response.
581
- # :code = code response (200=ok,500=wrong...).
582
- # All keys in response are lowercase.
583
- # message and code can also be accessed as attributes like .message .code.
584
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil }
585
- ######################################################
586
- def head(argument)
587
- begin
588
- if argument.kind_of?(String)
589
- argument = { :path => argument }
590
- end
591
- path, data, headers_t = manage_request(argument)
592
- @start_time = Time.now if @start_time.nil?
593
- if argument.kind_of?(Hash)
594
- arg = argument
595
- if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
596
- @logger.warn "Pay attention!!! This is a mock response:"
597
- @start_time_net = Time.now if @start_time_net.nil?
598
- manage_response(arg[:mock_response], "")
599
- return @response
600
- end
601
- end
602
-
603
- begin
604
- @start_time_net = Time.now if @start_time_net.nil?
605
- resp = @http.head(path, headers_t)
606
- if (resp.code == 401 or resp.code == 408) and @headers_orig.values.map(&:class).include?(Proc)
607
- try = false
608
- @headers_orig.each do |k,v|
609
- if v.is_a?(Proc) and headers_t.key?(k)
610
- try = true
611
- headers_t[k] = v.call
612
- end
613
- end
614
- if try
615
- @logger.warn "Not authorized. Trying to generate a new token."
616
- resp = @http.head(path, headers_t)
617
- end
618
- end
619
- data = resp.body
620
- rescue Exception => stack
621
- @logger.warn stack
622
- if !@timeout.nil? and (Time.now - @start_time_net) > @timeout
623
- @logger.warn "The connection seems to be closed in the host machine. Timeout."
624
- return { fatal_error: "Net::ReadTimeout", code: nil, message: nil, data: "" }
625
- else
626
- @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect"
627
- @http.finish()
628
- @http.start()
629
- @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)}
630
- @start_time_net = Time.now if @start_time_net.nil?
631
- resp, data = @http.head(path, headers_t)
632
- end
633
- end
634
- manage_response(resp, data)
635
- return @response
636
- rescue Exception => stack
637
- @logger.fatal stack
638
- return { fatal_error: stack.to_s, code: nil, message: nil }
639
- end
640
- end
641
-
642
- ######################################################
643
- # It will send the request depending on the :method declared on the request hash
644
- # Take a look at https://github.com/MarioRuiz/Request-Hash
645
- #
646
- # @param request_hash [Hash] containing at least key :path and :method. The methods that are accepted are: :get, :head, :post, :put, :delete, :patch
647
- #
648
- # @return [Hash] response
649
- # Including at least the symbol keys:
650
- # :data = the response data body.
651
- # :message = plain text response.
652
- # :code = code response (200=ok,500=wrong...).
653
- # All keys in response are lowercase.
654
- # data, message and code can also be accessed as attributes like .message .code .data.
655
- # In case of fatal error returns { fatal_error: "the error description", code: nil, message: nil, data: '' }
656
- # @example
657
- # resp = @http.send_request Requests::Customer.remove_session
658
- # assert resp.code == 204
659
- ######################################################
660
- def send_request(request_hash)
661
- unless request_hash.is_a?(Hash) and request_hash.key?(:method) and request_hash.key?(:path) and
662
- request_hash[:method].is_a?(Symbol) and
663
- [:get, :head, :post, :put, :delete, :patch].include?(request_hash[:method])
664
- message = "send_request: it needs to be supplied a Request Hash that includes a :method and :path. "
665
- message += "Supported methods: :get, :head, :post, :put, :delete, :patch"
666
- @logger.fatal message
667
- return { fatal_error: message, code: nil, message: nil }
668
- else
669
- case request_hash[:method]
670
- when :get
671
- resp = get request_hash
672
- when :post
673
- resp = post request_hash
674
- when :head
675
- resp = head request_hash
676
- when :put
677
- resp = put request_hash
678
- when :delete
679
- resp = delete request_hash
680
- when :patch
681
- resp = patch request_hash
682
- end
683
- return resp
684
- end
685
- end
686
- end