module Yasuri::Node

Attributes

children[R]
name[R]
url[R]
xpath[R]

Public Class Methods

new(xpath, name, children = [], **_opt) click to toggle source
# File lib/yasuri/yasuri_node.rb, line 8
def initialize(xpath, name, children = [], **_opt)
  @xpath, @name, @children = xpath, name, children
end

Public Instance Methods

inject(agent, page, opt = {}, element = page) click to toggle source
# File lib/yasuri/yasuri_node.rb, line 25
def inject(agent, page, opt = {}, element = page)
  fail "#{Kernel.__method__} is not implemented in included class."
end
node_type_str() click to toggle source
# File lib/yasuri/yasuri_node.rb, line 49
def node_type_str
  fail "#{Kernel.__method__} is not implemented in included class."
end
opts() click to toggle source
# File lib/yasuri/yasuri_node.rb, line 45
def opts
  {}
end
scrape(uri, opt = {}) click to toggle source
# File lib/yasuri/yasuri_node.rb, line 12
def scrape(uri, opt = {})
  agent = Mechanize.new
  scrape_with_agent(uri, agent, opt)
end
scrape_with_agent(uri, agent, opt = {}) click to toggle source
# File lib/yasuri/yasuri_node.rb, line 17
def scrape_with_agent(uri, agent, opt = {})
  retry_count = opt[:retry_count] || Yasuri::DefaultRetryCount
  interval_ms = opt[:interval_ms] || Yasuri::DefaultInterval_ms

  page = Yasuri.with_retry(retry_count, interval_ms) { agent.get(uri) }
  inject(agent, page, opt)
end
to_h() click to toggle source
# File lib/yasuri/yasuri_node.rb, line 29
def to_h
  return @xpath if @xpath and @children.empty? and self.opts.values.compact.empty?

  node_hash = {}
  self.opts.each{|k, v| node_hash[k] = v if not v.nil?}

  node_hash[:path] = @xpath if @xpath

  children.each do |child|
    child_node_name = "#{child.node_type_str}_#{child.name}"
    node_hash[child_node_name] = child.to_h
  end

  node_hash
end