Commit 6ae03d5512d12c3121af8f5598aea959c3674c0c

Authored by Daniela Feitosa
Committed by Antonio Terceiro
1 parent c35f9d57

ActionItem1215: user agent noosfero to fetch feeds

lib/feed_handler.rb
@@ -15,7 +15,12 @@ class FeedHandler @@ -15,7 +15,12 @@ class FeedHandler
15 def fetch(address) 15 def fetch(address)
16 begin 16 begin
17 content = "" 17 content = ""
18 - open(address) do |s| content = s.read end 18 + block = lambda { |s| content = s.read }
  19 + content = if is_web_address?(address)
  20 + open( address, "User-Agent" => "Noosfero/#{Noosfero::VERSION}", &block )
  21 + else
  22 + open_uri_original_open(address, &block)
  23 + end
19 return content 24 return content
20 rescue Exception => ex 25 rescue Exception => ex
21 raise FeedHandler::FetchError, ex.to_s 26 raise FeedHandler::FetchError, ex.to_s
@@ -39,4 +44,14 @@ class FeedHandler @@ -39,4 +44,14 @@ class FeedHandler
39 class ParseError < Exception; end 44 class ParseError < Exception; end
40 class FetchError < Exception; end 45 class FetchError < Exception; end
41 46
  47 + protected
  48 +
  49 + # extracted from the open implementation in the open-uri library
  50 + def is_web_address?(address)
  51 + address.respond_to?(:open) ||
  52 + address.respond_to?(:to_str) &&
  53 + (%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ address) &&
  54 + URI.parse(address).respond_to?(:open)
  55 + end
  56 +
42 end 57 end
test/unit/feed_handler_test.rb
@@ -73,4 +73,10 @@ class FeedHandlerTest &lt; Test::Unit::TestCase @@ -73,4 +73,10 @@ class FeedHandlerTest &lt; Test::Unit::TestCase
73 handler.process(container) 73 handler.process(container)
74 end 74 end
75 75
  76 + should 'identifies itself as noosfero user agent' do
  77 + handler = FeedHandler.new
  78 + handler.expects(:open).with('http://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}"}, anything).returns('bli content')
  79 + assert_equal 'bli content', handler.fetch('http://site.org/feed.xml')
  80 + end
  81 +
76 end 82 end