Sha256: 18535328397c540834871069e2df446f7f5bb4e64e28d20456c4d811da50cf09

Contents?: true

Size: 774 Bytes

Versions: 2

Compression:

Stored size: 774 Bytes

Contents

require 'page_by_page/version'
require 'page_by_page/url'
require 'nokogiri'
require 'open-uri'

class PageByPage

  class << self
    def fetch &block
      pbp = self.new &block
      pbp.fetch
    end
  end

  def initialize &block
    instance_eval &block
  end

  def url tmpl
    @url = Url.new tmpl
  end

  def selector sl
    @selector = sl
  end

  def fetch
    items, all_items = [nil], []
    catch :no_more do
      until items.empty?
        doc = parse @url.next
        items = doc.css @selector
        all_items << items
      end
    end
    all_items.flatten
  end

  private

  def parse url
    Nokogiri::HTML open url
  rescue OpenURI::HTTPError => e
    if e.message == '404 Not Found'
      throw :no_more
    else
      raise e
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
page_by_page-0.1.2 lib/page_by_page.rb
page_by_page-0.1.1 lib/page_by_page.rb