Class: RichUrls::BodyDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/body_decorator.rb

Constant Summary collapse

NoXMLError =
Class.new(StandardError)
PARSERS =
{
  'title' => Parsers::Property,
  'description' => Parsers::Property,
  'image' => Parsers::Url,
  'favicon' => Parsers::Url,
  'provider_display' => Parsers::ProviderDisplayParser,
  'embed' => Parsers::EmbedParser
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, body, filter) ⇒ BodyDecorator

Returns a new instance of BodyDecorator.



29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Class Method Details

.decorate(url, body, filter = []) ⇒ Object



23
24
25
# File 'lib/body_decorator.rb', line 23

def self.decorate(url, body, filter = [])
  new(url, body, filter).decorate
end

Instance Method Details

#decorateObject



43
44
45
46
47
# 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