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, IndependentPageParserFactory, NewYorkTimesPageParserFactory, WashingtonPostPageParserFactory
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
19 20 21 |
# File 'lib/web-page-parser/parser_factory.rb', line 19 def self.add_factory(f) @@factories << f unless @@factories.include? f end |
.factories ⇒ Object
23 24 25 |
# File 'lib/web-page-parser/parser_factory.rb', line 23 def self.factories @@factories end |
.inherited(factory) ⇒ Object
Keep track of any newly defined factories
45 46 47 |
# File 'lib/web-page-parser/parser_factory.rb', line 45 def self.inherited(factory) self.add_factory(factory) end |
.load(dirname) ⇒ Object
Load all the plugins in the given directory
37 38 39 40 41 42 |
# File 'lib/web-page-parser/parser_factory.rb', line 37 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
29 30 31 32 33 34 |
# File 'lib/web-page-parser/parser_factory.rb', line 29 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
7 8 9 |
# File 'lib/web-page-parser/parser_factory.rb', line 7 def can_parse?( = {}) false end |
#create(options = {}) ⇒ Object
Allocate a new parser. options hash is passed to new method of parser class.
13 14 15 |
# File 'lib/web-page-parser/parser_factory.rb', line 13 def create( = {}) nil end |