Method: Mechanize#initialize

Defined in:
lib/mechanize.rb

#initialize(connection_name = 'mechanize') {|_self| ... } ⇒ Mechanize

Creates a new mechanize instance. If a block is given, the created instance is yielded to the block for setting up pre-connection state such as SSL parameters or proxies:

agent = Mechanize.new do |a|
  a.proxy_addr = 'proxy.example'
  a.proxy_port = 8080
end

If you need segregated SSL connections give each agent a unique name. Otherwise the connections will be shared. This is particularly important if you are using certificates.

agent_1 = Mechanize.new 'conn1'
agent_2 = Mechanize.new 'conn2'

Yields:

  • (_self)

Yield Parameters:

  • _self (Mechanize)

    the object that the method was called on



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

def initialize(connection_name = 'mechanize')
  @agent = Mechanize::HTTP::Agent.new(connection_name)
  @agent.context = self
  @log = nil

  # attr_accessors
  @agent.user_agent = AGENT_ALIASES['Mechanize']
  @watch_for_set    = nil
  @history_added    = nil

  # attr_readers
  @pluggable_parser = PluggableParser.new

  @keep_alive_time  = 0

  # Proxy
  @proxy_addr = nil
  @proxy_port = nil
  @proxy_user = nil
  @proxy_pass = nil

  @html_parser = self.class.html_parser

  @default_encoding = nil
  @force_default_encoding = false

  # defaults
  @agent.max_history = 50

  yield self if block_given?

  @agent.set_proxy @proxy_addr, @proxy_port, @proxy_user, @proxy_pass
end