From be2de5287373d5a4c96fe02b9efcfe991726bee3 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 2 Jan 2025 15:20:36 -0500 Subject: [PATCH] quash Ruby 3.4 URI::Parser warnings by using URI::RFC23996_PARSER directly if it exists. --- CHANGELOG.md | 1 + lib/mechanize/util.rb | 30 ++++++++++++++---------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbdef6b5..7da19fce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## next / unreleased * Quash frozen string warnings in Ruby 3.4. (#661) @simpl1g +* Quash URI::Parser warnings in Ruby 3.4. (#662) @flavorjones ## 2.12.2 / 2023-10-02 diff --git a/lib/mechanize/util.rb b/lib/mechanize/util.rb index 5d1e682f..11fc2338 100644 --- a/lib/mechanize/util.rb +++ b/lib/mechanize/util.rb @@ -134,29 +134,27 @@ def self.detect_charset(src) end def self.uri_escape str, unsafe = nil - @parser ||= begin - URI::Parser.new - rescue NameError - URI - end - - if URI == @parser then + if URI == parser then unsafe ||= URI::UNSAFE else - unsafe ||= @parser.regexp[:UNSAFE] + unsafe ||= parser.regexp[:UNSAFE] end - @parser.escape str, unsafe + parser.escape str, unsafe end def self.uri_unescape str - @parser ||= begin - URI::Parser.new - rescue NameError - URI - end - - @parser.unescape str + parser.unescape str end + def self.parser + @parser ||= + if defined?(URI::RFC2396_PARSER) + URI::RFC2396_PARSER + elsif defined?(URI::Parser) + URI::Parser.new + else + URI + end + end end