From e083fcf74cbb5141b70f09654675c82df1538dde Mon Sep 17 00:00:00 2001 From: Michael Koper Date: Tue, 25 Feb 2025 17:13:42 +0100 Subject: [PATCH] Replacing deprecated URI::encode with URI.encode_www_form --- Gemfile | 2 +- Gemfile.lock | 4 ++-- lib/woocommerce_api.rb | 17 +++++++++++------ lib/woocommerce_api/oauth.rb | 2 +- test/fakeweb_patch.rb | 5 +++++ test/test.rb | 6 ++++-- 6 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 test/fakeweb_patch.rb diff --git a/Gemfile b/Gemfile index 9c451ee..33783d6 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source "https://rubygems.org" gem "httparty" -gem "json" +gem "json", ">= 2.6.3" group :test do gem "rake" diff --git a/Gemfile.lock b/Gemfile.lock index b42e040..ba623b0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,7 +4,7 @@ GEM fakeweb (1.3.0) httparty (0.14.0) multi_xml (>= 0.5.2) - json (2.0.2) + json (2.6.3) minitest (5.10.1) multi_xml (0.6.0) rake (12.0.0) @@ -15,7 +15,7 @@ PLATFORMS DEPENDENCIES fakeweb httparty - json + json (>= 2.6.3) minitest rake diff --git a/lib/woocommerce_api.rb b/lib/woocommerce_api.rb index 4ef29b2..f91c0c0 100644 --- a/lib/woocommerce_api.rb +++ b/lib/woocommerce_api.rb @@ -92,12 +92,17 @@ def options endpoint # data - A hash of data to flatten and append # # Returns an endpoint string with the data appended - def add_query_params endpoint, data + def add_query_params(endpoint, data) return endpoint if data.nil? || data.empty? endpoint += "?" unless endpoint.include? "?" endpoint += "&" unless endpoint.end_with? "?" - endpoint + URI.encode(flatten_hash(data).join("&")) + + flattened_params = flatten_hash(data) + + query_string = URI.encode_www_form(flattened_params) + + endpoint + query_string end # Internal: Get URL for requests @@ -184,17 +189,17 @@ def oauth_url url, method # hash - A hash to flatten # # Returns an array full of key value paired strings - def flatten_hash hash + def flatten_hash(hash) hash.flat_map do |key, value| case value when Hash value.map do |inner_key, inner_value| - "#{key}[#{inner_key}]=#{inner_value}" + ["#{key}[#{inner_key}]", inner_value] end when Array - value.map { |inner_value| "#{key}[]=#{inner_value}" } + value.map { |inner_value| ["#{key}[]", inner_value] } else - "#{key}=#{value}" + [[key, value]] end end end diff --git a/lib/woocommerce_api/oauth.rb b/lib/woocommerce_api/oauth.rb index c5bc44e..3df7c66 100644 --- a/lib/woocommerce_api/oauth.rb +++ b/lib/woocommerce_api/oauth.rb @@ -42,7 +42,7 @@ def get_oauth_url params["oauth_timestamp"] = Time.new.to_i params["oauth_signature"] = CGI::escape(generate_oauth_signature(params, url)) - query_string = URI::encode(params.map{|key, value| "#{key}=#{value}"}.join("&")) + query_string = URI.encode_www_form(params) "#{url}?#{query_string}" end diff --git a/test/fakeweb_patch.rb b/test/fakeweb_patch.rb new file mode 100644 index 0000000..ac41d54 --- /dev/null +++ b/test/fakeweb_patch.rb @@ -0,0 +1,5 @@ +unless File.respond_to?(:exists?) + class << File + alias_method :exists?, :exist? + end +end diff --git a/test/test.rb b/test/test.rb index 58f25f8..65b0279 100644 --- a/test/test.rb +++ b/test/test.rb @@ -1,4 +1,5 @@ require "minitest/autorun" +require_relative "fakeweb_patch" require "fakeweb" require "json" require "woocommerce_api" @@ -155,11 +156,12 @@ def test_oauth_put def test_adding_query_params url = @oauth.send(:add_query_params, 'foo.com', filter: { sku: '123' }, order: 'created_at') - assert_equal url, URI.encode('foo.com?filter[sku]=123&order=created_at') + expected = 'foo.com?filter%5Bsku%5D=123&order=created_at' + assert_equal expected, url end def test_invalid_signature_method - assert_raises WooCommerce::OAuth::InvalidSignatureMethodError do + assert_raises WooCommerce::OAuth::InvalidSignatureMethodError do client = WooCommerce::API.new("http://dev.test/", "user", "pass", signature_method: 'GARBAGE') client.get 'products' end