Skip to content

Commit

Permalink
Percent-encode square brackets in :url_encoded part of the result
Browse files Browse the repository at this point in the history
  • Loading branch information
squadette committed Oct 17, 2017
1 parent c74b28d commit 69f8ff7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.1.6:
0.1.7:

* square brackets are also special strings, because of HTTParty;
* percent-encode square brackets in :url_encoded part of the result, because of HTTParty;

0.1.5:

Expand Down
2 changes: 1 addition & 1 deletion lib/linkhum/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.generate_url(parts)
end

def self.encode_component(string, skip_percent_sign = true)
chars_to_keep_unencoded = Addressable::URI::CharacterClasses::RESERVED + Addressable::URI::CharacterClasses::UNRESERVED
chars_to_keep_unencoded = "\\:\\/\\?\\#\\@\\!\\$\\&\\'\\(\\)\\*\\+\\,\\;\\=" + Addressable::URI::CharacterClasses::UNRESERVED
chars_to_keep_unencoded << '\\%' if skip_percent_sign
Addressable::URI.encode_component(string, chars_to_keep_unencoded)
end
Expand Down
2 changes: 1 addition & 1 deletion linkhum-url.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'linkhum-url'
s.version = '0.1.6'
s.version = '0.1.7'
s.date = '2017-10-17'
s.summary = "Linkhum-URL creates both URL-encoded and readable versions of URLs"
s.description = "Input URL could be either human-readable, or URL-encoded. Two URLs are returned as result: human-readable and URL-encoded."
Expand Down
6 changes: 6 additions & 0 deletions spec/linkhum-url.spec
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ describe Linkhum::URL do
expect(lu[:url_encoded]).to eql("https://example.org/search.html?q=%D0%BF%D0%B8%D0%B2%D0%B1%D0%B0%D1%80")
end

it "percent-encodes square brackets in paths" do
lu = Linkhum::URL.parse("http://www.example.com/image[2].jpg")
expect(lu[:human_readable]).to eql("http://www.example.com/image[2].jpg")
expect(lu[:url_encoded]).to eql("http://www.example.com/image%5B2%5D.jpg")
end

it "handles non-ASCII fragments" do
lu = Linkhum::URL.parse("https://example.org/venues.html#пивбар")
expect(lu[:human_readable]).to eql("https://example.org/venues.html#пивбар")
Expand Down

0 comments on commit 69f8ff7

Please sign in to comment.