mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/rss/parser.rb (RSS::Parser#initialize): accept HTTP/FTP
URI and local file path too. * test/rss/test_parser.rb (RSS::TestParser#test_parse): test for the above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8819 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
17743ce0a8
commit
be7999fd3d
3 changed files with 64 additions and 2 deletions
|
@ -1,3 +1,11 @@
|
|||
Fri Jul 22 14:37:43 2005 Kouhei Sutou <kou@cozmixng.org>
|
||||
|
||||
* lib/rss/parser.rb (RSS::Parser#initialize): accept HTTP/FTP
|
||||
URI and local file path too.
|
||||
|
||||
* test/rss/test_parser.rb (RSS::TestParser#test_parse): test
|
||||
for the above.
|
||||
|
||||
Fri Jul 22 07:01:42 2005 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
|
||||
|
||||
* ext/tk/tkutil/tkutil.c (tk_conv_args): forget to revert
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "forwardable"
|
||||
require "open-uri"
|
||||
|
||||
require "rss/rss"
|
||||
|
||||
|
@ -77,7 +78,36 @@ module RSS
|
|||
:do_validate=)
|
||||
|
||||
def initialize(rss, parser_class=self.class.default_parser)
|
||||
@parser = parser_class.new(rss)
|
||||
@parser = parser_class.new(normalize_rss(rss))
|
||||
end
|
||||
|
||||
private
|
||||
def normalize_rss(rss)
|
||||
return rss if maybe_xml?(rss)
|
||||
|
||||
uri = to_uri(rss)
|
||||
|
||||
if uri.respond_to?(:read)
|
||||
uri.read
|
||||
elsif !rss.tainted? and File.readable?(rss)
|
||||
File.open(rss) {|f| f.read}
|
||||
else
|
||||
rss
|
||||
end
|
||||
end
|
||||
|
||||
def maybe_xml?(source)
|
||||
source.is_a?(String) and /</ =~ source
|
||||
end
|
||||
|
||||
def to_uri(rss)
|
||||
return rss if rss.is_a?(::URI::Generic)
|
||||
|
||||
begin
|
||||
URI(rss)
|
||||
rescue ::URI::Error
|
||||
rss
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require "fileutils"
|
||||
|
||||
require "rss-testcase"
|
||||
|
||||
require "rss/1.0"
|
||||
|
@ -9,10 +11,19 @@ module RSS
|
|||
|
||||
def setup
|
||||
@_default_parser = Parser.default_parser
|
||||
@rss10 = make_RDF(<<-EOR)
|
||||
#{make_channel}
|
||||
#{make_item}
|
||||
#{make_textinput}
|
||||
#{make_image}
|
||||
EOR
|
||||
@rss_file = "rss10.rdf"
|
||||
File.open(@rss_file, "w") {|f| f.print(@rss10)}
|
||||
end
|
||||
|
||||
def teardown
|
||||
Parser.default_parser = @_default_parser
|
||||
FileUtils.rm_f(@rss_file)
|
||||
end
|
||||
|
||||
def test_RDF
|
||||
|
@ -585,7 +596,7 @@ EOR
|
|||
end
|
||||
|
||||
def test_default_parser
|
||||
assert_nothing_raised() do
|
||||
assert_nothing_raised do
|
||||
Parser.default_parser = RSS::AVAILABLE_PARSERS.first
|
||||
end
|
||||
|
||||
|
@ -594,6 +605,19 @@ EOR
|
|||
end
|
||||
end
|
||||
|
||||
def test_parse
|
||||
assert_not_nil(RSS::Parser.parse(@rss_file))
|
||||
|
||||
garbage_rss_file = @rss_file + "-garbage"
|
||||
if RSS::Parser.default_parser.name == "RSS::XMLParserParser"
|
||||
assert_raise(RSS::NotWellFormedError) do
|
||||
RSS::Parser.parse(garbage_rss_file)
|
||||
end
|
||||
else
|
||||
assert_nil(RSS::Parser.parse(garbage_rss_file))
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue