Class: HtmlEntry::Page::EntityFetcher
- Inherits:
-
Object
- Object
- HtmlEntry::Page::EntityFetcher
- 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
Instance Attribute Summary collapse
-
#instructions ⇒ Array
Get instructions.
Instance Method Summary collapse
-
#fetch(document:, plenty: false) ⇒ Hash, Array
Fetch data from document.
-
#fetch_plenty(document) ⇒ Hash
Fetch collection data from document.
-
#fetch_single(document) ⇒ Hash
Fetch single data from document.
-
#get_values_collector(document) ⇒ Page::ValuesCollector
Get value collector.
-
#initialize ⇒ EntityFetcher
constructor
Init.
Constructor Details
#initialize ⇒ EntityFetcher
Init
30 31 32 |
# File 'lib/html_entry/page/entity_fetcher.rb', line 30 def initialize @selector_cache ||= {} end |
Instance Attribute Details
#instructions ⇒ Array
Get instructions
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
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
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
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
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 |