Class: Scraper
- Inherits:
-
Object
- Object
- Scraper
- Defined in:
- lib/object-scraper/scraper.rb
Defined Under Namespace
Classes: DuplicateDefinitionError
Class Attribute Summary collapse
-
.definition_file_paths ⇒ Object
Returns the value of attribute definition_file_paths.
-
.scrape_source_with ⇒ Object
Returns the value of attribute scrape_source_with.
-
.scrapers ⇒ Object
Returns the value of attribute scrapers.
Instance Attribute Summary collapse
-
#scraper_node ⇒ Object
readonly
Returns the value of attribute scraper_node.
-
#scraper_source ⇒ Object
readonly
Returns the value of attribute scraper_source.
Class Method Summary collapse
- .define(name, options = {}, &block) ⇒ Object
- .find_definitions ⇒ Object
- .get(name) ⇒ Object
- .parse(name) ⇒ Object
- .parse_all ⇒ Object
- .scraper_by_name(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(name, options = {}, &block) ⇒ Scraper
constructor
:nodoc:.
- #method_missing(symbol, *args, &block) ⇒ Object
- #parse ⇒ Object
Constructor Details
#initialize(name, options = {}, &block) ⇒ Scraper
:nodoc:
29 30 31 32 33 34 35 36 |
# File 'lib/object-scraper/scraper.rb', line 29 def initialize(name, = {}, &block) #:nodoc: () @objects = [] @class = class_for([:class]) @scraper_source = [:source] @scraper_node = [:node] @block = block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/object-scraper/scraper.rb', line 67 def method_missing(symbol, *args, &block) if block_given? @current_object.send("#{symbol}=", begin yield(@current_node) rescue puts "Warning, parsing failed at #{@current_node.inspect}" end) else @current_object.send("#{symbol}=", args.first) end end |
Class Attribute Details
.definition_file_paths ⇒ Object
Returns the value of attribute definition_file_paths.
10 11 12 |
# File 'lib/object-scraper/scraper.rb', line 10 def definition_file_paths @definition_file_paths end |
.scrape_source_with ⇒ Object
Returns the value of attribute scrape_source_with.
9 10 11 |
# File 'lib/object-scraper/scraper.rb', line 9 def scrape_source_with @scrape_source_with end |
.scrapers ⇒ Object
Returns the value of attribute scrapers.
8 9 10 |
# File 'lib/object-scraper/scraper.rb', line 8 def scrapers @scrapers end |
Instance Attribute Details
#scraper_node ⇒ Object (readonly)
Returns the value of attribute scraper_node.
17 18 19 |
# File 'lib/object-scraper/scraper.rb', line 17 def scraper_node @scraper_node end |
#scraper_source ⇒ Object (readonly)
Returns the value of attribute scraper_source.
17 18 19 |
# File 'lib/object-scraper/scraper.rb', line 17 def scraper_source @scraper_source end |
Class Method Details
.define(name, options = {}, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/object-scraper/scraper.rb', line 19 def self.define(name, = {}, &block) instance = Scraper.new(name, , &block) if self.scrapers[name] raise DuplicateDefinitionError, "Scraper already defined: #{name}" end self.scrapers[name] = instance end |
.find_definitions ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/object-scraper/scraper.rb', line 79 def self.find_definitions definition_file_paths.each do |path| require("#{path}.rb") if File.exists?("#{path}.rb") if File.directory? path Dir[File.join(path, '*.rb')].each do |file| require file end end end end |
.get(name) ⇒ Object
38 39 40 |
# File 'lib/object-scraper/scraper.rb', line 38 def self.get(name) scraper_by_name(name) end |
.parse(name) ⇒ Object
42 43 44 |
# File 'lib/object-scraper/scraper.rb', line 42 def self.parse(name) scraper_by_name(name).parse end |
.parse_all ⇒ Object
46 47 48 49 50 |
# File 'lib/object-scraper/scraper.rb', line 46 def self.parse_all objects = [] scrapers.each_value { |s| objects << s.parse } objects.flatten end |
.scraper_by_name(name) ⇒ Object
63 64 65 |
# File 'lib/object-scraper/scraper.rb', line 63 def self.scraper_by_name(name) scrapers[name.to_sym] or raise ArgumentError, "No such scraper: #{name.to_s}" end |
Instance Method Details
#parse ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/object-scraper/scraper.rb', line 52 def parse doc = open(@scraper_source) { |f| Scraper.scrape_source_with.call(f) } doc.search(@scraper_node).each do |n| @current_node = n @current_object = @class.new @objects << @current_object @block.call(self) end @objects end |