Method: Mechanize::HTTP::Agent#initialize

Defined in:
lib/mechanize/http/agent.rb

#initialize(connection_name = 'mechanize') ⇒ Agent

The connection_name can be used to segregate SSL connections. Agents with different names will not share the same persistent connection.

[View source]

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
# File 'lib/mechanize/http/agent.rb', line 143

def initialize(connection_name = 'mechanize')
  @allowed_error_codes      = []
  @conditional_requests     = true
  @context                  = nil
  @content_encoding_hooks   = []
  @cookie_jar               = Mechanize::CookieJar.new
  @follow_meta_refresh      = false
  @follow_meta_refresh_self = false
  @gzip_enabled             = true
  @history                  = Mechanize::History.new
  @ignore_bad_chunking      = false
  @keep_alive               = true
  @max_file_buffer          = 100_000 # 5MB for response bodies
  @open_timeout             = nil
  @post_connect_hooks       = []
  @pre_connect_hooks        = []
  @read_timeout             = nil
  @redirect_ok              = true
  @redirection_limit        = 20
  @request_headers          = {}
  @robots                   = false
  @robots_mutex             = Mutex.new
  @user_agent               = nil
  @webrobots                = nil
  @write_timeout            = nil

  # HTTP Authentication
  @auth_store           = Mechanize::HTTP::AuthStore.new
  @authenticate_parser  = Mechanize::HTTP::WWWAuthenticateParser.new
  @authenticate_methods = Hash.new do |methods, uri|
    methods[uri] = Hash.new do |realms, auth_scheme|
      realms[auth_scheme] = []
    end
  end
  @digest_auth          = Net::HTTP::DigestAuth.new
  @digest_challenges    = {}

  # SSL
  @pass = nil

  @scheme_handlers = Hash.new { |h, scheme|
    h[scheme] = lambda { |link, page|
      raise Mechanize::UnsupportedSchemeError.new(scheme, link)
    }
  }

  @scheme_handlers['http']      = lambda { |link, page| link }
  @scheme_handlers['https']     = @scheme_handlers['http']
  @scheme_handlers['relative']  = @scheme_handlers['http']
  @scheme_handlers['file']      = @scheme_handlers['http']

  @http =
    if defined?(Net::HTTP::Persistent::DEFAULT_POOL_SIZE)
      Net::HTTP::Persistent.new(name: connection_name)
    else
      # net-http-persistent < 3.0
      Net::HTTP::Persistent.new(connection_name)
    end
  @http.idle_timeout = 5
  @http.keep_alive   = 300
end