Sha256: 5b68ac8c5e3c7769f44d02547a6a166178d994a81f42f1f5b5d814f3f8e6764c

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# Author::    TAC (tac@tac42.net)

require_relative 'yasuri_node'

module Yasuri
  class PaginateNode
    include Node

    def initialize(xpath, name, children = [], limit: nil, flatten: false)
      super(xpath, name, children)
      @flatten = flatten
      @limit = limit
    end

    def inject(agent, page, opt = {}, element = page)
      retry_count = opt[:retry_count] || 5

      raise NotImplementedError.new("PagenateNode inside StructNode, Not Supported") if page != element

      child_results = []
      limit = @limit.nil? ? Float::MAX : @limit
      while page
        child_results_kv = @children.map do |child_node|
          child_name = Yasuri.NodeName(child_node.name, opt)
          [child_name, child_node.inject(agent, page, opt)]
        end
        child_results << Hash[child_results_kv]

        link = page.search(@xpath).first
        break if link == nil

        link_button = Mechanize::Page::Link.new(link, agent, page)
        page = Yasuri.with_retry(retry_count) { link_button.click }
        break if (limit -= 1) <= 0
      end

      if @flatten == true
        return child_results.map{|h| h.values}.flatten
      end

      child_results
    end
    def opts
      {limit:@limit, flatten:@flatten}
    end

    def node_type_str
      "pages"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yasuri-3.1.0 lib/yasuri/yasuri_paginate_node.rb