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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值