Skip to content

Replacing deprecated URI::encode with URI.encode_www_form #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"

gem "httparty"
gem "json"
gem "json", ">= 2.6.3"

group :test do
gem "rake"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -15,7 +15,7 @@ PLATFORMS
DEPENDENCIES
fakeweb
httparty
json
json (>= 2.6.3)
minitest
rake

Expand Down
17 changes: 11 additions & 6 deletions lib/woocommerce_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/woocommerce_api/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/fakeweb_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
unless File.respond_to?(:exists?)
class << File
alias_method :exists?, :exist?
end
end
6 changes: 4 additions & 2 deletions test/test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "minitest/autorun"
require_relative "fakeweb_patch"
require "fakeweb"
require "json"
require "woocommerce_api"
Expand Down Expand Up @@ -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
Expand Down