Skip to content

Commit

Permalink
Extract methods to decode and encode URI segments
Browse files Browse the repository at this point in the history
I wonder if we might want to roll our own eventually, seeing the state
of Ruby URI encoding/decoding methods are kinda of a mess.

- URI.encode_uri_component encodes ~ as %7E, which is not what we
  want.
- ERB::Util.url_encode does not encode ~, but has no symmetric
  ERB::Util.url_decode method.
  • Loading branch information
koppen committed Dec 6, 2024
1 parent 916ac7f commit ef05369
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/route_downcaser/downcase_route_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def downcased_path(uri)
end

def downcase_path_segment(segment)
URI.encode_www_form_component(URI.decode_www_form_component(segment).downcase)
uri_encode(uri_decode(segment).downcase)
end

def path(uri)
Expand All @@ -77,6 +77,14 @@ def has_querystring?(uri)
uri_items(uri).length > 1
end

def uri_decode(uri_segment)
URI.decode_www_form_component(uri_segment)
end

def uri_encode(uri_segment)
URI.encode_www_form_component(uri_segment)
end

def uri_items(uri)
uri.split("?", 2)
end
Expand Down

0 comments on commit ef05369

Please sign in to comment.