Parent

Class Index [+]

Quicksearch

Mechanize::PluggableParser

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 Mechanize::File object for content types that it does not know how to handle. 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 < 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 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.

Constants

CONTENT_TYPES

Attributes

default[RW]

Public Class Methods

new() click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 56
56:     def initialize
57:       @parsers = { CONTENT_TYPES[:html]   => Page,
58:         CONTENT_TYPES[:xhtml]  => Page,
59:         CONTENT_TYPES[:wap]    => Page,
60:       }
61:       @default = File
62:     end

Public Instance Methods

[](content_type) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 93
93:     def [](content_type)
94:       @parsers[content_type]
95:     end
[]=(content_type, klass) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 97
97:     def []=(content_type, klass)
98:       @parsers[content_type] = klass
99:     end
csv=(klass) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 85
85:     def csv=(klass)
86:       register_parser(CONTENT_TYPES[:csv], klass)
87:     end
html=(klass) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 72
72:     def html=(klass)
73:       register_parser(CONTENT_TYPES[:html], klass)
74:       register_parser(CONTENT_TYPES[:xhtml], klass)
75:     end
parser(content_type) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 64
64:     def parser(content_type)
65:       content_type.nil? ? default : @parsers[content_type] || default
66:     end
pdf=(klass) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 81
81:     def pdf=(klass)
82:       register_parser(CONTENT_TYPES[:pdf], klass)
83:     end
register_parser(content_type, klass) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 68
68:     def register_parser(content_type, klass)
69:       @parsers[content_type] = klass
70:     end
xhtml=(klass) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 77
77:     def xhtml=(klass)
78:       register_parser(CONTENT_TYPES[:xhtml], klass)
79:     end
xml=(klass) click to toggle source
    # File lib/mechanize/pluggable_parsers.rb, line 89
89:     def xml=(klass)
90:       register_parser(CONTENT_TYPES[:xml], klass)
91:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.