nice_http 1.6.4 → 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46d0a610c87e7bd8c88e34a5825b9fe6a9e06a33299f88585c97e3bf4489e961
4
- data.tar.gz: eb7977a7f5da3eb1abb76e93fd89d15a697137830c9ee8c2d54951ac04f78093
3
+ metadata.gz: e9134ed46c6d297b4e68c88b5e3e7e868100301bf24e3faaaa2aa17e0d75a989
4
+ data.tar.gz: 9ab95966908fb2365dd6573c9ddfbda4cb429741f06989fe589fb4176b4f927b
5
5
  SHA512:
6
- metadata.gz: a9461df52a09637c5be242e95680e7b3f1ba38de8d5b71fc95d7f706ea6519c479b4d55d75aeca2600927ca8343fcdc0efff4a97fd7deea99ccac7431ac67a05
7
- data.tar.gz: a84a1ec6d23ca1e103d792136072b1f5e498d86e2d04cf995bd21388e6b0740f1bf87ca19b8866c67e3ff8c94c7c7691ab714e440cd788fd6251290a4d405497
6
+ metadata.gz: 508f31aa41e757f510b95ae162ff145dce5e7a493fa0e09564414dae12b069fca20a4acb480ffd4633cb63b5cae993c2ad3620b3517e74f80e9e71b89714cd69
7
+ data.tar.gz: f99a4e393761bb8e69faf913ff22895f753dd531f2094b7d38eefaa66200c52bd3e454c95354823515ac0db2af3badd8e05b5dddab7a2445ed1e61f4949d5e20
@@ -43,7 +43,6 @@ require_relative "nice_http/http_methods"
43
43
  # @attr [Hash] values_for The default values to set on the data in case not specified others
44
44
  ######################################################
45
45
  class NiceHttp
46
-
47
46
  include NiceHttpManageRequest
48
47
  include NiceHttpManageResponse
49
48
  include NiceHttpHttpMethods
@@ -173,7 +172,7 @@ class NiceHttp
173
172
  require "net/https"
174
173
  @host = self.class.host
175
174
  @port = self.class.port
176
- @prepath = ''
175
+ @prepath = ""
177
176
  @ssl = self.class.ssl
178
177
  @headers = self.class.headers.dup
179
178
  @values_for = self.class.values_for.dup
@@ -196,7 +195,7 @@ class NiceHttp
196
195
  @host = uri.host unless uri.host.nil?
197
196
  @port = uri.port unless uri.port.nil?
198
197
  @ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
199
- @prepath = uri.path unless uri.path=='/'
198
+ @prepath = uri.path unless uri.path == "/"
200
199
  elsif args.is_a?(Hash) && !args.keys.empty?
201
200
  @host = args[:host] if args.keys.include?(:host)
202
201
  @port = args[:port] if args.keys.include?(:port)
@@ -212,21 +211,21 @@ class NiceHttp
212
211
  end
213
212
 
214
213
  begin
215
- log_filename =''
214
+ log_filename = ""
216
215
  if @log.kind_of?(String) or @log == :fix_file or @log == :file or @log == :file_run
217
216
  if @log.kind_of?(String)
218
217
  log_filename = @log
219
218
  elsif @log == :fix_file
220
- log_filename = 'nice_http.log'
219
+ log_filename = "nice_http.log"
221
220
  elsif @log == :file
222
221
  log_filename = "nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log"
223
222
  elsif @log == :file_run
224
223
  log_filename = "#{caller.first[/[^:]+/]}.log"
225
224
  end
226
225
  if self.class.log_files.key?(log_filename)
227
- @logger = self.class.log_files[log_filename]
226
+ @logger = self.class.log_files[log_filename]
228
227
  else
229
- f = File.new(log_filename, 'w')
228
+ f = File.new(log_filename, "w")
230
229
  f.sync = true
231
230
  @logger = Logger.new f
232
231
  self.class.log_files[log_filename] = @logger
@@ -235,7 +234,7 @@ class NiceHttp
235
234
  @logger = Logger.new STDOUT
236
235
  elsif @log == :no
237
236
  @logger = Logger.new nil
238
- else
237
+ else
239
238
  raise InfoMissing, :log
240
239
  end
241
240
  @logger.level = Logger::INFO
@@ -244,13 +243,12 @@ class NiceHttp
244
243
  raise InfoMissing, :log
245
244
  end
246
245
 
247
-
248
246
  if @host.to_s != "" and (@host.start_with?("http:") or @host.start_with?("https:"))
249
247
  uri = URI.parse(@host)
