Skip to content

Commit

Permalink
feat: add parameter to disable path encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Paula Pasqualini committed Oct 7, 2022
1 parent dae48e4 commit 581c6bc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
46 changes: 46 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
PATH
remote: .
specs:
imgix (4.0.3)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
ansi (1.5.0)
benchmark-ips (2.10.0)
builder (3.2.4)
crack (0.4.5)
rexml
hashdiff (1.0.1)
json (2.6.2)
minitest (5.16.3)
minitest-reporters (1.5.0)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
public_suffix (5.0.0)
rake (13.0.6)
rexml (3.2.5)
ruby-progressbar (1.11.0)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
x86_64-darwin-21

DEPENDENCIES
benchmark-ips
imgix!
json
minitest
minitest-reporters
rake
webmock

BUNDLED WITH
2.3.3
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ client.path('/images/demo.png').to_url(w: 200)
#=> https://your-subdomain.imgix.net/images/demo.png?w=200&s=2eadddacaa9bba4b88900d245f03f51e
```

To disable path encoding, pass `{disable_path_encoding: true}` as the second argument to the `Imgix#Path#to_url` function.

```rb
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token')
client.path('/images/demo.png').to_url
#=> https://your-subdomain.imgix.net/%5Bimages%5D/demo.png?s=270832685733a36ba02bd8ab9fd72df5
client.path('/images/demo.png').to_url({},{disable_path_encoding: true})
#=> https://your-subdomain.imgix.net/[images]/demo.png?s=ed6eb07e9eff3f6c8bbcc83fc4f63198

## Srcset Generation

The imgix gem allows for generation of custom `srcset` attributes, which can be invoked through `Imgix::Path#to_srcset`. By default, the `srcset` generated will allow for responsive size switching by building a list of image-width mappings.
Expand Down
23 changes: 13 additions & 10 deletions lib/imgix/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def initialize(prefix, secure_url_token, path = "/")
@options = {}
end

def to_url(opts = {})
sanitized_path = sanitize_path(@path)
def to_url(params = {}, options = {})
sanitized_path = sanitize_path(@path, options)
prev_options = @options.dup
@options.merge!(opts)
@options.merge!(params)

current_path_and_params = path_and_params(sanitized_path)
url = @prefix + current_path_and_params
Expand Down Expand Up @@ -132,16 +132,19 @@ def method_missing(method, *args, &block)
# This includes " +?:#" characters. If a path is being used as a proxy, utf8
# encode everything. If it is not being used as proxy, leave certain chars, like
# "/", alone. Method assumes path is not already encoded.
def sanitize_path(path)
def sanitize_path(path, options = {})
# remove the leading "/", we'll add it back after encoding
path = path.slice(1, path.length) if Regexp.new('^/') =~ path
# if path is being used as a proxy, encode the entire thing
if /^https?/ =~ path
return encode_URI_Component(path)
else
# otherwise, encode only specific characters
return encode_URI(path)
if !options[:disable_path_encoding]
# if path is being used as a proxy, encode the entire thing
if /^https?/ =~ path
return encode_URI_Component(path)
else
# otherwise, encode only specific characters
return encode_URI(path)
end
end
return "/" + path
end

# URL encode the entire path
Expand Down
6 changes: 6 additions & 0 deletions test/units/path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_creating_a_path
assert_equal "https://demo.imgix.net/images/demo.png?s=2c7c157eaf23b06a0deb2f60b81938c4", path.to_url
end

def test_disable_path_encoding
path = client.path("[images]/demo.png")
assert_equal "https://demo.imgix.net/%5Bimages%5D/demo.png?s=270832685733a36ba02bd8ab9fd72df5", path.to_url
assert_equal "https://demo.imgix.net/[images]/demo.png?s=ed6eb07e9eff3f6c8bbcc83fc4f63198", path.to_url({},{disable_path_encoding: true})
end

def test_signing_path_with_param
url = "https://demo.imgix.net/images/demo.png?w=200&s=da421114ca238d1f4a927b889f67c34e"
path = client.path("/images/demo.png").w(200)
Expand Down

0 comments on commit 581c6bc

Please sign in to comment.