Class WWW::Mechanize::PluggableParser
In: lib/mechanize/pluggable_parsers.rb
Parent: Object
Mechanize\n[lib/mechanize.rb\nlib/mechanize/cookie.rb\nlib/mechanize/errors.rb\nlib/mechanize/form.rb\nlib/mechanize/form_elements.rb\nlib/mechanize/history.rb\nlib/mechanize/list.rb\nlib/mechanize/page.rb\nlib/mechanize/page_elements.rb\nlib/mechanize/pluggable_parsers.rb] lib/mechanize.rb WWW dot/m_19_0.png

Synopsis

This class is used to register and maintain pluggable parsers for Mechanize to use.

A Pluggable Parser is a parser that Mechanize uses for any particular content type. Mechanize will ask PluggableParser for the class it should initialize given any content type. This class allows users to register their own pluggable parsers, or modify existing pluggable parsers.

PluggableParser returns a WWW::Mechanize::File object for content types that it does not know how to handle. WWW::Mechanize::File provides basic functionality for any content type, so it is a good class to extend when building your own parsers.

Example

To create your own parser, just create a class that takes four parameters in the constructor. Here is an example of registering a pluggable parser that handles CSV files:

 class CSVParser < WWW::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 = WWW::Mechanize.new
 agent.pluggable_parser.csv = CSVParser
 agent.get('http://example.com/test.csv')  # => CSVParser

Now any page that returns the content type of ‘text/csv’ will initialize a CSVParser and return that object to the caller.

To register a pluggable parser for a content type that pluggable parser does not know about, just use the hash syntax:

 agent.pluggable_parser['text/something'] = SomeClass

To set the default parser, just use the ‘defaut’ method:

 agent.pluggable_parser.default = SomeClass

Now all unknown content types will be instances of SomeClass.

Methods

[]   []=   csv=   html=   new   parser   pdf=   register_parser   xml=  

Constants

CONTENT_TYPES = { :html => 'text/html', :pdf => 'application/pdf', :csv => 'text/csv', :xml => 'text/xml', }

Attributes

default  [RW] 

Public Class methods

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 157
157:       def initialize
158:         @parsers = { CONTENT_TYPES[:html] => Page }
159:         @default = File
160:       end

Public Instance methods

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 186
186:       def [](content_type)
187:         @parsers[content_type]
188:       end

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 190
190:       def []=(content_type, klass)
191:         @parsers[content_type] = klass
192:       end

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 178
178:       def csv=(klass)
179:         register_parser(CONTENT_TYPES[:csv], klass)
180:       end

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 170
170:       def html=(klass)
171:         register_parser(CONTENT_TYPES[:html], klass)
172:       end

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 162
162:       def parser(content_type)
163:         content_type.nil? ? default : @parsers[content_type] || default
164:       end

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 174
174:       def pdf=(klass)
175:         register_parser(CONTENT_TYPES[:pdf], klass)
176:       end

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 166
166:       def register_parser(content_type, klass)
167:         @parsers[content_type] = klass
168:       end

[Source]

     # File lib/mechanize/pluggable_parsers.rb, line 182
182:       def xml=(klass)
183:         register_parser(CONTENT_TYPES[:xml], klass)
184:       end

[Validate]