Class: NiceHttp

Inherits:
Object
  • Object
show all
Includes:
NiceHttpHttpMethods, NiceHttpManageRequest, NiceHttpManageResponse
Defined in:
lib/nice_http.rb

Overview

Attributes you can access using NiceHttp.the_attribute: :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port, :last_request, :last_response, :request_id, :use_mocks, :connections, :active, :auto_redirect, :values_for, :create_stats, :stats

Constant Summary collapse

Error =
Class.new StandardError
InfoMissing =
Class.new Error do
  attr_reader :attribute

  def initialize(attribute, message = "")
    @attribute = attribute
    message += "It was not possible to create the http connection!!!\n"
    message += "Wrong #{attribute}. "
    message += "Remember to supply http:// or https:// in case you specify an url to create the http connection, for example:\n"
    message += "http = NiceHttp.new('http://example.com')"
    super message
  end
end

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ NiceHttp

Creates a new http connection.

Examples:

http = NiceHttp.new()
http = NiceHttp.new("https://www.example.com")
http = NiceHttp.new("example.com:8999")
http = NiceHttp.new("localhost:8322")
http2 = NiceHttp.new( host: "reqres.in", port: 443, ssl: true )
my_server = {host: "example.com",
             port: 80,
             headers: {"api-key": "zdDDdjkck"}
            }
http3 = NiceHttp.new my_server

Parameters:

  • args (defaults to: {})

    [] If no parameter supplied, by default will access how is setup on defaults

  • args (String) (defaults to: {})

    . The url to create the connection.

  • args (Hash) (defaults to: {})

    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 headers

    values_for -- hash with the values_for

    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

Raises:



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
369
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/nice_http.rb', line 318

