Class: NiceHttp
- Inherits:
-
Object
- Object
- NiceHttp
- Defined in:
- lib/nice_http.rb
Class Attribute Summary collapse
-
.active ⇒ Object
Returns the value of attribute active.
-
.auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
-
.connections ⇒ Object
Returns the value of attribute connections.
-
.debug ⇒ Object
Returns the value of attribute debug.
-
.headers ⇒ Object
Returns the value of attribute headers.
-
.host ⇒ Object
Returns the value of attribute host.
-
.last_request ⇒ Object
Returns the value of attribute last_request.
-
.last_response ⇒ Object
Returns the value of attribute last_response.
-
.log ⇒ Object
Returns the value of attribute log.
-
.port ⇒ Object
Returns the value of attribute port.
-
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
-
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
.request_id ⇒ Object
Returns the value of attribute request_id.
-
.ssl ⇒ Object
Returns the value of attribute ssl.
-
.use_mocks ⇒ Object
Returns the value of attribute use_mocks.
Instance Attribute Summary collapse
-
#auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#num_redirects ⇒ Object
readonly
Returns the value of attribute num_redirects.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#proxy_host ⇒ Object
readonly
Returns the value of attribute proxy_host.
-
#proxy_port ⇒ Object
readonly
Returns the value of attribute proxy_port.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#use_mocks ⇒ Object
Returns the value of attribute use_mocks.
Class Method Summary collapse
Instance Method Summary collapse
-
#close ⇒ Object
Close HTTP connection.
-
#delete(argument) ⇒ Object
Delete an existing resource input: 1 argument Hash containing at least key :path 1 argument String giving the path output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.remove_session) assert resp.code==204.
-
#get(arg) ⇒ Object
Get data from path input: 1 argument Hash containing at least key :path 1 argument path (string) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.get_profile) assert resp.code==200.
-
#head(argument) ⇒ Object
Implementation of the http HEAD method.
-
#initialize(args = {}) ⇒ NiceHttp
constructor
input: no parameters: By default will access how is setup on defaults one parameter: String "https://www.example.com" "example.com:8999" "localhost:8322" Hash containing these possible keys host -- example.com.
-
#patch(*arguments) ⇒ Object
Patch data to path input: 1 argument Hash containing at least keys :data, :path 3 arguments path (string) data (json data for example) additional_headers (Hash key=>value) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.unrelease_account) assert resp.code==200.
-
#post(*arguments) ⇒ Object
Post data to path input: 1 argument Hash containing at least keys :data, :path 3 arguments path (string) data (json data for example) additional_headers (Hash key=>value) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.update_customer) assert resp.code==201.
-
#put(*arguments) ⇒ Object
Put data to path input: 1 argument Hash containing at least keys :data, :path 3 arguments path (string) data (json data for example) additional_headers (Hash key=>value) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.remove_phone) assert resp.code==200.
Constructor Details
#initialize(args = {}) ⇒ NiceHttp
input: no parameters: By default will access how is setup on defaults one parameter: String "https://www.example.com" "example.com:8999" "localhost:8322" Hash containing these possible keys host -- example.com. (default blank screen) port -- port for the connection. 80 (default) ssl -- true, false (default) headers -- hash with the header key:values debug -- true, false (default) log -- :no, :screen, :file, :fix_file (default). A string with a path can be supplied. If :fix_file: nice_http.log In case :file it will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log proxy_host proxy_port
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 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/nice_http.rb', line 65 def initialize(args = {}) require 'net/http' require 'net/https' @host = NiceHttp.host @port = NiceHttp.port @ssl = NiceHttp.ssl @headers = NiceHttp.headers @debug = NiceHttp.debug @log = NiceHttp.log @proxy_host = NiceHttp.proxy_host @proxy_port = NiceHttp.proxy_port @use_mocks = NiceHttp.use_mocks @auto_redirect=false #set it up at the end of initialize auto_redirect = NiceHttp.auto_redirect @num_redirects=0 #todo: set only the cookies for the current domain #key: path, value: hash with key is the name of the cookie and value the value # we set the default value for non existing keys to empty Hash {} so in case of merge there is no problem @cookies=Hash.new {|h, k| h[k] = {}} begin if args.is_a?(String) uri = URI.parse(args) @host = uri.host unless uri.host.nil? @port = uri.port unless uri.port.nil? @ssl = true if !uri.scheme.nil? && (uri.scheme == 'https') elsif args.is_a?(Hash) && !args.keys.empty? @host = args[:host] if args.keys.include?(:host) @port = args[:port] if args.keys.include?(:port) @ssl = args[:ssl] if args.keys.include?(:ssl) @headers = args[:headers] if args.keys.include?(:headers) @debug = args[:debug] if args.keys.include?(:debug) @log = args[:log] if args.keys.include?(:log) @proxy_host = args[:proxy_host] if args.keys.include?(:proxy_host) @proxy_port = args[:proxy_port] if args.keys.include?(:proxy_port) @use_mocks = args[:use_mocks] if args.keys.include?(:use_mocks) @auto_redirect = args[:auto_redirect] if args.keys.include?(:auto_redirect) end if @host.include?("http:") or @host.include?("https:") uri = URI.parse(@host) @host = uri.host unless uri.host.nil? @port = uri.port unless uri.port.nil? @ssl = true if !uri.scheme.nil? && (uri.scheme == 'https') end if !@proxy_host.nil? && !@proxy_port.nil? @http = Net::HTTP::Proxy(@proxy_host, @proxy_port).new(@host, @port) @http.use_ssl = @ssl @http.set_debug_output $stderr if @debug @http.verify_mode = OpenSSL::SSL::VERIFY_NONE @http.start else @http = Net::HTTP.new(@host, @port) @http.use_ssl = @ssl @http.set_debug_output $stderr if @debug @http.verify_mode = OpenSSL::SSL::VERIFY_NONE @http.start end if @log.kind_of?(String) @logger = Logger.new File.new(@log, "w") elsif @log==:fix_file @logger = Logger.new File.new("nice_http.log", "w") elsif @log==:file @logger = Logger.new File.new("nice_http_#{Time.now.strftime('%Y-%m-%d-%H%M%S')}.log", 'w') elsif @log==:screen @logger = Logger.new STDOUT elsif @log==:no @logger = Logger.new nil end @logger.level = Logger::INFO @message_server="(#{self.object_id}):" ="(#{self.object_id}): Http connection created. host:#{@host}, port:#{@port}, ssl:#{@ssl}, mode:#{@mode}, proxy_host: #{@proxy_host.to_s()}, proxy_port: #{@proxy_port.to_s()} " @logger.info() @message_server+=" Http connection: " if @ssl then @message_server+="https://" else @message_server+="http://" end @message_server+="#{@host}:#{@port}" if @proxy_host.to_s!="" then @message_server+=" proxy:#{@proxy_host}:#{@proxy_port}" end @auto_redirect = auto_redirect rescue Exception => stack if @logger.nil? puts stack else @logger.fatal stack end end NiceHttp.active+=1 NiceHttp.connections.push(self) end |
Class Attribute Details
.active ⇒ Object
Returns the value of attribute active.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def active @active end |
.auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def auto_redirect @auto_redirect end |
.connections ⇒ Object
Returns the value of attribute connections.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def connections @connections end |
.debug ⇒ Object
Returns the value of attribute debug.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def debug @debug end |
.headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def headers @headers end |
.host ⇒ Object
Returns the value of attribute host.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def host @host end |
.last_request ⇒ Object
Returns the value of attribute last_request.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def last_request @last_request end |
.last_response ⇒ Object
Returns the value of attribute last_response.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def last_response @last_response end |
.log ⇒ Object
Returns the value of attribute log.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def log @log end |
.port ⇒ Object
Returns the value of attribute port.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def port @port end |
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def proxy_host @proxy_host end |
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def proxy_port @proxy_port end |
.request_id ⇒ Object
Returns the value of attribute request_id.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def request_id @request_id end |
.ssl ⇒ Object
Returns the value of attribute ssl.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def ssl @ssl end |
.use_mocks ⇒ Object
Returns the value of attribute use_mocks.
7 8 9 |
# File 'lib/nice_http.rb', line 7 def use_mocks @use_mocks end |
Instance Attribute Details
#auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
28 29 30 |
# File 'lib/nice_http.rb', line 28 def auto_redirect @auto_redirect end |
#cookies ⇒ Object
Returns the value of attribute cookies.
28 29 30 |
# File 'lib/nice_http.rb', line 28 def @cookies end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def debug @debug end |
#headers ⇒ Object
Returns the value of attribute headers.
28 29 30 |
# File 'lib/nice_http.rb', line 28 def headers @headers end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def host @host end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def log @log end |
#logger ⇒ Object
Returns the value of attribute logger.
28 29 30 |
# File 'lib/nice_http.rb', line 28 def logger @logger end |
#num_redirects ⇒ Object (readonly)
Returns the value of attribute num_redirects.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def num_redirects @num_redirects end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def port @port end |
#proxy_host ⇒ Object (readonly)
Returns the value of attribute proxy_host.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def proxy_host @proxy_host end |
#proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def proxy_port @proxy_port end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def response @response end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
27 28 29 |
# File 'lib/nice_http.rb', line 27 def ssl @ssl end |
#use_mocks ⇒ Object
Returns the value of attribute use_mocks.
28 29 30 |
# File 'lib/nice_http.rb', line 28 def use_mocks @use_mocks end |
Class Method Details
.defaults=(par = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/nice_http.rb', line 30 def self.defaults=(par = {}) @host = par[:host] if par.key?(:host) @port = par[:port] if par.key?(:port) @ssl = par[:ssl] if par.key?(:ssl) @headers = par[:headers] if par.key?(:headers) @debug = par[:debug] if par.key?(:debug) @log = par[:log] if par.key?(:log) @proxy_host = par[:proxy_host] if par.key?(:proxy_host) @proxy_port = par[:proxy_port] if par.key?(:proxy_port) @proxy_port = par[:use_mocks] if par.key?(:use_mocks) @auto_redirect = par[:auto_redirect] if par.key?(:auto_redirect) end |
Instance Method Details
#close ⇒ Object
Close HTTP connection
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'lib/nice_http.rb', line 658 def close begin pos=0 found=false NiceHttp.connections.each {|conn| if conn.object_id == self.object_id then found=true break end pos+=1 } if found NiceHttp.connections.delete_at(pos) end unless @closed if !@http.nil? then @http.finish() @http=nil @logger.info "the HTTP connection was closed: #{@message_server}" else @http=nil @logger.fatal "It was not possible to close the HTTP connection: #{@message_server}" end @closed=true else @logger.warn "It was not possible to close the HTTP connection, already closed: #{@message_server}" end rescue Exception => stack @logger.fatal stack end NiceHttp.active-=1 end |
#delete(argument) ⇒ Object
Delete an existing resource input: 1 argument Hash containing at least key :path 1 argument String giving the path output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.remove_session) assert resp.code==204
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 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 |
# File 'lib/nice_http.rb', line 539 def delete(argument) begin if argument.kind_of?(String) then argument={:path => argument} end path, data, headers_t=manage_request(argument) @start_time = Time.now if @start_time.nil? if argument.kind_of?(Hash) then arg=argument if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) then data="" if arg[:mock_response].keys.include?(:data) then 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.delete(path, headers_t) 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() @start_time_net = Time.now if @start_time_net.nil? resp, data=@http.delete(path) end manage_response(resp, data) return @response rescue Exception => stack @logger.fatal stack return :error end end |
#get(arg) ⇒ Object
Get data from path input: 1 argument Hash containing at least key :path 1 argument path (string) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.get_profile) assert resp.code==200
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 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/nice_http.rb', line 185 def get(arg) 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) then data="" if arg[:mock_response].keys.include?(:data) then 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.include?("http:") or path.include?("https:") then #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.include?("https:") if uri.port!=443 then server+="#{uri.host}:#{uri.port}" else server+="#{uri.host}" end http_redir=nil NiceHttp.connections.each {|conn| if conn.host == uri.host and conn.port==uri.port then 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) resp=http_redir.get(path.gsub(server, "")) #todo: remove only the server at the begining in case in query is the server it will be replaced when it should not be @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) 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? resp=@http.get(path) 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) then if @num_redirects<=30 then @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 :error end end |
#head(argument) ⇒ Object
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. input: 1 argument Hash containing at least key :path 1 argument String giving the path output: response -> Hash including the symbol keys: :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/nice_http.rb', line 605 def head(argument) begin if argument.kind_of?(String) then argument={:path => argument} end path, data, headers_t=manage_request(argument) @start_time = Time.now if @start_time.nil? if argument.kind_of?(Hash) then arg=argument if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) then data="" if arg[:mock_response].keys.include?(:data) then 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.head(path, headers_t) 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() @start_time_net = Time.now if @start_time_net.nil? resp, data=@http.head(path) end manage_response(resp, data) return @response rescue Exception => stack @logger.fatal stack return :error end end |
#patch(*arguments) ⇒ Object
Patch data to path input: 1 argument Hash containing at least keys :data, :path 3 arguments path (string) data (json data for example) additional_headers (Hash key=>value) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.unrelease_account) assert resp.code==200
458 459 460 461 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 |
# File 'lib/nice_http.rb', line 458 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) then arg=arguments[0] if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) then data="" if arg[:mock_response].keys.include?(:data) then 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) 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() @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) then if @num_redirects<=30 then @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 :error end end |
#post(*arguments) ⇒ Object
Post data to path input: 1 argument Hash containing at least keys :data, :path 3 arguments path (string) data (json data for example) additional_headers (Hash key=>value) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.update_customer) assert resp.code==201
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/nice_http.rb', line 299 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) then arg=arguments[0] if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) then data="" if arg[:mock_response].keys.include?(:data) then 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" then 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]) else resp=@http.post(path, data, headers_t) 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? 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) then if @num_redirects<=30 then @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.warn stack return :error end end |
#put(*arguments) ⇒ Object
Put data to path input: 1 argument Hash containing at least keys :data, :path 3 arguments path (string) data (json data for example) additional_headers (Hash key=>value) output: response -> Hash including at least the symbol keys: :data = the response data body :message = plain text response :code = code response (200=ok,500=wrong...) *All keys in response are lowercase data, message and code can also be accessed as attributes like .message .code .data, for example: [email protected](Requests::Customer.remove_phone) assert resp.code==200
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 |
# File 'lib/nice_http.rb', line 390 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) then arg=arguments[0] if @use_mocks and arg.kind_of?(Hash) and arg.keys.include?(:mock_response) then data="" if arg[:mock_response].keys.include?(:data) then 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) 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() @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, self return :error end end |