Class | Mechanize::PluggableParser |
In: |
lib/mechanize/pluggable_parsers.rb
|
Parent: | Object |
Mechanize allows different parsers for different content types. Mechanize uses PluggableParser to determine which parser to use for any content type. To use your own parser or to change the default parsers, register them with this class through Mechanize#pluggable_parser.
The default parser for unregistered content types is Mechanize::File.
The module Mechanize::Parser provides basic functionality for any content type, so you may use it in custom parsers you write. For small files you wish to perform in-memory operations on, you should subclass Mechanize::File. For large files you should subclass Mechanize::Download as the content is only loaded into memory in small chunks.
When writing your own pluggable parser, be sure to provide a method body that returns a String containing the response body for compatibility with Mechanize#get_file.
To create your own parser, just create a class that takes four parameters in the constructor. Here is an example of registering a parser that handles CSV files:
require 'csv' class CSVParser < Mechanize::File attr_reader :csv def initialize uri = nil, response = nil, body = nil, code = nil super uri, response, body, code @csv = CSV.parse body end end agent = Mechanize.new agent.pluggable_parser.csv = CSVParser agent.get('http://example.com/test.csv') # => CSVParser
Now any response with a content type of ‘text/csv’ will initialize a CSVParser and return that object to the caller.
To register a parser for a content type that Mechanize does not know about, use the hash syntax:
agent.pluggable_parser['text/something'] = SomeClass
To set the default parser, use default:
agent.pluggable_parser.default = Mechanize::Download
Now all unknown content types will be saved to disk and not loaded into memory.
CONTENT_TYPES | = | { :html => 'text/html', :wap => 'application/vnd.wap.xhtml+xml', :xhtml => 'application/xhtml+xml', :pdf => 'application/pdf', :csv => 'text/csv', :xml => 'text/xml', } |
default | [RW] |
Sets the parser for content_type content to klass
The content_type may either be a full MIME type a simplified MIME type (‘text/x-csv’ simplifies to ‘text/csv’) or a media type like ‘image’.