Add a default pluggable parser for XML documents #289

Closed
postmodern opened this Issue Feb 8, 2013 · 2 comments

Projects

None yet

2 participants

@postmodern
Contributor

As mentioned in issue #288, Mechanize's pluggable parser can be used to automatically parse requested XML documents. It is fairly common to encounter an XML document while using Mechanize. Therefore, Mechanize should include a parser for handling XML documents.

require 'mechanize'

class XMLFile < Mechanize::File
  attr_reader :doc

  def initialize uri = nil, response = nil, body = nil, code = nil
    super uri, response, body, code
    @doc = Nokogiri.parse body
  end
end

agent = Mechanize.new
agent.pluggable_parser.xml = XMLFile
page = Mechanize.get 'http://example.com/foo.xml'
page #=> XMLFile
page.doc #=> Nokogiri::Document

This would not add any additional dependencies, as Mechanize already relies on Nokogiri.

Owner

I've added this in a separate branch because I'm not really entirely sure about it, you could also argue that it would break compatibility with any applications using Mechanize to fetch XML files and expecting a Mechanize::File object (although XmlFile inherits from File so it shouldn't technically be an issue). It certainly needs more input, though.

/cc @drbrain

Owner

Merged xml-file into master

@leejarvis leejarvis closed this Feb 20, 2013
@leejarvis leejarvis added a commit that referenced this issue Feb 20, 2013
@leejarvis leejarvis updated changes for #289 025323d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment