Class: Juknife::Scraping::DSL::Items

Inherits:
Object
  • Object
show all
Includes:
Juknife::Scraping::DSL
Defined in:
lib/juknife/scraping/dsl/items.rb

Overview

A DSL node in the tree that scrapes elements.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Juknife::Scraping::DSL

#children, #item, #items, #scope

Constructor Details

#initialize(name, selector, type = :string, *args, &block) ⇒ Items

Returns a new instance of Items.



15
16
17
18
19
20
21
22
23
# File 'lib/juknife/scraping/dsl/items.rb', line 15

def initialize(name, selector, type = :string, *args, &block)
  @name = name
  @selector = selector
  @type = type
  @args = args

  return unless block
  instance_eval(&block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/juknife/scraping/dsl/items.rb', line 13

def name
  @name
end

#selectorObject (readonly)

Returns the value of attribute selector.



13
14
15
# File 'lib/juknife/scraping/dsl/items.rb', line 13

def selector
  @selector
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/juknife/scraping/dsl/items.rb', line 13

def type
  @type
end

Instance Method Details

#visit(context) ⇒ Object

rubocop:disable Metrics/AbcSize



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/juknife/scraping/dsl/items.rb', line 25

def visit(context) # rubocop:disable Metrics/AbcSize
  result = []

  context.find_all(selector).each do |ele_chlid|
    child_context = Context.new(ele_chlid, {})

    children.each do |child|
      child.visit(child_context)
    end

    result << child_context.result
  end

  context.result[name] =
    result.reject { |hash| hash.values.compact.all?(&:empty?) }
end