class RichUrls::BodyDecorator

Constants

NoXMLError
PARSERS

Public Class Methods

decorate(url, body, filter = []) click to toggle source
# File lib/body_decorator.rb, line 23
def self.decorate(url, body, filter = [])
  new(url, body, filter).decorate
end
new(url, body, filter) click to toggle source
# File lib/body_decorator.rb, line 29
def initialize(url, body, filter)
  @url = url
  @filter = filter
  @xml = XMLHandler.new(filter)

  Ox.sax_html(@xml, StringIO.new(body))

  unless @xml.elements.any?
    raise NoXMLError,
          'document is not proper XML'
  end
rescue XMLHandler::StopParsingError
end

Public Instance Methods

decorate() click to toggle source
# File lib/body_decorator.rb, line 43
def decorate
  parsers.each_with_object({}) do |(key, parser), object|
    object[key] = parser.call(@xml.properties[key], @url)
  end
end

Private Instance Methods

parsers() click to toggle source
# File lib/body_decorator.rb, line 51
def parsers
  return PARSERS if @filter.empty?

  PARSERS.slice(*@filter)
end