Method: Grubby::Scraper.each
- Defined in:
- lib/grubby/scraper.rb
.each(start, agent = $grubby, next_method: :next) {|scraper| ... } ⇒ void
This method returns an undefined value.
Iterates a series of pages, starting at start. The Scraper class is instantiated with each page, and each Scraper instance is passed to the given block. Subsequent pages in the series are determined by invoking the next_method method on each Scraper instance.
Iteration stops when the next_method method returns falsy. If the next_method method returns a String or URI, that value will be treated as the URL of the next page. Otherwise that value will be treated as the page itself.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/grubby/scraper.rb', line 196 def self.each(start, agent = $grubby, next_method: :next) unless self.method_defined?(next_method) raise NoMethodError.new(nil, next_method), "#{self} does not define `#{next_method}`" end return to_enum(:each, start, agent, next_method: next_method) unless block_given? current = start while current current = agent.get(current) if current.is_a?(String) || current.is_a?(URI) scraper = self.new(current) yield scraper current = scraper.send(next_method) end end |