Class: WebPageParser::ParserFactory

Inherits:
Object
  • Object
show all
Includes:
Oniguruma
Defined in:
lib/web-page-parser/parser_factory.rb

Constant Summary collapse

@@factories =
[]

Class Method Summary collapse

Instance Method Summary collapse

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

.factoriesObject



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(options = {})
  @@factories.each do |factory|
    return factory.create(options) if factory.can_parse?(options)
  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

Returns:

  • (Boolean)


8
9
10
# File 'lib/web-page-parser/parser_factory.rb', line 8

def can_parse?(options = {})
  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(options = {})
  nil
end