def initialize(args = {})
  require "net/http"
  require "net/https"
  @host = self.class.host
  @port = self.class.port
  @prepath = ""
  @ssl = self.class.ssl
  @headers = self.class.headers.dup
  @values_for = self.class.values_for.dup
  @debug = self.class.debug
  @log = self.class.log
  @proxy_host = self.class.proxy_host
  @proxy_port = self.class.proxy_port
  @use_mocks = self.class.use_mocks
  @auto_redirect = false #set it up at the end of initialize
  auto_redirect = self.class.auto_redirect
  @num_redirects = 0
  @create_stats = self.class.create_stats

  #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] = {} }

  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")
    @prepath = uri.path unless uri.path == "/"
  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].dup if args.keys.include?(:headers)
    @values_for = args[:values_for].dup if args.keys.include?(:values_for)
    @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

  log_filename = ""
  if @log.kind_of?(String) or @log == :fix_file or @log == :file or @log == :file_run
    if @log.kind_of?(String)
      log_filename = @log.dup

      unless log_filename.start_with?(".")
        if caller.first.start_with?(Dir.pwd)
          folder = File.dirname(caller.first[/[^:]+/])
        else
          folder = File.dirname("#{Dir.pwd}/#{caller.first[/[^:]+/]}")
        end
        folder += "/" unless log_filename.start_with?("/")
        log_filename = folder + log_filename
      end
      require "fileutils"
      FileUtils.mkdir_p File.dirname(log_filename)
      unless Dir.exist?(File.dirname(log_filename))
        @logger = Logger.new nil
        raise InfoMissing, :log, "Wrong directory specified for logs.\n"
      end
    elsif @log == :fix_file
      log_filename = "nice_http.log"
    elsif @log == :file
      log_filename = "nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log"
    elsif @log == :file_run
      log_filename = "#{caller.first[/[^:]+/]}.log"
    end
    if Thread.current.name.to_s != ""
      log_filename.gsub!(/\.log$/, "_#{Thread.current.name}.log")
    end
    if self.class.log_files.key?(log_filename)
      @logger = self.class.log_files[log_filename]
    else
      begin
        f = File.new(log_filename, "w")
        f.sync = true
        @logger = Logger.new f
      rescue Exception => stack
        @logger = Logger.new nil
        raise InfoMissing, :log
      end
      self.class.log_files[log_filename] = @logger
    end
  elsif @log == :screen
    @logger = Logger.new STDOUT
  elsif @log == :no
    @logger = Logger.new nil
  else
    raise InfoMissing, :log
  end
  @logger.level = Logger::INFO

  if @host.to_s != "" and (@host.start_with?("http:") or @host.start_with?("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")
    @prepath = uri.path unless uri.path == "/"
  end

  raise InfoMissing, :port if @port.to_s == ""
  raise InfoMissing, :host if @host.to_s == ""
  raise InfoMissing, :ssl unless @ssl.is_a?(TrueClass) or @ssl.is_a?(FalseClass)
  raise InfoMissing, :debug unless @debug.is_a?(TrueClass) or @debug.is_a?(FalseClass)
  raise InfoMissing, :auto_redirect unless auto_redirect.is_a?(TrueClass) or auto_redirect.is_a?(FalseClass)
  raise InfoMissing, :use_mocks unless @use_mocks.is_a?(TrueClass) or @use_mocks.is_a?(FalseClass)
  raise InfoMissing, :headers unless @headers.is_a?(Hash)
  raise InfoMissing, :values_for unless @values_for.is_a?(Hash)

  begin
    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

    @message_server = "(#{self.object_id}):"

    log_message = "(#{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(log_message)
    @message_server += " Http connection: "
    if @ssl
      @message_server += "https://"
    else
      @message_server += "http://"
    end
    @message_server += "#{@host}:#{@port}"
    if @proxy_host.to_s != ""
      @message_server += " proxy:#{@proxy_host}:#{@proxy_port}"
    end
    @auto_redirect = auto_redirect
    # for the case we have headers following nice_hash implementation
    @headers = @headers.generate

    self.class.active += 1
    self.class.connections.push(self)
  rescue Exception => stack
    puts stack
    @logger.fatal stack
  end
end

Class Attribute Details

.activeObject

Returns the value of attribute active.



68
69
70
# File 'lib/nice_http.rb', line 68

def active
  @active
end

.auto_redirectObject

Returns the value of attribute auto_redirect.



68
69
70
# File 'lib/nice_http.rb', line 68

def auto_redirect
  @auto_redirect
end

.connectionsObject

Returns the value of attribute connections.



68
69
70
# File 'lib/nice_http.rb', line 68

def connections
  @connections
end

.create_statsObject

Returns the value of attribute create_stats.



68
69
70
# File 'lib/nice_http.rb', line 68

def create_stats
  @create_stats
end

.debugObject

Returns the value of attribute debug.



68
69
70
# File 'lib/nice_http.rb', line 68

def debug
  @debug
end

.headersObject

Returns the value of attribute headers.



68
69
70
# File 'lib/nice_http.rb', line 68

def headers
  @headers
end

.hostObject

Returns the value of attribute host.



68
69
70
# File 'lib/nice_http.rb', line 68

def host
  @host
end

.last_requestObject

Returns the value of attribute last_request.



68
69
70
# File 'lib/nice_http.rb', line 68

def last_request
  @last_request
end

.last_responseObject

Returns the value of attribute last_response.



68
69
70
# File 'lib/nice_http.rb', line 68

def last_response
  @last_response
end

.logObject

Returns the value of attribute log.



68
69
70
# File 'lib/nice_http.rb', line 68

def log
  @log
end

.log_filesObject

Returns the value of attribute log_files.



68
69
70
# File 'lib/nice_http.rb', line 68

def log_files
  @log_files
end

.portObject

Returns the value of attribute port.



68
69
70
# File 'lib/nice_http.rb', line 68

def port
  @port
end

.proxy_hostObject

Returns the value of attribute proxy_host.



68
69
70
# File 'lib/nice_http.rb', line 68

def proxy_host
  @proxy_host
end

.proxy_portObject

Returns the value of attribute proxy_port.



68
69
70
# File 'lib/nice_http.rb', line 68

def proxy_port
  @proxy_port
end

.request_idObject

Returns the value of attribute request_id.



68
69
70
# File 'lib/nice_http.rb', line 68

def request_id
  @request_id
end

.sslObject

Returns the value of attribute ssl.



68
69
70
# File 'lib/nice_http.rb', line 68

def ssl
  @ssl
end

.statsObject

Returns the value of attribute stats.



68
69
70
# File 'lib/nice_http.rb', line 68

def stats
  @stats
end

.use_mocksObject

Returns the value of attribute use_mocks.



68
69
70
# File 'lib/nice_http.rb', line 68

def use_mocks
  @use_mocks
end

.values_forObject

Returns the value of attribute values_for.



68
69
70
# File 'lib/nice_http.rb', line 68

def values_for
  @values_for
end

Instance Attribute Details

#activeInteger

Number of active connections

Returns:

  • (Integer)

    the current value of active



47
48
49
# File 'lib/nice_http.rb', line 47

def active
  @active
end

#auto_redirectBoolean

If true, NiceHttp will take care of the auto redirections when required by the responses

Returns:

  • (Boolean)

    the current value of auto_redirect



47
48
49
# File 'lib/nice_http.rb', line 47

def auto_redirect
  @auto_redirect
end

#connectionsArray

It will include all the active connections (NiceHttp instances)

Returns:

  • (Array)

    the current value of connections



47
48
49
# File 'lib/nice_http.rb', line 47

def connections
  @connections
end

#cookiesHash

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: { '/' => { "cfid" => "d95adfas2550255", "amddom.settings" => "doom" } }

Returns:

  • (Hash)

    the current value of cookies



47
48
49
# File 'lib/nice_http.rb', line 47

def cookies
  @cookies
end

#create_statsBoolean

If true, NiceHttp will create stats of the http communication and store them on NiceHttp.stats hash

Returns:

  • (Boolean)

    the current value of create_stats



47
48
49
# File 'lib/nice_http.rb', line 47

def create_stats
  @create_stats
end

#debugBoolean

In case true shows all the details of the communication with the host

Returns:

  • (Boolean)

    the current value of debug



47
48
49
# File 'lib/nice_http.rb', line 47

def debug
  @debug
end

#headersHash

Contains the headers you will be using on your connection

Returns:

  • (Hash)

    the current value of headers



47
48
49
# File 'lib/nice_http.rb', line 47

def headers
  @headers
end

#hostString

The host to be accessed

Returns:

  • (String)

    the current value of host



47
48
49
# File 'lib/nice_http.rb', line 47

def host
  @host
end

#last_requestString

The last request with all the content sent

Returns:

  • (String)

    the current value of last_request



47
48
49
# File 'lib/nice_http.rb', line 47

def last_request
  @last_request
end

#last_responseString

Only in case :debug is true, the last response with all the content

Returns:

  • (String)

    the current value of last_response



47
48
49
# File 'lib/nice_http.rb', line 47

def last_response
  @last_response
end

#logString, Symbol

:fix_file, :no, :screen, :file, "path and file name". :fix_file, will log the communication on nice_http.log. (default). :no, will not generate any logs. :screen, will print the logs on the screen. :file, will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log. :file_run, will generate a log file with the name where the object was created and extension .log, fex: myfile.rb.log String the path and file name where the logs will be stored.

Returns:

  • (String, Symbol)

    the current value of log



47
48
49
# File 'lib/nice_http.rb', line 47

def log
  @log
end

#loggerLogger

An instance of the Logger class where logs will be stored. You can access on anytime to store specific data, for example: my_http.logger.info "add this to the log file"

Returns:

  • (Logger)

    the current value of logger

See Also:



47
48
49
# File 'lib/nice_http.rb', line 47

def logger
  @logger
end

#num_redirectsInteger

Number of consecutive redirections managed

Returns:

  • (Integer)

    the current value of num_redirects



47
48
49
# File 'lib/nice_http.rb', line 47

def num_redirects
  @num_redirects
end

#portInteger

The port number

Returns:

  • (Integer)

    the current value of port



47
48
49
# File 'lib/nice_http.rb', line 47

def port
  @port
end

#proxy_hostString

the proxy host to be used

Returns:

  • (String)

    the current value of proxy_host



47
48
49
# File 'lib/nice_http.rb', line 47

def proxy_host
  @proxy_host
end

#proxy_portInteger

the proxy port to be used

Returns:

  • (Integer)

    the current value of proxy_port



47
48
49
# File 'lib/nice_http.rb', line 47

def proxy_port
  @proxy_port
end

#request_idString

If the response includes a requestId, will be stored here

Returns:

  • (String)

    the current value of request_id



47
48
49
# File 'lib/nice_http.rb', line 47

def request_id
  @request_id
end

#responseHash

Contains the full response hash

Returns:

  • (Hash)

    the current value of response



47
48
49
# File 'lib/nice_http.rb', line 47

def response
  @response
end

#sslBoolean

If you use ssl or not

Returns:

  • (Boolean)

    the current value of ssl



47
48
49
# File 'lib/nice_http.rb', line 47

def ssl
  @ssl
end

#statsHash

It contains detailed stats of the http communication

Returns:

  • (Hash)

    the current value of stats



47
48
49
# File 'lib/nice_http.rb', line 47

def stats
  @stats
end

#use_mocksBoolean

If true, in case the request hash includes a :mock_response key, it will be used as the response instead

Returns:

  • (Boolean)

    the current value of use_mocks



47
48
49
# File 'lib/nice_http.rb', line 47

def use_mocks
  @use_mocks
end

#values_forHash

The default values to set on the data in case not specified others

Returns:

  • (Hash)

    the current value of values_for



47
48
49
# File 'lib/nice_http.rb', line 47

def values_for
  @values_for
end

Class Method Details

.add_stats(name, state, started, finished, item = nil) ⇒ Object

To add specific stats The stats will be added to NiceHttp.stats[:specific]

Examples:

started = Time.now
@http.send_request Requests::Customer.add_customer
30.times do
   resp = @http.get(Requests::Customer.get_customer)
   break if resp.code == 200
   sleep 0.5
end
NiceHttp.add_stats(:customer, :create, started, Time.now)

Parameters:

  • name (Symbol)

    name to group your specific stats

  • state (Symbol)

    state of the name supplied to group your specific stats

  • started (Time)

    when the process you want the stats started

  • finished (Time)

    when the process you want the stats finished

  • item (Object) (defaults to: nil)

    (Optional) The item to be added to :items key to store all items in an array



171
172
173
174
175
176
177
178
179
180
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
# File 'lib/nice_http.rb', line 171

def self.add_stats(name, state, started, finished, item = nil)
  self.stats[:specific] ||= {}
  self.stats[:specific][name] ||= { num: 0, started: started, finished: started, real_time_elapsed: 0, time_elapsed: { total: 0, maximum: 0, minimum: 100000, average: 0 } }
  self.stats[:specific][name][:num] += 1

  if started < self.stats[:specific][name][:finished]
    self.stats[:specific][name][:real_time_elapsed] += (finished - self.stats[:specific][name][:finished])
  else
    self.stats[:specific][name][:real_time_elapsed] += (finished - started)
  end
  self.stats[:specific][name][:finished] = finished

  time_elapsed = self.stats[:specific][name][:time_elapsed]
  time_elapsed[:total] += finished - started
  if time_elapsed[:maximum] < (finished - started)
    time_elapsed[:maximum] = (finished - started)
    if !item.nil?
      time_elapsed[:item_maximum] = item
    elsif Thread.current.name.to_s != ""
      time_elapsed[:item_maximum] = Thread.current.name
    end
  end
  if time_elapsed[:minimum] > (finished - started)
    time_elapsed[:minimum] = (finished - started)
    if !item.nil?
      time_elapsed[:item_minimum] = item
    elsif Thread.current.name.to_s != ""
      time_elapsed[:item_minimum] = Thread.current.name
    end
  end
  time_elapsed[:average] = time_elapsed[:total] / self.stats[:specific][name][:num]

  self.stats[:specific][name][state] ||= { num: 0, started: started, finished: started, real_time_elapsed: 0, time_elapsed: { total: 0, maximum: 0, minimum: 1000, average: 0 }, items: [] }
  self.stats[:specific][name][state][:num] += 1
  if started < self.stats[:specific][name][state][:finished]
    self.stats[:specific][name][state][:real_time_elapsed] += (finished - self.stats[:specific][name][state][:finished])
  else
    self.stats[:specific][name][state][:real_time_elapsed] += (finished - started)
  end

  self.stats[:specific][name][state][:finished] = finished

  self.stats[:specific][name][state][:items] << item unless item.nil? or self.stats[:specific][name][state][:items].include?(item)
  time_elapsed = self.stats[:specific][name][state][:time_elapsed]
  time_elapsed[:total] += finished - started
  if time_elapsed[:maximum] < (finished - started)
    time_elapsed[:maximum] = (finished - started)
    if !item.nil?
      time_elapsed[:item_maximum] = item
    elsif Thread.current.name.to_s != ""
      time_elapsed[:item_maximum] = Thread.current.name
    end
  end
  if time_elapsed[:minimum] > (finished - started)
    time_elapsed[:minimum] = (finished - started)
    if !item.nil?
      time_elapsed[:item_minimum] = item
    elsif Thread.current.name.to_s != ""
      time_elapsed[:item_minimum] = Thread.current.name
    end
  end
  time_elapsed[:average] = time_elapsed[:total] / self.stats[:specific][name][state][:num]
end

.defaults=(par = {}) ⇒ Object

Change the default values for NiceHttp supplying a Hash

Parameters:

  • par (Hash) (defaults to: {})

    keys: :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port, :use_mocks, :auto_redirect, :values_for, :create_stats



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nice_http.rb', line 136

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].dup if par.key?(:headers)
  @values_for = par[:values_for].dup if par.key?(:values_for)
  @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)
  @use_mocks = par[:use_mocks] if par.key?(:use_mocks)
  @auto_redirect = par[:auto_redirect] if par.key?(:auto_redirect)
  @create_stats = par[:create_stats] if par.key?(:create_stats)
end

.inherited(subclass) ⇒ Object

If inheriting from NiceHttp class



124
125
126
# File 'lib/nice_http.rb', line 124

def self.inherited(subclass)
  subclass.reset!
end

.reset!Object

to reset to the original defaults



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
# File 'lib/nice_http.rb', line 82

def self.reset!
  @host = nil
  @port = 80
  @ssl = false
  @headers = {}
  @values_for = {}
  @debug = false
  @log = :fix_file
  @proxy_host = nil
  @proxy_port = nil
  @last_request = nil
  @last_response = nil
  @request_id = ""
  @use_mocks = false
  @connections = []
  @active = 0
  @auto_redirect = true
  @log_files = {}
  @create_stats = false
  @stats = {
    all: {
      num_requests: 0,
      started: nil,
      finished: nil,
      real_time_elapsed: 0,
      time_elapsed: {
        total: 0,
        maximum: 0,
        minimum: 1000000,
        average: 0,
      },
      method: {},
    },
    path: {},
    name: {},
  }
end

.save_stats(file_name = "") ⇒ Object

It will save the NiceHttp.stats on different files, each key of the hash in a different file.

Examples:

NiceHttp.save_stats
NiceHttp.save_stats('./stats/my_stats.yaml')
NiceHttp.save_stats('./stats/my_stats.json')

