Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Rails) Can't get Mechanize to correctly read a web xml file
    text
    copied!<p>I have to read xml files that are accessibles through http with authentication. That's why I use mechanize.</p> <p>My problem is that I can't get mechanize to recognize these XML files so I can use .find or .search on them.</p> <p>Here is what I tried first - in my view (html file)</p> <p><code>&lt;% agent = Mechanize.new %&gt;</code></p> <p><code>&lt;% page = agent.get("http://dl.dropbox.com/u/344349/xml.xml") %&gt;</code></p> <p><code>&lt;%= page %&gt;</code></p> <p>Which returns <code>#&lt;Mechanize::File:0x007f9dd602de30&gt;</code>. It's <code>::File</code> and not <code>::Page</code> I can't use a .find or .search on this as it'll error with <code>undefined method find for #&lt;Mechanize::File:0x007f9dd624cbd0&gt;</code></p> <p>Mechanize doc says : This is the default (and base) class for the Pluggable Parsers. If Mechanize cannot find an appropriate class to use for the content type, this class will be used. For example, if you download a JPG, Mechanize will not know how to parse it, so this class will be instantiated.</p> <p>So I created a class as described here : <a href="http://rdoc.info/github/tenderlove/mechanize/master/Mechanize/PluggableParser" rel="nofollow">http://rdoc.info/github/tenderlove/mechanize/master/Mechanize/PluggableParser</a></p> <p><code>My class</code></p> <p><code>class XMLParser &lt; Mechanize::File</code></p> <p><code>attr_reader :xml</code></p> <p><code>def initialize(uri=nil, response=nil, body=nil, code=nil)</code></p> <p><code>super(uri, response, body, code)</code></p> <p><code>@xml = xml.parse(body)</code></p> <p><code>end</code></p> <p><code>end</code></p> <p>and the updated code in my view (html file)</p> <p><code>&lt;% agent = Mechanize.new %&gt;</code></p> <p><code>&lt;% agent.pluggable_parser['text/xml'] = XMLParser %&gt;</code></p> <p><code>&lt;% agent.user_agent_alias = 'Windows Mozilla' %&gt;</code></p> <p><code>&lt;% page = agent.get("http://dl.dropbox.com/u/344349/xml.xml") %&gt;</code> </p> <p><code>&lt;%= page %&gt;</code></p> <p>or even</p> <p><code>&lt;% agent = Mechanize.new %&gt;</code></p> <p><code>&lt;% agent.pluggable_parser.xml = XMLParser %&gt;</code></p> <p><code>&lt;% page1 = agent.get('http://dl.dropbox.com/u/344349/xml.xml') # =&gt; CSVParser %&gt;</code></p> <p><code>&lt;%= page1 %&gt;</code></p> <p>Still returns <code>#&lt;Mechanize::File:0x007f9dd5253b48&gt;</code></p> <p>I even tested the exact code (CSVParser - <a href="http://rdoc.info/github/tenderlove/mechanize/master/Mechanize/PluggableParser" rel="nofollow">http://rdoc.info/github/tenderlove/mechanize/master/Mechanize/PluggableParser</a>) and tried loading a csv file that is still seen as a ::File.</p> <p>What am I doing wrong ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload