Class WWW::Mechanize::PluggableParser
In: lib/www/mechanize/pluggable_parsers.rb
Parent: Object
Mechanize\n[lib/www/mechanize.rb\nlib/www/mechanize/chain.rb\nlib/www/mechanize/chain/auth_headers.rb\nlib/www/mechanize/chain/body_decoding_handler.rb\nlib/www/mechanize/chain/connection_resolver.rb\nlib/www/mechanize/chain/custom_headers.rb\nlib/www/mechanize/chain/header_resolver.rb\nlib/www/mechanize/chain/parameter_resolver.rb\nlib/www/mechanize/chain/pre_connect_hook.rb\nlib/www/mechanize/chain/request_resolver.rb\nlib/www/mechanize/chain/response_body_parser.rb\nlib/www/mechanize/chain/response_header_handler.rb\nlib/www/mechanize/chain/response_reader.rb\nlib/www/mechanize/chain/ssl_resolver.rb\nlib/www/mechanize/chain/uri_resolver.rb\nlib/www/mechanize/content_type_error.rb\nlib/www/mechanize/cookie.rb\nlib/www/mechanize/cookie_jar.rb\nlib/www/mechanize/file.rb\nlib/www/mechanize/file_response.rb\nlib/www/mechanize/file_saver.rb\nlib/www/mechanize/form.rb\nlib/www/mechanize/form/button.rb\nlib/www/mechanize/form/check_box.rb\nlib/www/mechanize/form/field.rb\nlib/www/mechanize/form/file_upload.rb\nlib/www/mechanize/form/image_button.rb\nlib/www/mechanize/form/multi_select_list.rb\nlib/www/mechanize/form/option.rb\nlib/www/mechanize/form/radio_button.rb\nlib/www/mechanize/form/select_list.rb\nlib/www/mechanize/headers.rb\nlib/www/mechanize/history.rb\nlib/www/mechanize/monkey_patch.rb\nlib/www/mechanize/page.rb\nlib/www/mechanize/page/base.rb\nlib/www/mechanize/page/frame.rb\nlib/www/mechanize/page/link.rb\nlib/www/mechanize/page/meta.rb\nlib/www/mechanize/pluggable_parsers.rb\nlib/www/mechanize/redirect_limit_reached_error.rb\nlib/www/mechanize/redirect_not_get_or_head_error.rb\nlib/www/mechanize/response_code_error.rb\nlib/www/mechanize/unsupported_scheme_error.rb\nlib/www/mechanize/util.rb] lib/www/mechanize.rb Handler WWW dot/m_52_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   xhtml=   xml=  

Constants

CONTENT_TYPES = { :html => 'text/html', :wap => 'application/vnd.wap.xhtml+xml', :xhtml => 'application/xhtml+xml', :pdf => 'application/pdf', :csv => 'text/csv', :xml => 'text/xml', }

Attributes

default  [RW] 

Public Class methods

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 57
57:       def initialize
58:         @parsers = { CONTENT_TYPES[:html]   => Page,
59:                      CONTENT_TYPES[:xhtml]  => Page,
60:                      CONTENT_TYPES[:wap]    => Page,
61:         }
62:         @default = File
63:       end

Public Instance methods

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 94
94:       def [](content_type)
95:         @parsers[content_type]
96:       end

[Source]

     # File lib/www/mechanize/pluggable_parsers.rb, line 98
 98:       def []=(content_type, klass)
 99:         @parsers[content_type] = klass
100:       end

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 86
86:       def csv=(klass)
87:         register_parser(CONTENT_TYPES[:csv], klass)
88:       end

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 73
73:       def html=(klass)
74:         register_parser(CONTENT_TYPES[:html], klass)
75:         register_parser(CONTENT_TYPES[:xhtml], klass)
76:       end

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 65
65:       def parser(content_type)
66:         content_type.nil? ? default : @parsers[content_type] || default
67:       end

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 82
82:       def pdf=(klass)
83:         register_parser(CONTENT_TYPES[:pdf], klass)
84:       end

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 69
69:       def register_parser(content_type, klass)
70:         @parsers[content_type] = klass
71:       end

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 78
78:       def xhtml=(klass)
79:         register_parser(CONTENT_TYPES[:xhtml], klass)
80:       end

[Source]

    # File lib/www/mechanize/pluggable_parsers.rb, line 90
90:       def xml=(klass)
91:         register_parser(CONTENT_TYPES[:xml], klass)
92:       end

[Validate]