Sha256: 3dddbe8101906fc0b804b4f48fc5265eaa5e176b8fb4a49030ac6c18353fcc63

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Tokamak
  module Representation
    module Atom
      # Create a new Representation::Atom object using a +string_or_io+
      # object.
      #
      # Examples
      #   xml  = IO.read("spec/units/lib/atoms/full_atom.xml")
      #   atom = Tokamak::Representation::Atom.new(xml)
      class Factory
        # RelaxNG file to validate atom
        RELAXNG_ATOM = File.join(File.dirname(__FILE__), 'atom.rng')

        if Nokogiri::VERSION_INFO["libxml"]["loaded"] < "2.7.7"
          puts "WARNING! In order to use schema validation on atom representations you need libxml version 2.7.7 or superior loaded in your system." 
          SCHEMA       = nil
        else
          SCHEMA       = ::Nokogiri::XML::RelaxNG(File.open(RELAXNG_ATOM))
        end
  
        def self.create(string_or_io)
          doc = string_or_io.kind_of?(Nokogiri::XML::Document) ? string_or_io : Nokogiri::XML(string_or_io) 
        
          if SCHEMA && !(errors = SCHEMA.validate(doc)).empty?
            raise AtomInvalid.new("Invalid Atom: "+ errors.join(", "))
          end
          
          representation_for doc
        
        end      
        
        private
        def self.representation_for(doc)
          case doc.root.name
          when "feed"
            Tokamak::Representation::Atom::Feed.new(doc)
          when "entry"
            Tokamak::Representation::Atom::Entry.new(doc)
          end        
        end
        
      end
    
      class AtomInvalid < StandardError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tokamak-1.0.0.beta2 lib/tokamak/representation/atom/factory.rb