Module: Termtter::HTTPpool

Defined in:
lib/termtter/httppool.rb

Constant Summary collapse

@@connections =
{}
@@http_class =
nil

Class Method Summary collapse

Class Method Details

.connection(host, port = 80, ssl = false) ⇒ Object


26
27
28
29
30
31
32
# File 'lib/termtter/httppool.rb', line 26

def self.connection(host, port = 80, ssl = false)
  key = connection_key(host, port)
  http_io = http_class.new(host, port)
  http_io.use_ssl = ssl
  http_io.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @@connections[key] ||= http_io.start
end

.connection_key(host, port) ⇒ Object


40
41
42
# File 'lib/termtter/httppool.rb', line 40

def self.connection_key(host, port)
  port == 80 ? host : [host, port.to_s].join(':')
end

.finish(host, port = 80) ⇒ Object


34
35
36
37
38
# File 'lib/termtter/httppool.rb', line 34

def self.finish(host, port = 80)
  key = connection_key(host, port)
  @@connections[key] && @@connections[key].do_finish rescue nil
  @@connections.delete(key)
end

.http_classObject


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/termtter/httppool.rb', line 44

def self.http_class
  @@http_class ||=
    if config.proxy.host.nil? or config.proxy.host.empty?
      Net::HTTP
    else
      Net::HTTP::Proxy(
        config.proxy.host,
        config.proxy.port,
        config.proxy.user_name,
        config.proxy.password)
    end
end

.start(host, port = 80, ssl = false) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/termtter/httppool.rb', line 12

def self.start(host, port = 80, ssl = false)
  count = config.retry || 3
  begin
    yield(connection(host, port, ssl))
  rescue EOFError
    finish(host, port)
    if count > 0
      count -= 1
      retry
    end
    raise
  end
end