Module: NiceHttpHttpMethods
- Included in:
- NiceHttp
- Defined in:
- lib/nice_http/http_methods.rb
Instance Method Summary collapse
-
#delete(argument) ⇒ Hash
Delete an existing resource.
-
#get(arg, save_data: '') ⇒ Hash
Get data from path.
-
#head(argument) ⇒ Hash
Implementation of the http HEAD method.
-
#patch(*arguments) ⇒ Hash
Patch data to path.
-
#post(*arguments) ⇒ Hash
Post data to path.
-
#put(*arguments) ⇒ Hash
Put data to path.
-
#send_request(request_hash) ⇒ Hash
It will send the request depending on the :method declared on the request hash Take a look at https://github.com/MarioRuiz/Request-Hash.
Instance Method Details
#delete(argument) ⇒ Hash
Delete an existing resource
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/nice_http/http_methods.rb', line 462 def delete(argument) begin if argument.kind_of?(String) argument = { :path => argument } end path, data, headers_t = manage_request(argument) @start_time = Time.now if @start_time.nil? if argument.kind_of?(Hash) arg = argument if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) data = "" if arg[:mock_response].keys.include?(:data) data = arg[:mock_response][:data] if data.kind_of?(Hash) #to json begin require "json" data = data.to_json rescue @logger.fatal "There was a problem converting to json: #{data}" end end end @logger.warn "Pay attention!!! This is a mock response:" @start_time_net = Time.now if @start_time_net.nil? manage_response(arg[:mock_response], data.to_s) return @response end end begin @start_time_net = Time.now if @start_time_net.nil? if data.to_s == "" resp = @http.delete(path, headers_t) if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc) try = false @headers_orig.each do |k,v| if v.is_a?(Proc) and headers_t.key?(k) try = true headers_t[k] = v.call end end if try @logger.warn "Not authorized. Trying to generate a new token." resp = @http.delete(path, headers_t) end end else request = Net::HTTP::Delete.new(path, headers_t) request.body = data resp = @http.request(request) if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc) try = false @headers_orig.each do |k,v| if v.is_a?(Proc) and headers_t.key?(k) try = true headers_t[k] = v.call end end if try @logger.warn "Not authorized. Trying to generate a new token." request = Net::HTTP::Delete.new(path, headers_t) request.body = data resp = @http.request(request) end end end data = resp.body rescue Exception => stack @logger.warn stack @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect" @http.finish() @http.start() @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)} @start_time_net = Time.now if @start_time_net.nil? resp, data = @http.delete(path, headers_t) end manage_response(resp, data) return @response rescue Exception => stack @logger.fatal stack return { fatal_error: stack.to_s, code: nil, message: nil, data: "" } end end |
#get(arg, save_data: '') ⇒ Hash
Get data from path
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/nice_http/http_methods.rb', line 29 def get(arg, save_data: '') begin path, data, headers_t = manage_request(arg) @start_time = Time.now if @start_time.nil? if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) data = "" if arg[:mock_response].keys.include?(:data) data = arg[:mock_response][:data] if data.kind_of?(Hash) #to json begin require "json" data = data.to_json rescue @logger.fatal "There was a problem converting to json: #{data}" end end end @logger.warn "Pay attention!!! This is a mock response:" @start_time_net = Time.now if @start_time_net.nil? manage_response(arg[:mock_response], data.to_s) return @response end begin if path.start_with?("http:") or path.start_with?("https:") #server included on path problably because of a redirection to a different server require "uri" uri = URI.parse(path) ssl = false ssl = true if path.include?("https:") server = "http://" server = "https://" if path.start_with?("https:") if uri.port != 443 server += "#{uri.host}:#{uri.port}" else server += "#{uri.host}" end http_redir = nil self.class.connections.each { |conn| if conn.host == uri.host and conn.port == uri.port http_redir = conn break end } if !http_redir.nil? path, data, headers_t = manage_request(arg) http_redir..merge!(@cookies) http_redir.headers.merge!(headers_t) #todo: remove only the server at the begining in case in query is the server it will be replaced when it should not be resp = http_redir.get(path.gsub(server, "")) @response = http_redir.response else @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." end else @start_time_net = Time.now if @start_time_net.nil? resp = @http.get(path, headers_t) if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc) try = false @headers_orig.each do |k,v| if v.is_a?(Proc) and headers_t.key?(k) try = true headers_t[k] = v.call end end if try @logger.warn "Not authorized. Trying to generate a new token." resp = @http.get(path, headers_t) end end data = resp.body manage_response(resp, data) end rescue Exception => stack @logger.warn stack @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect" @http.finish() @http.start() @start_time_net = Time.now if @start_time_net.nil? @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)} resp = @http.get(path, headers_t) data = resp.body manage_response(resp, data) end if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location) if @num_redirects <= 30 @num_redirects += 1 current_server = "http" current_server += "s" if @ssl == true current_server += "://#{@host}" location = @response[:location].gsub(current_server, "") @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}" get(location) else @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" @num_redirects = 0 end else @num_redirects = 0 end if save_data!='' require 'pathname' pn_get = Pathname.new(path) if Dir.exist?(save_data) save = save_data + "/" + pn_get.basename.to_s elsif save_data[-1]=="/" save = save_data + pn_get.basename.to_s else save = save_data end if Dir.exist?(Pathname.new(save).dirname) File.open(save, 'wb') { |fp| fp.write(@response.data) } else @logger.fatal "The folder #{Pathname.new(save).dirname} doesn't exist" end end return @response rescue Exception => stack @logger.fatal stack return { fatal_error: stack.to_s, code: nil, message: nil, data: "" } end end |
#head(argument) ⇒ Hash
Implementation of the http HEAD method.
Asks for the response identical to the one that would correspond to a GET request, but without the response body.
This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/nice_http/http_methods.rb', line 561 def head(argument) begin if argument.kind_of?(String) argument = { :path => argument } end path, data, headers_t = manage_request(argument) @start_time = Time.now if @start_time.nil? if argument.kind_of?(Hash) arg = argument if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) @logger.warn "Pay attention!!! This is a mock response:" @start_time_net = Time.now if @start_time_net.nil? manage_response(arg[:mock_response], "") return @response end end begin @start_time_net = Time.now if @start_time_net.nil? resp = @http.head(path, headers_t) if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc) try = false @headers_orig.each do |k,v| if v.is_a?(Proc) and headers_t.key?(k) try = true headers_t[k] = v.call end end if try @logger.warn "Not authorized. Trying to generate a new token." resp = @http.head(path, headers_t) end end data = resp.body rescue Exception => stack @logger.warn stack @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect" @http.finish() @http.start() @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)} @start_time_net = Time.now if @start_time_net.nil? resp, data = @http.head(path, headers_t) end manage_response(resp, data) return @response rescue Exception => stack @logger.fatal stack return { fatal_error: stack.to_s, code: nil, message: nil } end end |
#patch(*arguments) ⇒ Hash
Patch data to path
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/nice_http/http_methods.rb', line 370 def patch(*arguments) begin path, data, headers_t = manage_request(*arguments) @start_time = Time.now if @start_time.nil? if arguments.size > 0 and arguments[0].kind_of?(Hash) arg = arguments[0] if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) data = "" if arg[:mock_response].keys.include?(:data) data = arg[:mock_response][:data] if data.kind_of?(Hash) #to json begin require "json" data = data.to_json rescue @logger.fatal "There was a problem converting to json: #{data}" end end end @logger.warn "Pay attention!!! This is a mock response:" @start_time_net = Time.now if @start_time_net.nil? manage_response(arg[:mock_response], data.to_s) return @response end end begin @start_time_net = Time.now if @start_time_net.nil? resp = @http.patch(path, data, headers_t) if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc) try = false @headers_orig.each do |k,v| if v.is_a?(Proc) and headers_t.key?(k) try = true headers_t[k] = v.call end end if try @logger.warn "Not authorized. Trying to generate a new token." resp = @http.patch(path, data, headers_t) end end data = resp.body rescue Exception => stack @logger.warn stack @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect" @http.finish() @http.start() @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)} @start_time_net = Time.now if @start_time_net.nil? resp, data = @http.patch(path, data, headers_t) end manage_response(resp, data) if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location) if @num_redirects <= 30 @num_redirects += 1 current_server = "http" current_server += "s" if @ssl == true current_server += "://#{@host}" location = @response[:location].gsub(current_server, "") @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}" get(location) else @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" @num_redirects = 0 end else @num_redirects = 0 end return @response rescue Exception => stack @logger.fatal stack return { fatal_error: stack.to_s, code: nil, message: nil, data: "" } end end |
#post(*arguments) ⇒ Hash
Post data to path
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/nice_http/http_methods.rb', line 181 def post(*arguments) begin path, data, headers_t = manage_request(*arguments) @start_time = Time.now if @start_time.nil? if arguments.size > 0 and arguments[0].kind_of?(Hash) arg = arguments[0] if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) data = "" if arg[:mock_response].keys.include?(:data) data = arg[:mock_response][:data] if data.kind_of?(Hash) #to json begin require "json" data = data.to_json rescue @logger.fatal "There was a problem converting to json: #{data}" end end end @logger.warn "Pay attention!!! This is a mock response:" @start_time_net = Time.now if @start_time_net.nil? manage_response(arg[:mock_response], data.to_s) return @response end end begin @start_time_net = Time.now if @start_time_net.nil? if headers_t["Content-Type"] == "multipart/form-data" require "net/http/post/multipart" headers_t.each { |key, value| arguments[0][:data].add_field(key, value) #add to Headers } resp = @http.request(arguments[0][:data]) elsif headers_t["Content-Type"].to_s.include?("application/x-www-form-urlencoded") encoded_form = URI.encode_www_form(arguments[0][:data]) resp = @http.request_post(path, encoded_form, headers_t) data = resp.body else resp = @http.post(path, data, headers_t) #todo: do it also for forms and multipart if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc) try = false @headers_orig.each do |k,v| if v.is_a?(Proc) and headers_t.key?(k) try = true headers_t[k] = v.call end end if try @logger.warn "Not authorized. Trying to generate a new token." resp = @http.post(path, data, headers_t) end end data = resp.body end rescue Exception => stack @logger.warn stack @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect" @http.finish() @http.start() @start_time_net = Time.now if @start_time_net.nil? @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)} resp, data = @http.post(path, data, headers_t) end manage_response(resp, data) if @auto_redirect and @response[:code].to_i >= 300 and @response[:code].to_i < 400 and @response.include?(:location) if @num_redirects <= 30 @num_redirects += 1 current_server = "http" current_server += "s" if @ssl == true current_server += "://#{@host}" location = @response[:location].gsub(current_server, "") @logger.info "(#{@num_redirects}) Redirecting NiceHttp to #{location}" get(location) else @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" @num_redirects = 0 end else @num_redirects = 0 end return @response rescue Exception => stack @logger.fatal stack return { fatal_error: stack.to_s, code: nil, message: nil, data: "" } end end |
#put(*arguments) ⇒ Hash
Put data to path
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/nice_http/http_methods.rb', line 289 def put(*arguments) begin path, data, headers_t = manage_request(*arguments) @start_time = Time.now if @start_time.nil? if arguments.size > 0 and arguments[0].kind_of?(Hash) arg = arguments[0] if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) data = "" if arg[:mock_response].keys.include?(:data) data = arg[:mock_response][:data] if data.kind_of?(Hash) #to json begin require "json" data = data.to_json rescue @logger.fatal "There was a problem converting to json: #{data}" end end end @logger.warn "Pay attention!!! This is a mock response:" @start_time_net = Time.now if @start_time_net.nil? manage_response(arg[:mock_response], data.to_s) return @response end end begin @start_time_net = Time.now if @start_time_net.nil? resp = @http.send_request("PUT", path, data, headers_t) if resp.code == 401 and @headers_orig.values.map(&:class).include?(Proc) try = false @headers_orig.each do |k,v| if v.is_a?(Proc) and headers_t.key?(k) try = true headers_t[k] = v.call end end if try @logger.warn "Not authorized. Trying to generate a new token." resp = @http.send_request("PUT", path, data, headers_t) end end data = resp.body rescue Exception => stack @logger.warn stack @logger.warn "The connection seems to be closed in the host machine. Trying to reconnect" @http.finish() @http.start() @headers_orig.each {|k,v| headers_t[k] = v.call if v.is_a?(Proc) and headers_t.key?(k)} @start_time_net = Time.now if @start_time_net.nil? resp, data = @http.send_request("PUT", path, data, headers_t) end manage_response(resp, data) return @response rescue Exception => stack @logger.fatal stack return { fatal_error: stack.to_s, code: nil, message: nil, data: "" } end end |
#send_request(request_hash) ⇒ Hash
It will send the request depending on the :method declared on the request hash
Take a look at https://github.com/MarioRuiz/Request-Hash
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/nice_http/http_methods.rb', line 630 def send_request(request_hash) unless request_hash.is_a?(Hash) and request_hash.key?(:method) and request_hash.key?(:path) and request_hash[:method].is_a?(Symbol) and [:get, :head, :post, :put, :delete, :patch].include?(request_hash[:method]) = "send_request: it needs to be supplied a Request Hash that includes a :method and :path. " += "Supported methods: :get, :head, :post, :put, :delete, :patch" @logger.fatal return { fatal_error: , code: nil, message: nil } else case request_hash[:method] when :get resp = get request_hash when :post resp = post request_hash when :head resp = head request_hash when :put resp = put request_hash when :delete resp = delete request_hash when :patch resp = patch request_hash end return resp end end |