File: nokogiri.rb

package info (click to toggle)
ruby-oembed 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,468 kB
  • sloc: ruby: 2,351; makefile: 3
file content (41 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Only allow this backend the nokogiri gem is already loaded
raise ::LoadError, "The nokogiri library isn't available. require 'nokogiri'" unless defined?(Nokogiri)

module OEmbed
  module Formatter
    module XML
      module Backends
        # Use the nokogiri gem to parse XML values.
        module Nokogiri
          extend self

          # Parses an XML string or IO and convert it into an object.
          def decode(xml)
            obj = {}
            doc = ::Nokogiri::XML(xml) { |config| config.strict }
            doc.root.elements.each do |el|
              obj[el.name] = el.text
            end
            obj
          rescue
            case $!
            when parse_error
              raise $!
            else
              raise parse_error, "Couldn't parse the given document."
            end  
          end
          
          def decode_fail_msg
            "The version of the nokogiri library you have installed isn't parsing XML like ruby-oembed expected."
          end
          
          def parse_error
            ::Nokogiri::XML::SyntaxError
          end
        
        end
      end
    end
  end
end