class HtmlEntry::Page::ValuesCollector
This class responsible for getting values according to an instruction
@see tests/html_entry/page/test_entity_fetcher.rb
Attributes
Public Class Methods
# File lib/html_entry/page/values_collector.rb, line 28 def initialize(options = {}) @options = options @data = {} end
Public Instance Methods
Fetch value of element
@param [Symbol] name @param [Hash] instruction @param [Nokogiri::XML::Element] node @return [String, Nokogiri::XML::Element]
# File lib/html_entry/page/values_collector.rb, line 41 def fetch(name, instruction, node) if node && (instruction[:type] == :attribute) value = get_node_attribute( node, instruction ) elsif instruction[:type] == :function value = call_function(name, instruction) elsif instruction[:type] == :boolean || instruction[:type] == :bool value = !!node elsif node && instruction[:type] == :children value = children( name: name, instruction: instruction, node: node, plenty: if instruction[:children_plenty].nil? true else instruction[:children_plenty] end ) elsif node && (instruction[:type] == :value || instruction[:type].nil?) # empty type should be determined as :value value = node elsif instruction.is_a?(Hash) && !instruction[:default].nil? value = instruction[:default] elsif node.nil? value = nil else raise HtmlEntry::Error, 'Unknown instruction type or XML/HTML value not found.' end value = filter_value(value, instruction) if data[name].instance_of?(Array) && value.instance_of?(Array) data[name] = [data[name], value].flatten else unless data[name].nil? && (!instruction[:overwrite]) raise "Value already set for data key name '#{name}'." end data[name] = value end data[name] end
Protected Instance Methods
Call custom function
@param [Hash] instruction @return [*]
# File lib/html_entry/page/values_collector.rb, line 163 def call_function(name, instruction) if instruction[:function].instance_of? Proc instruction[:function].call name, instruction, data, @options else HtmlEntry::Error.new ':function is not instance of Proc' end end
Fetch value of element
@param [Symbol] name @param [Hash] instruction @param [Nokogiri::XML::Element] node @return [Hash, Array]
# File lib/html_entry/page/values_collector.rb, line 96 def children(instruction:, node:, plenty: nil, name: nil) instruction = if instruction[:instructions] == :the_same @options[:instructions] else instruction[:instructions] end fetcher = Page::EntityFetcher.new fetcher.instructions = instruction fetcher.fetch document: node, plenty: plenty.nil? ? true : plenty end
Filter fetched value
@param [Nokogiri::XML::Element] value @param [Symbol] filter @return [String, Nokogiri::XML::Element]
# File lib/html_entry/page/values_collector.rb, line 127 def filter(value, filter = nil) # return as is, :filter can be omitted in instruction return value if filter == :element # return text with tags return value.to_s.strip if filter == :node_text # return text without tags by default value = value.text if value.instance_of?(Nokogiri::XML::Element) # return integer return value.to_i if filter == :to_i # return non-stripped text return value.to_s if filter == :no_strip return value.strip if value.is_a? String value end
Filter value
@param [Nokogiri::XML::Element] value @param [Hash] instruction @return [String, Nokogiri::XML::Element]
# File lib/html_entry/page/values_collector.rb, line 116 def filter_value(value, instruction) filter(value, instruction[:filter]) end
@param [Nokogiri::XML::Element] node @param [Hash] instruction @return [String]
# File lib/html_entry/page/values_collector.rb, line 153 def get_node_attribute(node, instruction) node[instruction[:attribute]] end