250
248
  @host = uri.host unless uri.host.nil?
251
249
  @port = uri.port unless uri.port.nil?
252
250
  @ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
253
- @prepath = uri.path unless uri.path=='/'
251
+ @prepath = uri.path unless uri.path == "/"
254
252
  end
255
253
 
256
254
  raise InfoMissing, :port if @port.to_s == ""
@@ -261,7 +259,7 @@ class NiceHttp
261
259
  raise InfoMissing, :use_mocks unless @use_mocks.is_a?(TrueClass) or @use_mocks.is_a?(FalseClass)
262
260
  raise InfoMissing, :headers unless @headers.is_a?(Hash)
263
261
  raise InfoMissing, :values_for unless @values_for.is_a?(Hash)
264
-
262
+
265
263
  begin
266
264
  if !@proxy_host.nil? && !@proxy_port.nil?
267
265
  @http = Net::HTTP::Proxy(@proxy_host, @proxy_port).new(@host, @port)
@@ -25,7 +25,7 @@ module NiceHttpHttpMethods
25
25
  def get(arg)
26
26
  begin
27
27
  path, data, headers_t = manage_request(arg)
28
-
28
+
29
29
  @start_time = Time.now if @start_time.nil?
30
30
  if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response)
31
31
  data = ""
@@ -73,7 +73,7 @@ module NiceHttpHttpMethods
73
73
  http_redir.cookies.merge!(@cookies)
74
74
  http_redir.headers.merge!(headers_t)
75
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, ""))
76
+ resp = http_redir.get(path.gsub(server, ""))
77
77
  @response = http_redir.response
78
78
  else
79
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."
@@ -499,31 +499,28 @@ module NiceHttpHttpMethods
499
499
  ######################################################
500
500
  def send_request(request_hash)
501
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
-
502
+ request_hash[:method].is_a?(Symbol) and
503
+ [:get, :head, :post, :put, :delete, :patch].include?(request_hash[:method])
505
504
  message = "send_request: it needs to be supplied a Request Hash that includes a :method and :path. "
506
505
  message += "Supported methods: :get, :head, :post, :put, :delete, :patch"
507
506
  @logger.fatal message
508
507
  return {fatal_error: message, code: nil, message: nil}
509
508
  else
510
509
  case request_hash[:method]
511
- when :get
512
- resp = get request_hash
513
- when :post
514
- resp = post request_hash
515
- when :head
516
- resp = head request_hash
517
- when :put
518
- resp = put request_hash
519
- when :delete
520
- resp = delete request_hash
521
- when :patch
522
- resp = patch request_hash
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
523
522
  end
524
523
  return resp
525
524
  end
526
-
527
525
  end
528
-
529
- end
526
+ end
@@ -11,6 +11,8 @@ module NiceHttpManageRequest
11
11
  ######################################################
12
12
  def manage_request(*arguments)
13
13
  require "json"
14
+
15
+ @prev_request = Hash.new() if @prev_request.nil?
14
16
  begin
15
17
  content_type_included = false
16
18
  path = ""
@@ -26,7 +28,7 @@ module NiceHttpManageRequest
26
28
  elsif arguments.size == 1 and arguments[0].kind_of?(String)
27
29
  path = arguments[0].to_s()
28
30
  end
