Class: HtmlEntry::Page::EntityFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/html_entry/page/entity_fetcher.rb

Overview

This entity-html_entry class designed for reading data from HTML/XML block according to instructions

See Also:

  • tests/html_entry/page/test_entity_fetchertests/html_entry/page/test_entity_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntityFetcher

Init



30
31
32
# File 'lib/html_entry/page/entity_fetcher.rb', line 30

def initialize
  @selector_cache ||= {}
end

Instance Attribute Details

#instructionsArray

Get instructions

Returns:

  • (Array)


25
26
27
# File 'lib/html_entry/page/entity_fetcher.rb', line 25

def instructions
  @instructions
end

Instance Method Details

#fetch(document:, plenty: false) ⇒ Hash, Array

Fetch data from document

Parameters:

  • document (Nokogiri::HTML::Document, Nokogiri::XML::Element)
  • plenty (TrueClass, FalseClass) (defaults to: false)

    Get plenty of elements or the only one

Returns:

  • (Hash, Array)


84
85
86
87
88
89
90
# File 'lib/html_entry/page/entity_fetcher.rb', line 84

def fetch(document:, plenty: false)
  if plenty
    fetch_plenty(document)
  else
    fetch_single(document)
  end
end

#fetch_plenty(document) ⇒ Hash

Fetch collection data from document

Parameters:

  • document (Nokogiri::HTML::Document, Nokogiri::XML::Element)

Returns:

  • (Hash)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/html_entry/page/entity_fetcher.rb', line 129

def fetch_plenty(document)
  unless instructions.instance_of? Array
    raise 'Instructions must be an array.'
  end

  collectors, data = process_instructions(document)

  collectors.each do |_i, collector|
    # @type [HtmlEntry::Page::ValuesCollector] collector
    data.push collector.data
  end

  data
end

#fetch_single(document) ⇒ Hash

Fetch single data from document

Parameters:

  • document (Nokogiri::HTML::Document, Nokogiri::XML::Element)

Returns:

  • (Hash)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/html_entry/page/entity_fetcher.rb', line 98

def fetch_single(document)
  collector = get_values_collector(document)

  instructions.each do |instruction|
    node = Page.fetch_node(document, instruction)

    next unless instruction[:data]
    instruction[:data].each do |name, data_instruction|
      collector.fetch name, data_instruction, node
    end
  end

  collector.data
end

#get_values_collector(document) ⇒ Page::ValuesCollector

Get value collector

Parameters:

  • document (Nokogiri::HTML::Document, Nokogiri::XML::Element)

Returns:



119
120
121
122
# File 'lib/html_entry/page/entity_fetcher.rb', line 119

def get_values_collector(document)
  Page::ValuesCollector.new document:     document,
                            instructions: instructions
end