Commit 6ae03d5512d12c3121af8f5598aea959c3674c0c
Committed by
Antonio Terceiro
1 parent
c35f9d57
Exists in
master
and in
23 other branches
ActionItem1215: user agent noosfero to fetch feeds
Showing
2 changed files
with
22 additions
and
1 deletions
Show diff stats
lib/feed_handler.rb
| ... | ... | @@ -15,7 +15,12 @@ class FeedHandler |
| 15 | 15 | def fetch(address) |
| 16 | 16 | begin |
| 17 | 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 | 24 | return content |
| 20 | 25 | rescue Exception => ex |
| 21 | 26 | raise FeedHandler::FetchError, ex.to_s |
| ... | ... | @@ -39,4 +44,14 @@ class FeedHandler |
| 39 | 44 | class ParseError < Exception; end |
| 40 | 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 | 57 | end | ... | ... |
test/unit/feed_handler_test.rb
| ... | ... | @@ -73,4 +73,10 @@ class FeedHandlerTest < Test::Unit::TestCase |
| 73 | 73 | handler.process(container) |
| 74 | 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 | 82 | end | ... | ... |