Since Mechanize 2.0, the arguments pre_connect_hooks() and post_connect_hooks() have been changed.
See the Mechanize documentation:
pre_connect_hooks ()
A list of intercepts to call before receiving a response. Hooks are called using the agent, URI, response, and response body.
post_connect_hooks ()
List of hooks to call after receiving a response. Hooks are called using the agent, URI, response, and response body.
Now you cannot change the value of the internal body of the response, because the argument is not an array. So, the next best way is to replace the internal parser with your own:
class MyParser def self.parse(thing, url = nil, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_HTML, &block) # insert your conversion code here. For example: # thing = NKF.nkf("-wm0X", thing).sub(/Shift_JIS/,"utf-8") # you need to rewrite content charset if it exists. Nokogiri::HTML::Document.parse(thing, url, encoding, options, &block) end end agent = Mechanize.new agent.html_parser = MyParser page = agent.get('http://somewhere.com/') ...
source share