Class: WebPageParser::ParserFactory
- Inherits:
-
Object
- Object
- WebPageParser::ParserFactory
- Includes:
- Oniguruma
- Defined in:
- lib/web-page-parser/parser_factory.rb
Direct Known Subclasses
TestPageParserFactory, BbcNewsPageParserFactory, GuardianPageParserFactory
Constant Summary collapse
- @@factories =
[]
Class Method Summary collapse
- .add_factory(f) ⇒ Object
- .factories ⇒ Object
-
.inherited(factory) ⇒ Object
Keep track of any newly defined factories.
-
.load(dirname) ⇒ Object
Load all the plugins in the given directory.
-
.parser_for(options = {}) ⇒ Object
Return a PageParser than can parse the given page.
Instance Method Summary collapse
-
#can_parse?(options = {}) ⇒ Boolean
return true if the Parser can handle the given page.
-
#create(options = {}) ⇒ Object
Allocate a new parser.
Class Method Details
.add_factory(f) ⇒ Object
20 21 22 |
# File 'lib/web-page-parser/parser_factory.rb', line 20 def self.add_factory(f) @@factories << f unless @@factories.include? f end |
.factories ⇒ Object
24 25 26 |
# File 'lib/web-page-parser/parser_factory.rb', line 24 def self.factories @@factories end |
.inherited(factory) ⇒ Object
Keep track of any newly defined factories
46 47 48 |
# File 'lib/web-page-parser/parser_factory.rb', line 46 def self.inherited(factory) self.add_factory(factory) end |
.load(dirname) ⇒ Object
Load all the plugins in the given directory
38 39 40 41 42 43 |
# File 'lib/web-page-parser/parser_factory.rb', line 38 def self.load(dirname) Dir.open(dirname).each do |fn| next unless fn =~ /page_parser\.rb$/ require File.join(dirname, fn) end end |
.parser_for(options = {}) ⇒ Object
Return a PageParser than can parse the given page. options hash must have a :url key
30 31 32 33 34 35 |
# File 'lib/web-page-parser/parser_factory.rb', line 30 def self.parser_for( = {}) @@factories.each do |factory| return factory.create() if factory.can_parse?() end nil end |
Instance Method Details
#can_parse?(options = {}) ⇒ Boolean
return true if the Parser can handle the given page. options hash must have a :url key
8 9 10 |
# File 'lib/web-page-parser/parser_factory.rb', line 8 def can_parse?( = {}) false end |
#create(options = {}) ⇒ Object
Allocate a new parser. options hash is passed to new method of parser class.
14 15 16 |
# File 'lib/web-page-parser/parser_factory.rb', line 14 def create( = {}) nil end |