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



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

.factoriesObject



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(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)


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

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