如何向feedjira添加自定义解析器?

时间:2015-10-18 06:42:28

标签: ruby-on-rails feedjira

我有一个xml feed,格式如下:

<league country="england">
 <match status="15:00" time="15:00">
   <home name="Newcastle Utd"/>
   <away name="Norwich"/>
  </match>
    <match status="15:00" time="15:00">
    <home name="Newcastle Utd"/>
    <away name="Norwich"/>
    </match>
    <match status="19:00" time="19:00">
    <home name="Swansea"/>
    <away name="Stoke City"/>
    </match>
 </league>

如何在feedjira中添加自定义解析器并将其用于此Feed?感谢

1 个答案:

答案 0 :(得分:0)

require 'feedjira';
require './Custom.rb';
c = Feedjira::Feed.parse_with Custom::Custom, '<xml><title>TitlePArsed</title></xml>'
print c.title;

Custom.rb:

require 'feedjira';
module Custom
 # Parser for dealing with Atom feeds.
 class Custom
   include SAXMachine
   include Feedjira::FeedUtilities
   element :title

   def self.able_to_parse?(xml) #:nodoc:
      true
   end

   def url
     @url || (links - [feed_url]).last || links.last
   end

   def feed_url
     @feed_url ||= links.first
   end

   def self.preprocess(xml)
     Preprocessor.new(xml).to_xml
   end
 end
end