Class: WWW::Mechanize::PluggableParser

Inherits:
Object
  • Object
show all
Defined in:
lib/www/mechanize/pluggable_parsers.rb

Overview

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.

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePluggableParser

Returns a new instance of PluggableParser.



57
58
59
60
61
62
63
# File 'lib/www/mechanize/pluggable_parsers.rb', line 57

def initialize
  @parsers = { CONTENT_TYPES[:html]   => Page,
               CONTENT_TYPES[:xhtml]  => Page,
               CONTENT_TYPES[:wap]    => Page,
  }
  @default = File
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



55
56
57
# File 'lib/www/mechanize/pluggable_parsers.rb', line 55

def default
  @default
end

Instance Method Details

#[](content_type) ⇒ Object



94
95
96
# File 'lib/www/mechanize/pluggable_parsers.rb', line 94

def [](content_type)
  @parsers[content_type]
end

#[]=(content_type, klass) ⇒ Object



98
99
100
# File 'lib/www/mechanize/pluggable_parsers.rb', line 98

def []=(content_type, klass)
  @parsers[content_type] = klass
end

#csv=(klass) ⇒ Object



86
87
88
# File 'lib/www/mechanize/pluggable_parsers.rb', line 86

def csv=(klass)
  register_parser(CONTENT_TYPES[:csv], klass)
end

#html=(klass) ⇒ Object



73
74
75
76
# File 'lib/www/mechanize/pluggable_parsers.rb', line 73

def html=(klass)
  register_parser(CONTENT_TYPES[:html], klass)
  register_parser(CONTENT_TYPES[:xhtml], klass)
end

#parser(content_type) ⇒ Object



65
66
67
# File 'lib/www/mechanize/pluggable_parsers.rb', line 65

def parser(content_type)
  content_type.nil? ? default : @parsers[content_type] || default
end

#pdf=(klass) ⇒ Object



82
83
84
# File 'lib/www/mechanize/pluggable_parsers.rb', line 82

def pdf=(klass)
  register_parser(CONTENT_TYPES[:pdf], klass)
end

#register_parser(content_type, klass) ⇒ Object



69
70
71
# File 'lib/www/mechanize/pluggable_parsers.rb', line 69

def register_parser(content_type, klass)
  @parsers[content_type] = klass
end

#xhtml=(klass) ⇒ Object



78
79
80
# File 'lib/www/mechanize/pluggable_parsers.rb', line 78

def xhtml=(klass)
  register_parser(CONTENT_TYPES[:xhtml], klass)
end

#xml=(klass) ⇒ Object



90
91
92
# File 'lib/www/mechanize/pluggable_parsers.rb', line 90

def xml=(klass)
  register_parser(CONTENT_TYPES[:xml], klass)
end