29
- path = (@prepath + path).gsub('//','/') unless path.nil? or path.start_with?('http:') or path.start_with?('https:')
31
+ path = (@prepath + path).gsub("//", "/") unless path.nil? or path.start_with?("http:") or path.start_with?("https:")
30
32
  @cookies.each { |cookie_path, cookies_hash|
31
33
  cookie_path = "" if cookie_path == "/"
32
34
  path_to_check = path
@@ -90,8 +92,8 @@ module NiceHttpManageRequest
90
92
  if arguments[0].include?(:values) and !arguments[0].include?(:values_for)
91
93
  arguments[0][:values_for] = arguments[0][:values]
92
94
  end
93
-
94
- if @values_for.size>0
95
+
96
+ if @values_for.size > 0
95
97
  if arguments[0][:values_for].nil?
96
98
  arguments[0][:values_for] = @values_for.dup
97
99
  else
@@ -112,11 +114,11 @@ module NiceHttpManageRequest
112
114
  if data.kind_of?(String)
113
115
  if arguments[0].include?(:values_for)
114
116
  arguments[0][:values_for].each { |key, value|
115
- data.gsub!(/"(#{key})":\s*"([^"]*)"/,'"\1": "'+value+'"') # "key":"value"
116
- data.gsub!(/(#{key}):\s*"([^"]*)"/,'\1: "'+value+'"') # key:"value"
117
- data.gsub!(/(#{key}):\s*'([^']*)'/,'\1: \''+value+"'") # key:'value'
118
- data.gsub!(/"(#{key})":\s*(\w+)/,'"\1": '+value) # "key":456
119
- data.gsub!(/(#{key}):\s*(\w+)/,'\1: '+value) # key:456
117
+ data.gsub!(/"(#{key})":\s*"([^"]*)"/, '"\1": "' + value + '"') # "key":"value"
118
+ data.gsub!(/(#{key}):\s*"([^"]*)"/, '\1: "' + value + '"') # key:"value"
119
+ data.gsub!(/(#{key}):\s*'([^']*)'/, '\1: \'' + value + "'") # key:'value'
120
+ data.gsub!(/"(#{key})":\s*(\w+)/, '"\1": ' + value) # "key":456
121
+ data.gsub!(/(#{key}):\s*(\w+)/, '\1: ' + value) # key:456
120
122
  }
121
123
  end
122
124
  elsif data.kind_of?(Hash)
@@ -191,16 +193,21 @@ module NiceHttpManageRequest
191
193
  headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }
192
194
  message = "#{"- " * 25}\n"
193
195
  if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
194
- message+= "#{method_s.upcase} Request: #{arguments[0][:name]}\n"
196
+ message += "#{method_s.upcase} Request: #{arguments[0][:name]}"
195
197
  else
196
- message+= "#{method_s.upcase} Request\n"
198
+ message += "#{method_s.upcase} Request"
199
+ end
200
+ message += "\n path: " + path.to_s() + "\n"
201
+ if @debug or @prev_request[:path] != path or @prev_request[:headers] != headers_t or @prev_request[:data] != data
202
+ message += " headers: {" + headers_ts.to_s() + "}\n"
203
+ message += " data: " + data_s.to_s() + "\n"
204
+ message = @message_server + "\n" + message
205
+ else
206
+ message += " Same as the last request."
197
207
  end
198
- message += " path: " + path.to_s() + "\n"
199
- message += " headers: {" + headers_ts.to_s() + "}\n"
200
- message += " data: " + data_s.to_s() + "\n"
201
- message = @message_server + "\n" + message
202
208
  if path.to_s().scan(/^https?:\/\//).size > 0 and path.to_s().scan(/^https?:\/\/#{@host}/).size == 0
203
209
  # the path is for another server than the current
210
+ # todo: identify if it is better to log the request, or if it is done later
204
211
  else
205
212
  self.class.last_request = message
206
213
  @logger.info(message)
@@ -209,6 +216,9 @@ module NiceHttpManageRequest
209
216
  if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
210
217
  data = data.to_s().encode(encoding, "UTF-8")
211
218
  end
219
+ @prev_request[:path] = path
220
+ @prev_request[:data] = data
221
+ @prev_request[:headers] = headers_t
212
222
  return path, data, headers_t
213
223
  rescue Exception => stack
214
224
  @logger.fatal(stack)
@@ -216,5 +226,4 @@ module NiceHttpManageRequest
216
226
  return :error
217
227
  end
218
228
  end
219
-
220
- end
229
+ end
@@ -10,6 +10,7 @@ module NiceHttpManageResponse
10
10
  ######################################################
11
11
  def manage_response(resp, data)
12
12
  require "json"
13
+ @prev_response = Hash.new() if @prev_response.nil?
13
14
  begin
14
15
  if @start_time.kind_of?(Time)
15
16
  @response[:time_elapsed_total] = Time.now - @start_time
@@ -40,8 +41,8 @@ module NiceHttpManageResponse
40
41
  if resp.kind_of?(Hash) and !resp.has_key?(:header)
41
42
  resp[:header] = {}
42
43
  end
43
-
44
- #todo: check this. not sure if this is valid anymore since resp it will be a hash only when mock_response
44
+
45
+ #todo: check this. not sure if this is valid anymore since resp it will be a hash only when mock_response
45
46
  if resp.kind_of?(Hash)
46
47
  resp.each { |k, v|
47
48
  if k != :code and k != :message and k != :data and k != :'set-cookie' and k != :header
@@ -76,8 +77,8 @@ module NiceHttpManageResponse
76
77
  resp.each { |key, val| @response[key.to_sym] = val.to_s().encode("UTF-8", encoding_response.to_s()) }
77
78
  else
78
79
  @response[:message] = resp.message
79
- resp.each { |key, val|
80
- @response[key.to_sym] = val
80
+ resp.each { |key, val|
81
+ @response[key.to_sym] = val
81
82
  }
82
83
  end
83
84
 
@@ -88,59 +89,59 @@ module NiceHttpManageResponse
88
89
  end
89
90
 
90
91
  @response[:code] = resp.code
91
-
92
- unless @response.nil?
93
- message = "\nRESPONSE: \n " + @response[:code].to_s() + ":" + @response[:message].to_s()
94
- #if @debug
95
- self.class.last_response = message if @debug
96
- @response.each { |key, value|
97
- if value.to_s() != ""
98
- value_orig = value
99
- if key.kind_of?(Symbol)
100
- if key == :code or key == :data or key == :header or key == :message
101
- if key == :data and !@response[:'content-type'].to_s.include?('text/html')
102
- begin
103
- JSON.parse(value_orig)
104
- data_s = JSON.pretty_generate(JSON.parse(value_orig))
105
- rescue
106
- data_s = value_orig
107
- end
108
- if @debug
109
- self.class.last_response += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
110
- end
111
- if value_orig != value
112
- message += "\n " + key.to_s() + ": '" + value.gsub("<", "&lt;") + "'\n"
113
- else
114
- message += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
115
- end
92
+ message = "\nRESPONSE: \n " + @response[:code].to_s() + ":" + @response[:message].to_s()
93
+ if @debug or @prev_response[:'content-type']!=@response[:'content-type'] or @prev_response[:'content-length']!=@response[:'content-length'] or
94
+ @prev_response[:data]!=@response[:data] or @prev_response[:code]!=@response[:code] or @prev_response[:message]!= @response[:message]
95
+ self.class.last_response = message if @debug
96
+ @response.each { |key, value|
97
+ if value.to_s() != ""
98
+ value_orig = value
99
+ if key.kind_of?(Symbol)
100
+ if key == :code or key == :data or key == :header or key == :message
101
+ if key == :data and !@response[:'content-type'].to_s.include?("text/html")
102
+ begin
103
+ JSON.parse(value_orig)
104
+ data_s = JSON.pretty_generate(JSON.parse(value_orig))
105
+ rescue
106
+ data_s = value_orig
107
+ end
108
+ if @debug
109
+ self.class.last_response += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
110
+ end
111
+ if value_orig != value
112
+ message += "\n " + key.to_s() + ": '" + value.gsub("<", "&lt;") + "'\n"
116
113
  else
117
- if @debug
118
- self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
119
- message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
120
- end
114
+ message += "\n " + key.to_s() + ": '" + data_s.gsub("<", "&lt;") + "'\n"
121
115
  end
122
116
  else
123
117
  if @debug
124
118
  self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
125
- end
126
- message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
119
+ message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
120
+ end
127
121
  end
128
- elsif !@response.include?(key.to_sym)
122
+ else
129
123
  if @debug
130
124
  self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
131
125
  end
132
126
  message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
133
127
  end
128
+ elsif !@response.include?(key.to_sym)
129
+ if @debug
130
+ self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
131
+ end
132
+ message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "&lt;") + "'"
134
133
  end
135
- }
136
- #end
137
- @logger.info message
138
- if @response.kind_of?(Hash)
139
- if @response.keys.include?(:requestid)
140
- @headers["requestId"] = @response[:requestid]
141
- self.class.request_id = @response[:requestid]
142
- @logger.info "requestId was found on the response header and it has been added to the headers for the next request"
143
134
  end
135
+ }
136
+ else
137
+ message += "\n Same as the last response."
138
+ end
139
+ @logger.info message
140
+ if @response.kind_of?(Hash)
141
+ if @response.keys.include?(:requestid)
142
+ @headers["requestId"] = @response[:requestid]
143
+ self.class.request_id = @response[:requestid]
144
+ @logger.info "requestId was found on the response header and it has been added to the headers for the next request"
144
145
  end
145
146
  end
146
147
 
@@ -173,9 +174,10 @@ module NiceHttpManageResponse
173
174
  end
174
175
  end
175
176
  end
177
+ @prev_response = @response
176
178
  rescue Exception => stack
177
179
  @logger.fatal stack
178
180
  @logger.fatal "manage_response Error on method #{method_s} "
179
181
  end
180
182
  end
181
- end
183
+ 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.6.4
4
+ version: 1.6.5
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-03-15 00:00:00.000000000 Z
11
+ date: 2019-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nice_hash
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '1.11'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.10.0
22
+ version: 1.11.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '1.10'
29
+ version: '1.11'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 1.10.0
32
+ version: 1.11.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rspec
35
35
  requirement: !ruby/object:Gem::Requirement