class WWW::Mechanize::PluggableParser
Now all unknown content types will be instances of SomeClass.
agent.pluggable_parser.default = SomeClass
To set the default parser, just use the ‘defaut’ method:<br><br>agent.pluggable_parser = SomeClass
does not know about, just use the hash syntax:
To register a pluggable parser for a content type that pluggable parser
a CSVParser and return that object to the caller.
Now any page that returns the content type of ‘text/csv’ will initialize
agent.get(‘example.com/test.csv’) # => CSVParser
agent.pluggable_parser.csv = CSVParser
agent = WWW::Mechanize.new
end
end
@csv = CSV.parse(body)
super(uri, response, body, code)
def initialize(uri=nil, response=nil, body=nil, code=nil)
attr_reader :csv
class CSVParser < WWW::Mechanize::File
a pluggable parser that handles CSV files:
parameters in the constructor. Here is an example of registering
To create your own parser, just create a class that takes four
== Example
extend when building your own parsers.
basic functionality for any content type, so it is a good class to
that it does not know how to handle. WWW::Mechanize::File provides
PluggableParser returns a WWW::Mechanize::File object for content types
parsers.
register their own pluggable parsers, or modify existing pluggable
should initialize given any content type. This class allows users to
content type. Mechanize will ask PluggableParser for the class it
A Pluggable Parser is a parser that Mechanize uses for any particular
Mechanize to use.
This class is used to register and maintain pluggable parsers for
= Synopsis
def [](content_type)
def [](content_type) @parsers[content_type] end
def []=(content_type, klass)
def []=(content_type, klass) @parsers[content_type] = klass end
def csv=(klass)
def csv=(klass) register_parser(CONTENT_TYPES[:csv], klass) end
def html=(klass)
def html=(klass) register_parser(CONTENT_TYPES[:html], klass) register_parser(CONTENT_TYPES[:xhtml], klass) end
def initialize
def initialize @parsers = { CONTENT_TYPES[:html] => Page, CONTENT_TYPES[:xhtml] => Page } @default = File end
def parser(content_type)
def parser(content_type) content_type.nil? ? default : @parsers[content_type] || default end
def pdf=(klass)
def pdf=(klass) register_parser(CONTENT_TYPES[:pdf], klass) end
def register_parser(content_type, klass)
def register_parser(content_type, klass) @parsers[content_type] = klass end
def xhtml=(klass)
def xhtml=(klass) register_parser(CONTENT_TYPES[:xhtml], klass) end
def xml=(klass)
def xml=(klass) register_parser(CONTENT_TYPES[:xml], klass) end