nice_http 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -3
  3. data/lib/nice_http.rb +104 -56
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b7b94ecb393c3395796636b8e9308bcea20dde7ca9e314ddeae7dd3129ac8bd
4
- data.tar.gz: c760b6830a3f2f6a1d681ef731df55d47f555c67a2a5b38709f1a61af5943ce9
3
+ metadata.gz: bc467413aa1e110d9ee4f26473a1a8777642836bc0c0187e00566fc2558fd80d
4
+ data.tar.gz: '04568929a3b8180fded025a5cc144ebdddfa25416ad0e16455aa076febb8b1ae'
5
5
  SHA512:
6
- metadata.gz: 87ab72b2e991b77c83a82a12e114ed070544202805afa5aeceec1ae07fa0cd28a2ca514e5118b2a5bfb5a5832cb39477feeeb57df1f8bf156394a5e5a76b3db3
7
- data.tar.gz: 30bbd18f121e0502e83ea5bacce0c998cc1aafcb7fa23162333028484873379653c054dfc7f203dadd74e37d5d2702913e453abe721a4d03ec5c45ee02ef8c39
6
+ metadata.gz: c6d3572e818f3939c9bade93bcd1d7b4e4198e0da5fa2913f346b32ecdc5694cdd7df29337784d1453c080b15190e4fe2e477ba591102e0459ca32925e0117bc
7
+ data.tar.gz: ab2b3cb27234d48fefea4b1c4ef61c123519450b202829dbc3d32bcfac1b91b29bebec1ae09dfa01160c77a9acd2e850e82114a45e1887ca65e7fe2d0651893c
data/README.md CHANGED
@@ -64,10 +64,11 @@ http3 = NiceHttp.new my_reqres_server
64
64
  ```
65
65
 
66
66
 
67
- You can specify all the defaults you will be using when creating connections by using the NiceHttp methods, in this example, http1 and http2 will be connecting to reqres.in and http3 to example.com:
67
+ You can specify all the defaults you will be using when creating connections by using the NiceHttp methods, in this example, http1 and http2 will be connecting to reqres.in with the default parameters and http3 to example.com:
68
68
 
69
69
  ```ruby
70
70
 
71
+ # default parameters
71
72
  NiceHttp.host = 'reqres.in'
72
73
  NiceHttp.ssl = true
73
74
  NiceHttp.port = 443
@@ -83,6 +84,20 @@ http3 = NiceHttp.new("https://example.com")
83
84
 
84
85
  ```
85
86
 
87
+ If you prefer to supply a hash to change the default settings for NiceHttp:
88
+
89
+ ```ruby
90
+ NiceHttp.defaults = {
91
+ host: 'reqres.in',
92
+ ssl: true,
93
+ port: 443,
94
+ debug: false,
95
+ log: "./my_logs.log",
96
+ headers: {"api-key": "the api key"}
97
+ }
98
+ ```
99
+
100
+
86
101
  ## Creating requests
87
102
 
88
103
  You can use hash requests to simplify the management of your requests, for example creating a file specifying all the requests for your Customers API.
@@ -161,12 +176,12 @@ pp resp.data.json(:job)
161
176
  ```
162
177
 
163
178
 
164
- In case you want to modify the request before sending it, for example just changing one field but the rest will be the same, you can supply a new key :values in the request hash that will contain a hash with the keys to be changed and NiceHttp will perform the necessary changes at any level:
179
+ In case you want to modify the request before sending it, for example just changing one field but the rest will be the same, you can supply a new key :values_at in the request hash that will contain a hash with the keys to be changed and NiceHttp will perform the necessary changes at any level:
165
180
 
166
181
  ```ruby
167
182
 
168
183
  req = Requests::Example.create_user_hash
169
- req[:values] = {job: "developer"}
184
+ req.values_at = {job: "developer"}
170
185
 
171
186
  resp = http.post req
172
187
 
@@ -2,7 +2,43 @@ require 'logger'
2
2
  require 'nice_hash'
3
3
  require_relative 'nice_http/utils'
4
4
 
5
- class NiceHttp
5
+ ######################################################
6
+ # Attributes you can access using NiceHttp.the_attribute:
7
+ # :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port,
8
+ # :last_request, :last_response, :request_id, :use_mocks, :connections,
9
+ # :active, :auto_redirect
10
+ #
11
+ # @attr [String] host The host to be accessed
12
+ # @attr [Integer] port The port number
13
+ # @attr [Boolean] ssl If you use ssl or not
14
+ # @attr [Hash] headers Contains the headers you will be using on your connection
15
+ # @attr [Boolean] debug In case true shows all the details of the communication with the host
16
+ # @attr [String, Symbol] log :fix_file, :no, :screen, :file, "path and file name".
17
+ # :fix_file will log the communication on nice_http.log. (default).
18
+ # :no will not generate any logs.
19
+ # :screen will print the logs on the screen.
20
+ # :file will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log.
21
+ # String the path and file name where the logs will be stored.
22
+ # @attr [String] proxy_host the proxy host to be used
23
+ # @attr [Integer] proxy_port the proxy port to be used
24
+ # @attr [String] last_request The last request with all the content sent
25
+ # @attr [String] last_response Only in case :debug is true, the last response with all the content
26
+ # @attr [String] request_id If the response includes a requestId, will be stored here
27
+ # @attr [Boolean] use_mocks If true, in case the request hash includes a :mock_response key, it will be used as the response instead
28
+ # @attr [Array] connections It will include all the active connections (NiceHttp instances)
29
+ # @attr [Integer] active Number of active connections
30
+ # @attr [Boolean] auto_redirect If true, NiceHttp will take care of the auto redirections when required by the responses
31
+ # @attr [Hash] response Contains the full response hash
32
+ # @attr [Integer] num_redirects Number of consecutive redirections managed
33
+ # @attr [Hash] headers The updated headers of the communication
34
+ # @attr [Hash] cookies Cookies set. The key is the path (String) where cookies are set and the value a Hash with pairs of cookie keys and values, example:
35
+ # { '/' => { "cfid" => "d95adfas2550255", "amddom.settings" => "doom" } }
36
+ # @attr [Logger] logger An instance of the Logger class where logs will be stored. You can access on anytime to store specific data, for example:
37
+ # my_http.logger.info "add this to the log file"
38
+ # @see https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html
39
+ ######################################################
40
+ class NiceHttp
41
+
6
42
  class << self
7
43
  attr_accessor :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port,
8
44
  :last_request, :last_response, :request_id, :use_mocks, :connections,
@@ -27,6 +63,11 @@ class NiceHttp
27
63
  attr_reader :host, :port, :ssl, :debug, :log, :proxy_host, :proxy_port, :response, :num_redirects
28
64
  attr_accessor :headers, :cookies, :use_mocks, :auto_redirect, :logger
29
65
 
66
+ ######################################################
67
+ # Change the default values for NiceHttp supplying a Hash
68
+ #
69
+ # @param par [Hash] keys: :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port, :use_mocks, :auto_redirect
70
+ ######################################################
30
71
  def self.defaults=(par = {})
31
72
  @host = par[:host] if par.key?(:host)
32
73
  @port = par[:port] if par.key?(:port)
@@ -36,7 +77,7 @@ class NiceHttp
36
77
  @log = par[:log] if par.key?(:log)
37
78
  @proxy_host = par[:proxy_host] if par.key?(:proxy_host)
38
79
  @proxy_port = par[:proxy_port] if par.key?(:proxy_port)
39
- @proxy_port = par[:use_mocks] if par.key?(:use_mocks)
80
+ @use_mocks = par[:use_mocks] if par.key?(:use_mocks)
40
81
  @auto_redirect = par[:auto_redirect] if par.key?(:auto_redirect)
41
82
  end
42
83
 
@@ -61,7 +102,7 @@ class NiceHttp
61
102
  #
62
103
  # ssl -- true, false (default)
63
104
  #
64
- # headers -- hash with the header key:values
105
+ # headers -- hash with the headers
65
106
  #
66
107
  # debug -- true, false (default)
67
108
  #
@@ -208,8 +249,8 @@ class NiceHttp
208
249
  # :message = plain text response.
209
250
  # :code = code response (200=ok,500=wrong...).
210
251
  # All keys in response are lowercase.
211
- # data, message and code can also be accessed as attributes like .message .code .data
212
- # @return [Symbol] in case of error returns :error
252
+ # data, message and code can also be accessed as attributes like .message .code .data.
253
+ # In case of fatal error returns {fatal_error: "the error description", code: nil, message: nil, data: ''}
213
254
  #
214
255
  # @example
215
256
  # resp = @http.get(Requests::Customer.get_profile)
@@ -309,25 +350,26 @@ class NiceHttp
309
350
  return @response
310
351
  rescue Exception => stack
311
352
  @logger.fatal stack
312
- return :error
353
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ''}
313
354
  end
314
355
  end
315
356
 
316
357
  ######################################################
317
358
  # Post data to path
318
- # @param arguments [Hash] containing at least keys :data and :path
319
- # @param arguments [Array<path, data ,additional_headers]
320
- # path (string)
321
- # data (json data for example)
322
- # additional_headers (Hash key=>value)
359
+ # @param arguments [Hash] containing at least keys :data and :path.
360
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
361
+ # @param arguments [Array<path, data, additional_headers>]
362
+ # path (string).
363
+ # data (json data for example).
364
+ # additional_headers (Hash key=>value).
323
365
  # @return [Hash] response
324
366
  # Including at least the symbol keys:
325
367
  # :data = the response data body.
326
368
  # :message = plain text response.
327
369
  # :code = code response (200=ok,500=wrong...).
328
370
  # All keys in response are lowercase.
329
- # data, message and code can also be accessed as attributes like .message .code .data
330
- # @return [Symbol] in case of error returns :error
371
+ # data, message and code can also be accessed as attributes like .message .code .data.
372
+ # In case of fatal error returns {fatal_error: "the error description", code: nil, message: nil, data: ''}
331
373
  # @example
332
374
  # resp = @http.post(Requests::Customer.update_customer)
333
375
  # assert resp.code == 201
@@ -403,27 +445,28 @@ class NiceHttp
403
445
  end
404
446
  return @response
405
447
  rescue Exception => stack
406
- @logger.warn stack
407
- return :error
448
+ @logger.fatal stack
449
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ''}
408
450
  end
409
451
 
410
452
  end
411
453
 
412
454
  ######################################################
413
455
  # Put data to path
414
- # @param arguments [Hash] containing at least keys :data and :path
415
- # @param arguments [Array<path, data ,additional_headers]
416
- # path (string)
417
- # data (json data for example)
418
- # additional_headers (Hash key=>value)
456
+ # @param arguments [Hash] containing at least keys :data and :path.
457
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
458
+ # @param arguments [Array<path, data, additional_headers>]
459
+ # path (string).
460
+ # data (json data for example).
461
+ # additional_headers (Hash key=>value).
419
462
  # @return [Hash] response
420
463
  # Including at least the symbol keys:
421
464
  # :data = the response data body.
422
465
  # :message = plain text response.
423
466
  # :code = code response (200=ok,500=wrong...).
424
467
  # All keys in response are lowercase.
425
- # data, message and code can also be accessed as attributes like .message .code .data
426
- # @return [Symbol] in case of error returns :error
468
+ # data, message and code can also be accessed as attributes like .message .code .data.
469
+ # In case of fatal error returns {fatal_error: "the error description", code: nil, message: nil, data: ''}
427
470
  # @example
428
471
  # resp = @http.put(Requests::Customer.remove_phone)
429
472
  ######################################################
@@ -469,8 +512,8 @@ class NiceHttp
469
512
 
470
513
  return @response
471
514
  rescue Exception => stack
472
- @logger.fatal stack, self
473
- return :error
515
+ @logger.fatal stack
516
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ''}
474
517
  end
475
518
 
476
519
  end
@@ -478,19 +521,20 @@ class NiceHttp
478
521
 
479
522
  ######################################################
480
523
  # Patch data to path
481
- # @param arguments [Hash] containing at least keys :data and :path
482
- # @param arguments [Array<path, data ,additional_headers]
483
- # path (string)
484
- # data (json data for example)
485
- # additional_headers (Hash key=>value)
524
+ # @param arguments [Hash] containing at least keys :data and :path.
525
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
526
+ # @param arguments [Array<path, data, additional_headers>]
527
+ # path (string).
528
+ # data (json data for example).
529
+ # additional_headers (Hash key=>value).
486
530
  # @return [Hash] response
487
531
  # Including at least the symbol keys:
488
532
  # :data = the response data body.
489
533
  # :message = plain text response.
490
534
  # :code = code response (200=ok,500=wrong...).
491
535
  # All keys in response are lowercase.
492
- # data, message and code can also be accessed as attributes like .message .code .data
493
- # @return [Symbol] in case of error returns :error
536
+ # data, message and code can also be accessed as attributes like .message .code .data.
537
+ # In case of fatal error returns {fatal_error: "the error description", code: nil, message: nil, data: ''}
494
538
  # @example
495
539
  # resp = @http.patch(Requests::Customer.unrelease_account)
496
540
  ######################################################
@@ -552,7 +596,7 @@ class NiceHttp
552
596
  return @response
553
597
  rescue Exception => stack
554
598
  @logger.fatal stack
555
- return :error
599
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ''}
556
600
  end
557
601
 
558
602
  end
@@ -568,8 +612,8 @@ class NiceHttp
568
612
  # :message = plain text response.
569
613
  # :code = code response (200=ok,500=wrong...).
570
614
  # All keys in response are lowercase.
571
- # data, message and code can also be accessed as attributes like .message .code .data
572
- # @return [Symbol] in case of error returns :error
615
+ # data, message and code can also be accessed as attributes like .message .code .data.
616
+ # In case of fatal error returns {fatal_error: "the error description", code: nil, message: nil, data: ''}
573
617
  # @example
574
618
  # resp = @http.delete(Requests::Customer.remove_session)
575
619
  # assert resp.code == 204
@@ -620,7 +664,7 @@ class NiceHttp
620
664
  return @response
621
665
  rescue Exception => stack
622
666
  @logger.fatal stack
623
- return :error
667
+ return {fatal_error: stack.to_s, code: nil, message: nil, data: ''}
624
668
  end
625
669
 
626
670
  end
@@ -637,8 +681,8 @@ class NiceHttp
637
681
  # :message = plain text response.
638
682
  # :code = code response (200=ok,500=wrong...).
639
683
  # All keys in response are lowercase.
640
- # message and code can also be accessed as attributes like .message .code
641
- # @return [Symbol] in case of error returns :error
684
+ # message and code can also be accessed as attributes like .message .code.
685
+ # In case of fatal error returns {fatal_error: "the error description", code: nil, message: nil}
642
686
  ######################################################
643
687
  def head(argument)
644
688
  begin
@@ -685,7 +729,7 @@ class NiceHttp
685
729
  return @response
686
730
  rescue Exception => stack
687
731
  @logger.fatal stack
688
- return :error
732
+ return {fatal_error: stack.to_s, code: nil, message: nil}
689
733
  end
690
734
  end
691
735
 
@@ -733,6 +777,7 @@ class NiceHttp
733
777
  # input:
734
778
  # 3 args: path, data, headers
735
779
  # 1 arg: Hash containg at least keys :path and :data
780
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
736
781
  # output:
737
782
  # path, data, headers
738
783
  ######################################################
@@ -814,20 +859,23 @@ class NiceHttp
814
859
  headers_t['Content-Type'] = 'application/json'
815
860
  content_type_included=true
816
861
  end
817
-
862
+ # to be backwards compatible since before was :values
863
+ if arguments[0].include?(:values) and !arguments[0].include?(:values_at)
864
+ arguments[0][:values_at] = arguments[0][:values]
865
+ end
818
866
  if content_type_included and (!headers_t["Content-Type"][/text\/xml/].nil? or
819
867
  !headers_t["Content-Type"]["application/soap+xml"].nil? or
820
868
  !headers_t["Content-Type"][/application\/jxml/].nil?) then
821
- if arguments[0].include?(:values) then
822
- arguments[0][:values].each {|key, value|
869
+ if arguments[0].include?(:values_at) then
870
+ arguments[0][:values_at].each {|key, value|
823
871
  data=NiceHttpUtils.set_value_xml_tag(key.to_s(), data, value.to_s(), true)
824
872
  }
825
873
  end
826
874
  elsif content_type_included and !headers_t["Content-Type"][/application\/json/].nil? and data.to_s()!="" then
827
875
  require 'json'
828
876
  if data.kind_of?(String) then
829
- if arguments[0].include?(:values) then
830
- arguments[0][:values].each {|key, value|
877
+ if arguments[0].include?(:values_at) then
878
+ arguments[0][:values_at].each {|key, value|
831
879
  data.gsub!(/(( *|^)"?#{key.to_s()}"? *: *")(.*)(" *, *$)/, '\1' + value+ '\4') # "key":"value", or key:"value",
832
880
  data.gsub!(/(( *|^)"?#{key.to_s()}"? *: *")(.*)(" *$)/, '\1' + value+ '\4') # "key":"value" or key:"value"
833
881
  data.gsub!(/(( *|^)"?#{key.to_s()}"? *: *[^"])([^"].*)([^"] *, *$)/, '\1' + value+ '\4') # "key":456, or key:456,
@@ -839,10 +887,10 @@ class NiceHttp
839
887
  data.each {|key, value|
840
888
  data_n[key.to_s()]=value
841
889
  }
842
- if arguments[0].include?(:values) then
843
- #req[:values][:loginName] or req[:values]["loginName"]
890
+ if arguments[0].include?(:values_at) then
891
+ #req[:values_at][:loginName] or req[:values_at]["loginName"]
844
892
  new_values_hash=Hash.new()
845
- arguments[0][:values].each {|kv, vv|
893
+ arguments[0][:values_at].each {|kv, vv|
846
894
  if data_n.keys.include?(kv.to_s()) then
847
895
  new_values_hash[kv.to_s()]=vv
848
896
  end
@@ -861,18 +909,18 @@ class NiceHttp
861
909
  row.each {|key, value|
862
910
  data_n[key.to_s()]=value
863
911
  }
864
- if arguments[0].include?(:values) then
865
- #req[:values][:loginName] or req[:values]["loginName"]
912
+ if arguments[0].include?(:values_at) then
913
+ #req[:values_at][:loginName] or req[:values_at]["loginName"]
866
914
  new_values_hash=Hash.new()
867
- if arguments[0][:values].kind_of?(Hash) then #values[:mykey][3]
868
- arguments[0][:values].each {|kv, vv|
915
+ if arguments[0][:values_at].kind_of?(Hash) then #values[:mykey][3]
916
+ arguments[0][:values_at].each {|kv, vv|
869
917
  if data_n.keys.include?(kv.to_s()) and !vv[indx].nil? then
870
918
  new_values_hash[kv.to_s()]=vv[indx]
871
919
  end
872
920
  }
873
- elsif arguments[0][:values].kind_of?(Array) then #values[5][:mykey]
874
- if !arguments[0][:values][indx].nil? then
875
- arguments[0][:values][indx].each {|kv, vv|
921
+ elsif arguments[0][:values_at].kind_of?(Array) then #values[5][:mykey]
922
+ if !arguments[0][:values_at][indx].nil? then
923
+ arguments[0][:values_at][indx].each {|kv, vv|
876
924
  if data_n.keys.include?(kv.to_s()) then
877
925
  new_values_hash[kv.to_s()]=vv
878
926
  end
@@ -891,10 +939,10 @@ class NiceHttp
891
939
  @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
892
940
  return :error, :error, :error
893
941
  end
894
- elsif content_type_included and arguments[0].include?(:values) then
895
- if arguments[0][:values].kind_of?(Hash) and arguments[0][:values].keys.size>0 then
942
+ elsif content_type_included and arguments[0].include?(:values_at) then
943
+ if arguments[0][:values_at].kind_of?(Hash) and arguments[0][:values_at].keys.size>0 then
896
944
  if !headers_t.nil? and headers_t.kind_of?(Hash) and headers_t["Content-Type"]!="application/x-www-form-urlencoded" and headers_t["content-type"]!="application/x-www-form-urlencoded" then
897
- @logger.warn(":values key given without a valid content-type or data for request. No values modified on the request")
945
+ @logger.warn(":values_at key given without a valid content-type or data for request. No values modified on the request")
898
946
  end
899
947
  end
900
948
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice_http
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-14 00:00:00.000000000 Z
11
+ date: 2019-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nice_hash
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.7.2
19
+ version: 1.7.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.7.2
26
+ version: 1.7.3
27
27
  description: NiceHttp -- simplest library for accessing and testing HTTP and REST
28
28
  resources. Manage different hosts on the fly. Easily get the value you want from
29
29
  the JSON strings. Use hashes on your requests.