Skip to content

Commit

Permalink
fix: use 'uri' to percent-encode string
Browse files Browse the repository at this point in the history
- explicitly require 'uri'. resolve #34
- uri is a lighter library compared to erb
  • Loading branch information
phiHero committed Oct 8, 2024
1 parent 77e9244 commit 613e245
Show file tree
Hide file tree
Showing 16 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/typesense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Typesense
end

require 'uri'
require_relative 'typesense/version'
require_relative 'typesense/configuration'
require_relative 'typesense/client'
Expand Down
2 changes: 1 addition & 1 deletion lib/typesense/alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/analytics_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/analytics_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/preset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/presets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/synonym.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/typesense/synonyms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/typesense/alias_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 613e245

Please sign in to comment.