ruby-changes:51200
From: kou <ko1@a...>
Date: Sat, 12 May 2018 18:06:07 +0900 (JST)
Subject: [ruby-changes:51200] kou:r63407 (trunk): rss: Add option Hash support to RSS::Parser.parse
kou 2018-05-12 18:06:00 +0900 (Sat, 12 May 2018) New Revision: 63407 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63407 Log: rss: Add option Hash support to RSS::Parser.parse Available options: * :validate * :ignore_unknown_element * :parser_class Modified files: trunk/NEWS trunk/lib/rss/parser.rb trunk/test/rss/test_parser.rb Index: NEWS =================================================================== --- NEWS (revision 63406) +++ NEWS (revision 63407) @@ -176,6 +176,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L176 * "name(#{NODE_SET})" case +* RSS + + * RSS::Parser.parse: Accept options as Hash. :validate, + :ignore_unknown_element, :parser_class options are available. + * Set * Aliased methods: Index: lib/rss/parser.rb =================================================================== --- lib/rss/parser.rb (revision 63406) +++ lib/rss/parser.rb (revision 63407) @@ -72,13 +72,31 @@ module RSS https://github.com/ruby/ruby/blob/trunk/lib/rss/parser.rb#L72 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, Index: test/rss/test_parser.rb =================================================================== --- test/rss/test_parser.rb (revision 63406) +++ test/rss/test_parser.rb (revision 63407) @@ -61,5 +61,61 @@ EOR https://github.com/ruby/ruby/blob/trunk/test/rss/test_parser.rb#L61 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/