Parameters:

  • file_name (String) (defaults to: "")

    path and file name to be used to store the stats. In case no one supplied it will be used the value in NiceHttp.log and it will be saved on YAML format. In case extension is .yaml will be saved on YAML format. In case extension is .json will be saved on JSON format.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/nice_http.rb', line 248

def self.save_stats(file_name = "")
  if file_name == ""
    if self.log.is_a?(String)
      file_name = self.log
    else
      file_name = "./nice_http.log"
    end
  end
  require "fileutils"
  FileUtils.mkdir_p File.dirname(file_name)
  if file_name.match?(/\.json$/)
    require "json"
    self.stats.keys.each do |key|
      File.open("#{file_name.gsub(/.json$/, "_stats_")}#{key}.json", "w") { |file| file.write(self.stats[key].to_json) }
    end
  else
    require "yaml"
    self.stats.keys.each do |key|
      File.open("#{file_name.gsub(/.\w+$/, "_stats_")}#{key}.yaml", "w") { |file| file.write(self.stats[key].to_yaml) }
    end
  end
end

Instance Method Details

#closeObject

Close HTTP connection



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
# File 'lib/nice_http.rb', line 476

def close
  begin
    pos = 0
    found = false
    self.class.connections.each { |conn|
      if conn.object_id == self.object_id
        found = true
        break
      else
        pos += 1
      end
    }
    if found
      self.class.connections.delete_at(pos)
    end

    unless @closed
      if !@http.nil?
        @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
  self.class.active -= 1
end