1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

rss: Add option Hash support to RSS::Parser.parse

Available options:

  * :validate
  * :ignore_unknown_element
  * :parser_class


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63407 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2018-05-12 09:06:00 +00:00
parent 961cbe5262
commit 97c07d3487
3 changed files with 81 additions and 2 deletions

5
NEWS
View file

@ -176,6 +176,11 @@ with all sufficient information, see the ChangeLog file or Redmine
* "name(#{NODE_SET})" case
* RSS
* RSS::Parser.parse: Accept options as Hash. :validate,
:ignore_unknown_element, :parser_class options are available.
* Set
* Aliased methods:

View file

@ -72,13 +72,31 @@ module RSS
end
end
def parse(rss, do_validate=true, ignore_unknown_element=true,
parser_class=default_parser)
def parse(rss, *args)
if args.last.is_a?(Hash)
options = args.pop
else
options = {}
end
do_validate = boolean_argument(args[0], options[:validate], true)
ignore_unknown_element =
boolean_argument(args[1], options[:ignore_unknown_element], true)
parser_class = args[2] || options[:parser_class] || default_parser
parser = new(rss, parser_class)
parser.do_validate = do_validate
parser.ignore_unknown_element = ignore_unknown_element
parser.parse
end
private
def boolean_argument(positioned_value, option_value, default)
value = positioned_value
if value.nil? and not option_value.nil?
value = option_value
end
value = default if value.nil?
value
end
end
def_delegators(:@parser, :parse, :rss,

View file

@ -61,5 +61,61 @@ EOR
EOR
end
end
def test_parse_option_validate_nil
assert_raise(RSS::MissingTagError) do
RSS::Parser.parse(make_RDF(<<-RDF), :validate => nil)
RDF
end
end
def test_parse_option_validate_true
assert_raise(RSS::MissingTagError) do
RSS::Parser.parse(make_RDF(<<-RDF), :validate => true)
RDF
end
end
def test_parse_option_validate_false
rdf = RSS::Parser.parse(make_RDF(<<-RDF), :validate => false)
RDF
assert_nil(rdf.channel)
end
def test_parse_option_ignore_unknown_element_nil
assert_nothing_raised do
RSS::Parser.parse(make_RDF(<<-RDF), :ignore_unknown_element => nil)
<unknown/>
#{make_channel}
#{make_item}
#{make_textinput}
#{make_image}
RDF
end
end
def test_parse_option_ignore_unknown_element_true
assert_nothing_raised do
RSS::Parser.parse(make_RDF(<<-RDF), :ignore_unknown_element => true)
<unknown/>
#{make_channel}
#{make_item}
#{make_textinput}
#{make_image}
RDF
end
end
def test_parse_option_ignore_unknown_element_false
assert_raise(RSS::NotExpectedTagError) do
RSS::Parser.parse(make_RDF(<<-RDF), :ignore_unknown_element => false)
<unknown/>
#{make_channel}
#{make_item}
#{make_textinput}
#{make_image}
RDF
end
end
end
end