class Flexparser::TagParser
A parser for a single tag.
Attributes
options[RW]
xpaths[RW]
Public Class Methods
new(tags, **opts)
click to toggle source
@param name [Symbol] the name used for the accessor
defined on the parent.
@param xpath [String] an xpath string used to access a Nokogiri-Fragment
# File lib/flexparser/tag_parser.rb, line 15 def initialize(tags, **opts) @xpaths = XPaths.new(tags) @options = opts end
Public Instance Methods
name()
click to toggle source
# File lib/flexparser/tag_parser.rb, line 31 def name options[:name] || xpaths.method_name end
parse(doc)
click to toggle source
@param doc [Nokogiri::Node] a node that can be accessed through xpath @return a String if no type was specified, otherwise the type
will try to parse the string using ::parse
# File lib/flexparser/tag_parser.rb, line 25 def parse(doc) result = content(doc) || return return options[:sub_parser].parse(result) if sub_parser type ? transform(result.text) : result.text end
Protected Instance Methods
content(doc)
click to toggle source
# File lib/flexparser/tag_parser.rb, line 51 def content(doc) xpaths.valid_paths(doc).each do |path| set = doc.xpath("(#{path})[1]") return set unless set.empty? end return unless options[:required] raise_required_error(doc) end
raise_required_error(doc)
click to toggle source
# File lib/flexparser/tag_parser.rb, line 60 def raise_required_error(doc) raise(RequiredMissingError.new(self), "Required field #{name} not found in #{doc}") end
sub_parser()
click to toggle source
# File lib/flexparser/tag_parser.rb, line 37 def sub_parser options[:sub_parser] end
transform(string)
click to toggle source
# File lib/flexparser/tag_parser.rb, line 45 def transform(string) return options[:type].parse string if options[:type] return string.public_send(options[:transform]) if options[:transform] string end
type()
click to toggle source
# File lib/flexparser/tag_parser.rb, line 41 def type options[:type] || options[:transform] end