Class: Net::HTTP::ConnectionPool::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/net/http/connection_pool/session.rb

Overview

Used by Net::HTTP::ConnectionPool to wrap Net::HTTP::Session objects. Users should never need to interact with these session wrappers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_session, key) ⇒ Session

Returns a new instance of Session.

Parameters:

  • http_session (Net::HTTPSession)
  • key (String)


27
28
29
30
31
32
# File 'lib/net/http/connection_pool/session.rb', line 27

def initialize http_session, key
  @http_session = http_session
  @key = key
  @created_at = Time.now
  @last_used_at = nil
end

Instance Attribute Details

#created_atTime (readonly)

Returns:

  • (Time)


41
42
43
# File 'lib/net/http/connection_pool/session.rb', line 41

def created_at
  @created_at
end

#http_sessionNet::HTTPSession (readonly)

Returns:

  • (Net::HTTPSession)


35
36
37
# File 'lib/net/http/connection_pool/session.rb', line 35

def http_session
  @http_session
end

#keyString (readonly)

Returns:

  • (String)


38
39
40
# File 'lib/net/http/connection_pool/session.rb', line 38

def key
  @key
end

#last_used_atTime (readonly)

Returns:

  • (Time)


44
45
46
# File 'lib/net/http/connection_pool/session.rb', line 44

def last_used_at
  @last_used_at
end

Class Method Details

.start(connection, options = {}) ⇒ Session

Starts a new HTTP session and returns it wrapped in a Net::HTTP::ConnectionPool::Session object.

Parameters:

  • connection (Connection)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :open_timeout (Integer) — default: 15

    The number of seconds to wait while trying to open the HTTP session before timeing out.

  • :debug_logger (Logger)

    HTTP wire traces are logged here when specified.

Returns:



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
# File 'lib/net/http/connection_pool/session.rb', line 89

def start connection, options = {}

  http_args = []
  http_args << connection.host
  http_args << connection.port
  if connection.proxy?
    http_args << connection.proxy_address
    http_args << connection.proxy_port
    http_args << connection.proxy_user
    http_args << connection.proxy_password
  end

  http = Net::HTTP.new(*http_args)
  http.set_debug_output(options[:debug_logger]) if options[:debug_logger]
  http.open_timeout = options[:open_timeout] || 15

  if connection.ssl?
    http.use_ssl = true
    if connection.ssl_verify_peer?
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.ca_file = connection.ssl_ca_file if connection.ssl_ca_file
      http.ca_path = connection.ssl_ca_path if connection.ssl_ca_path
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  else
    http.use_ssl = false
  end

  http.start

  Session.new(http, connection.key)

end

Instance Method Details

#finishnil

Attempts to cleanly close the HTTP session.

Returns:

  • (nil)


71
72
73
74
75
76
77
# File 'lib/net/http/connection_pool/session.rb', line 71

def finish
  begin
    http_session.finish if http_session.started?
  rescue IOError
  end
  nil
end

#read_timeout=(timeout) ⇒ Object

Parameters:

  • timeout (Integer)

    Number of seconds before Net::HTTP should timeout while waiting to read a response.



48
49
50
# File 'lib/net/http/connection_pool/session.rb', line 48

def read_timeout= timeout
  http_session.read_timeout = timeout
end

#request(*args) {|response| ... } ⇒ nil

Makes a HTTP request. See Net::HTTPSession#request documentation from the Ruby standard library for information about argments.

connection.request(Net::HTTP::Get.new('/')) do |response|
  # Parse the response (status, headers and body) here.
  # You should be done with the response by the end of the block.
end

Yields:

  • (response)

Yield Parameters:

  • response (Net::HTTPResponse)

Returns:

  • (nil)


63
64
65
66
67
# File 'lib/net/http/connection_pool/session.rb', line 63

def request *args, &block
  http_session.request(*args, &block)
  @last_used_at = Time.now
  nil
end