From 613e245e12e466da89144200ddc4331eb8048073 Mon Sep 17 00:00:00 2001 From: Hayden Date: Tue, 8 Oct 2024 20:23:08 +0700 Subject: [PATCH] fix: use 'uri' to percent-encode string - explicitly require 'uri'. resolve #34 - uri is a lighter library compared to erb --- lib/typesense.rb | 1 + lib/typesense/alias.rb | 2 +- lib/typesense/aliases.rb | 2 +- lib/typesense/analytics_rule.rb | 2 +- lib/typesense/analytics_rules.rb | 2 +- lib/typesense/collection.rb | 2 +- lib/typesense/document.rb | 2 +- lib/typesense/documents.rb | 2 +- lib/typesense/key.rb | 2 +- lib/typesense/override.rb | 2 +- lib/typesense/overrides.rb | 2 +- lib/typesense/preset.rb | 2 +- lib/typesense/presets.rb | 2 +- lib/typesense/synonym.rb | 2 +- lib/typesense/synonyms.rb | 2 +- spec/typesense/alias_spec.rb | 2 +- 16 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/typesense.rb b/lib/typesense.rb index ed07c77..bfa51a6 100644 --- a/lib/typesense.rb +++ b/lib/typesense.rb @@ -3,6 +3,7 @@ module Typesense end +require 'uri' require_relative 'typesense/version' require_relative 'typesense/configuration' require_relative 'typesense/client' diff --git a/lib/typesense/alias.rb b/lib/typesense/alias.rb index 63a1f56..5f238e4 100644 --- a/lib/typesense/alias.rb +++ b/lib/typesense/alias.rb @@ -18,7 +18,7 @@ def delete private def endpoint_path - "#{Aliases::RESOURCE_PATH}/#{ERB::Util.url_encode(@name)}" + "#{Aliases::RESOURCE_PATH}/#{URI.encode_www_form_component(@name)}" end end end diff --git a/lib/typesense/aliases.rb b/lib/typesense/aliases.rb index 277de2c..28caddb 100644 --- a/lib/typesense/aliases.rb +++ b/lib/typesense/aliases.rb @@ -24,7 +24,7 @@ def [](alias_name) private def endpoint_path(alias_name) - "#{Aliases::RESOURCE_PATH}/#{ERB::Util.url_encode(alias_name)}" + "#{Aliases::RESOURCE_PATH}/#{URI.encode_www_form_component(alias_name)}" end end end diff --git a/lib/typesense/analytics_rule.rb b/lib/typesense/analytics_rule.rb index 22c77b7..5f1f501 100644 --- a/lib/typesense/analytics_rule.rb +++ b/lib/typesense/analytics_rule.rb @@ -18,7 +18,7 @@ def delete private def endpoint_path - "#{AnalyticsRules::RESOURCE_PATH}/#{ERB::Util.url_encode(@rule_name)}" + "#{AnalyticsRules::RESOURCE_PATH}/#{URI.encode_www_form_component(@rule_name)}" end end end diff --git a/lib/typesense/analytics_rules.rb b/lib/typesense/analytics_rules.rb index da6b3db..45e65d8 100644 --- a/lib/typesense/analytics_rules.rb +++ b/lib/typesense/analytics_rules.rb @@ -24,7 +24,7 @@ def [](rule_name) private def endpoint_path(operation = nil) - "#{AnalyticsRules::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}" + "#{AnalyticsRules::RESOURCE_PATH}#{operation.nil? ? '' : "/#{URI.encode_www_form_component(operation)}"}" end end end diff --git a/lib/typesense/collection.rb b/lib/typesense/collection.rb index 8b61397..1217a41 100644 --- a/lib/typesense/collection.rb +++ b/lib/typesense/collection.rb @@ -27,7 +27,7 @@ def delete private def endpoint_path - "#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@name)}" + "#{Collections::RESOURCE_PATH}/#{URI.encode_www_form_component(@name)}" end end end diff --git a/lib/typesense/document.rb b/lib/typesense/document.rb index 1375988..4662d21 100644 --- a/lib/typesense/document.rb +++ b/lib/typesense/document.rb @@ -23,7 +23,7 @@ def update(partial_document, options = {}) private def endpoint_path - "#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Documents::RESOURCE_PATH}/#{ERB::Util.url_encode(@document_id)}" + "#{Collections::RESOURCE_PATH}/#{URI.encode_www_form_component(@collection_name)}#{Documents::RESOURCE_PATH}/#{URI.encode_www_form_component(@document_id)}" end end end diff --git a/lib/typesense/documents.rb b/lib/typesense/documents.rb index f8a6b62..46c3911 100644 --- a/lib/typesense/documents.rb +++ b/lib/typesense/documents.rb @@ -75,7 +75,7 @@ def delete(query_parameters = {}) private def endpoint_path(operation = nil) - "#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}" + "#{Collections::RESOURCE_PATH}/#{URI.encode_www_form_component(@collection_name)}#{Documents::RESOURCE_PATH}#{operation.nil? ? '' : "/#{operation}"}" end end end diff --git a/lib/typesense/key.rb b/lib/typesense/key.rb index bfaa6a3..e88df53 100644 --- a/lib/typesense/key.rb +++ b/lib/typesense/key.rb @@ -18,7 +18,7 @@ def delete private def endpoint_path - "#{Keys::RESOURCE_PATH}/#{ERB::Util.url_encode(@id)}" + "#{Keys::RESOURCE_PATH}/#{URI.encode_www_form_component(@id)}" end end end diff --git a/lib/typesense/override.rb b/lib/typesense/override.rb index 7aa2cdf..0525aef 100644 --- a/lib/typesense/override.rb +++ b/lib/typesense/override.rb @@ -19,7 +19,7 @@ def delete private def endpoint_path - "#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Overrides::RESOURCE_PATH}/#{ERB::Util.url_encode(@override_id)}" + "#{Collections::RESOURCE_PATH}/#{URI.encode_www_form_component(@collection_name)}#{Overrides::RESOURCE_PATH}/#{URI.encode_www_form_component(@override_id)}" end end end diff --git a/lib/typesense/overrides.rb b/lib/typesense/overrides.rb index 50a5580..61957c5 100644 --- a/lib/typesense/overrides.rb +++ b/lib/typesense/overrides.rb @@ -25,7 +25,7 @@ def [](override_id) private def endpoint_path(operation = nil) - "#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Overrides::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}" + "#{Collections::RESOURCE_PATH}/#{URI.encode_www_form_component(@collection_name)}#{Overrides::RESOURCE_PATH}#{operation.nil? ? '' : "/#{URI.encode_www_form_component(operation)}"}" end end end diff --git a/lib/typesense/preset.rb b/lib/typesense/preset.rb index 114bb1f..d6eb2ec 100644 --- a/lib/typesense/preset.rb +++ b/lib/typesense/preset.rb @@ -18,7 +18,7 @@ def delete private def endpoint_path - "#{Presets::RESOURCE_PATH}/#{ERB::Util.url_encode(@preset_name)}" + "#{Presets::RESOURCE_PATH}/#{URI.encode_www_form_component(@preset_name)}" end end end diff --git a/lib/typesense/presets.rb b/lib/typesense/presets.rb index 42bada1..83a2841 100644 --- a/lib/typesense/presets.rb +++ b/lib/typesense/presets.rb @@ -24,7 +24,7 @@ def [](preset_name) private def endpoint_path(operation = nil) - "#{Presets::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}" + "#{Presets::RESOURCE_PATH}#{operation.nil? ? '' : "/#{URI.encode_www_form_component(operation)}"}" end end end diff --git a/lib/typesense/synonym.rb b/lib/typesense/synonym.rb index 7d55591..e52dd89 100644 --- a/lib/typesense/synonym.rb +++ b/lib/typesense/synonym.rb @@ -19,7 +19,7 @@ def delete private def endpoint_path - "#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Synonyms::RESOURCE_PATH}/#{ERB::Util.url_encode(@synonym_id)}" + "#{Collections::RESOURCE_PATH}/#{URI.encode_www_form_component(@collection_name)}#{Synonyms::RESOURCE_PATH}/#{URI.encode_www_form_component(@synonym_id)}" end end end diff --git a/lib/typesense/synonyms.rb b/lib/typesense/synonyms.rb index 86b1d23..993e0ff 100644 --- a/lib/typesense/synonyms.rb +++ b/lib/typesense/synonyms.rb @@ -25,7 +25,7 @@ def [](synonym_id) private def endpoint_path(operation = nil) - "#{Collections::RESOURCE_PATH}/#{ERB::Util.url_encode(@collection_name)}#{Synonyms::RESOURCE_PATH}#{operation.nil? ? '' : "/#{ERB::Util.url_encode(operation)}"}" + "#{Collections::RESOURCE_PATH}/#{URI.encode_www_form_component(@collection_name)}#{Synonyms::RESOURCE_PATH}#{operation.nil? ? '' : "/#{URI.encode_www_form_component(operation)}"}" end end end diff --git a/spec/typesense/alias_spec.rb b/spec/typesense/alias_spec.rb index d8e0456..1e83618 100644 --- a/spec/typesense/alias_spec.rb +++ b/spec/typesense/alias_spec.rb @@ -25,7 +25,7 @@ end it 'returns the specified alias with URI encoded name' do - stub_request(:get, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/aliases/abc123%3F%3D%2B-_!%40%23%24%25%5E%26*()~%20%2F', typesense.configuration.nodes[0])) + stub_request(:get, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/aliases/abc123%3F%3D%2B-_!%40%23%24%25%5E%26*()~+%2F', typesense.configuration.nodes[0])) .with(headers: { 'X-Typesense-Api-Key' => typesense.configuration.api_key, 'Content-Type' => 'application/json'