`

feedtools解析atom的超时问题

阅读更多
feedtools是一个用来解析rss和atom的gem包
安装如下
gem install feedtools

首页说明上提供了简单的使用说明

  require 'feed_tools'
  slashdot_feed = FeedTools::Feed.open('http://www.slashdot.org/index.rss')
  slashdot_feed.title

这里会遇到超时问题,我要设定open的这个链接超时时间,并在自定的时间后做处理的话,就无法满足了。
  源代码中的open方法是这么写的

def Feed.open(href, options={})
      FeedTools::GenericHelper.validate_options(
        FeedTools.configurations.keys, options.keys)

      # clean up the url
      href = FeedTools::UriHelper.normalize_url(href)

      feed_configurations = FeedTools.configurations.merge(options)
      cache_object = nil
      deserialized_feed = nil
.....
其他代码省略


FeedTools.configurations.merge(options)做的操作时把options的值merge到configurations中去,因此设定超时等操作的参数必定在这个configurations中

在源代码的load_configurations方法中可以看到要设置的options,其中:http_timeout => nil,可以设定自定义的时间,试了一下,单位是秒

def FeedTools.load_configurations
    if @configurations.blank?
      # TODO: Load this from a config file.
      config_hash = {}
      @configurations = {
        :feed_cache => nil,
        :disable_update_from_remote => false,
        :proxy_address => nil,
        :proxy_port => nil,
        :proxy_user => nil,
        :proxy_password => nil,
        :auth_user => nil,
        :auth_password => nil,
        :auth_scheme => nil,
        :http_timeout => nil,
        :user_agent =>
          "FeedTools/#{FeedTools::FEED_TOOLS_VERSION::STRING} " + 
          "+http://www.sporkmonger.com/projects/feedtools/",
        :generator_name =>
          "FeedTools/#{FeedTools::FEED_TOOLS_VERSION::STRING}",
        :generator_href =>
          "http://www.sporkmonger.com/projects/feedtools/",
        :tidy_enabled => false,
        :tidy_options => {},
        :lazy_parsing_enabled => true,
        :serialization_enabled => false,
        :idn_enabled => true,
        :sanitization_enabled => true,
        :sanitize_with_nofollow => true,
        :always_strip_wrapper_elements => true,
        :timestamp_estimation_enabled => true,
        :url_normalization_enabled => true,
        :entry_sorting_property => "time",
        :strip_comment_count => false,
        :tab_spaces => 2,
        :max_ttl => 3.days.to_s,
        :default_ttl => 1.hour.to_s,
        :output_encoding => "utf-8"
      }.merge(config_hash)
    end
    return @configurations
  end


最后,如果设置超时时间是10秒的话,就这么写

slashdot_feed = FeedTools::Feed.open('http://www.slashdot.org/index.rss',:http_timeout => 10)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics