Skip to content

Commit

Permalink
Switch to using keyword lists internally
Browse files Browse the repository at this point in the history
  • Loading branch information
axelson committed Nov 5, 2024
1 parent 9de214f commit 72e251d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes to imgex

## Unreleased

* [Fix compilation warnings and update deps](https://github.com/ianwalter/imgex/pull/15)
* [#17 Switch to using keyword lists internally](https://github.com/ianwalter/imgex/pull/17)

## v0.3.0

* [#8 Add Imgex.srcset to generate srcset pairs](https://github.com/ianwalter/imgex/pull/8)
Expand Down
38 changes: 21 additions & 17 deletions lib/imgex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ defmodule Imgex do
https://my-social-network.imgix.net/images/lulu.jpg?dpr=3&w=100&s=97d0f1731b4c8d8dd609424dfca2eab5 3x,
https://my-social-network.imgix.net/images/lulu.jpg?dpr=4&w=100&s=b96a02e08eeb50df5a75223c998e46f5 4x,
https://my-social-network.imgix.net/images/lulu.jpg?dpr=5&w=100&s=9ba1ab37db9f09283d9194223fbafb2f 5x"
iex> Imgex.srcset("/images/lulu.jpg", %{ar: "3:4", h: 500})
"https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=1&h=500&s=fa2016a84454271a30c00c93a6d236a2 1x,
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=2&h=500&s=43303719ce9a76e618c6d16ef7b5f30f 2x,
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=3&h=500&s=b1f39589cf13b10a7480c4b90f4dcea4 3x,
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=4&h=500&s=1be6ccb379a227b8e4cfa8ebcbca2b76 4x,
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=5&h=500&s=455776036fb49c420f20d93fb59af96e 5x"
iex> Imgex.srcset("/images/lulu.jpg", ar: "3:4", h: 500)
"https://my-social-network.imgix.net/images/lulu.jpg?dpr=1&ar=3%3A4&h=500&s=842a70d9c7ead7417b4a8056f45a88b3 1x,
https://my-social-network.imgix.net/images/lulu.jpg?dpr=2&ar=3%3A4&h=500&s=7cce91f2cd0d2bd1d252ca241523c09b 2x,
https://my-social-network.imgix.net/images/lulu.jpg?dpr=3&ar=3%3A4&h=500&s=509e0045d21a08324811d2db978c874c 3x,
https://my-social-network.imgix.net/images/lulu.jpg?dpr=4&ar=3%3A4&h=500&s=cc5790442b6185768435a48a44e040c9 4x,
https://my-social-network.imgix.net/images/lulu.jpg?dpr=5&ar=3%3A4&h=500&s=cf724f11656961377da13f8608c60b4a 5x"
"""
def srcset(
path,
params \\ %{},
params \\ [],
source \\ configured_source()
)
when is_map(params) do
when (is_list(params) or is_map(params)) do
params = to_list(params)
width = params[:w]
height = params[:h]
aspect_ratio = params[:ar]
Expand Down Expand Up @@ -112,24 +112,28 @@ defmodule Imgex do
"https://cannonball.imgix.net/images/jets.png?con=10&s=d982f04bbca4d819971496524aa5f95a"
"""
def url(path, params \\ %{}, source \\ configured_source()) when is_map(params) do
def url(path, params \\ [], source \\ configured_source()) when (is_map(params) or is_list(params)) do
params = to_list(params)
# Add query parameters to the path.
path = path_with_params(path, params)

# Use a md5 hash of the path and secret token as a signature.
signature = Base.encode16(:erlang.md5(source.token <> path), case: :lower)

# Append the signature to verify the request is valid and return the URL.
if params == %{} do
if params == [] do
source.domain <> path <> "?s=" <> signature
else
source.domain <> path <> "&s=" <> signature
end
end

defp path_with_params(path, params) when params == %{}, do: path
defp to_list(params) when is_list(params), do: params
defp to_list(params) when is_map(params), do: Map.to_list(params)

defp path_with_params(path, params) when params == [], do: path

defp path_with_params(path, params) when is_map(params) do
defp path_with_params(path, params) when is_list(params) do
path <> "?" <> URI.encode_query(params)
end

Expand Down Expand Up @@ -170,18 +174,18 @@ defmodule Imgex do

@default_srcset_target_ratios [1, 2, 3, 4, 5]

defp build_srcset_pairs(path, params, source) when is_map(params) do
defp build_srcset_pairs(path, params, source) when is_list(params) do
@default_srcset_target_widths
|> Enum.map(fn width ->
updated_params = Map.put(params, :w, width)
updated_params = Keyword.put(params, :w, width)
url(path, updated_params, source) <> " #{width}w"
end)
|> Enum.join(",\n")
end

defp build_dpr_srcset(path, params, source) when is_map(params) do
defp build_dpr_srcset(path, params, source) when is_list(params) do
Enum.map(@default_srcset_target_ratios, fn ratio ->
updated_params = Map.put(params, :dpr, ratio)
updated_params = Keyword.put(params, :dpr, ratio)
url(path, updated_params, source) <> " #{ratio}x"
end)
|> Enum.join(",\n")
Expand Down
19 changes: 13 additions & 6 deletions test/imgex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ defmodule ImgexTest do
"https://my-social-network.imgix.net/images/jets.png?s=7c6a3ef8679f4965f5aaecb66547fa61"
end

test "url/2 supports a map params are an empty map generates an appropriate url" do
url = Imgex.url("/images/jets.png", %{fm: "auto", h: 100})
uri = URI.new!(url)

assert %{"fm" => "auto", "h" => "100"} = URI.decode_query(uri.query)
end

describe "srcset/3" do
@default_srcset_widths ~w(
100 116 134 156 182 210 244 282 328 380 442 512 594 688 798 926 1074
Expand All @@ -35,15 +42,15 @@ defmodule ImgexTest do
test "with only a height, generates 31 width pairs" do
path = "/images/lulu.jpg"

srcset = Imgex.srcset(path, %{h: 100})
srcset = Imgex.srcset(path, h: 100)
split = String.split(srcset, ",\n")
assert length(split) == 31

@default_srcset_widths
|> Enum.with_index()
|> Enum.each(fn {width, i} ->
src = Enum.at(split, i)
assert src == "#{Imgex.url(path, %{h: 100, w: width})} #{width}w"
assert src == "#{Imgex.url(path, w: width, h: 100)} #{width}w"
end)
end

Expand All @@ -56,21 +63,21 @@ defmodule ImgexTest do
[1, 2, 3, 4, 5]
|> Enum.each(fn dpr ->
src = Enum.at(split, dpr - 1)
assert src == "#{Imgex.url(path, %{ar: "3:4", dpr: dpr, h: 100})} #{dpr}x"
assert src == "#{Imgex.url(path, dpr: dpr, h: 100, ar: "3:4")} #{dpr}x"
end)
end

test "with a height, aspect ratio, and other params, generates 5 dpr pairs" do
path = "/images/lulu.jpg"
params = %{ar: "3:4", crop: "faces,entropy,left", h: 100}
params = [ar: "3:4", crop: "faces,entropy,left", h: 100]
srcset = Imgex.srcset(path, params)
split = String.split(srcset, ",\n")
assert length(split) == 5

[1, 2, 3, 4, 5]
|> Enum.each(fn dpr ->
src = Enum.at(split, dpr - 1)
assert src == "#{Imgex.url(path, Map.put(params, :dpr, dpr))} #{dpr}x"
assert src == "#{Imgex.url(path, Keyword.put(params, :dpr, dpr))} #{dpr}x"
end)
end

Expand All @@ -83,7 +90,7 @@ defmodule ImgexTest do
[1, 2, 3, 4, 5]
|> Enum.each(fn dpr ->
src = Enum.at(split, dpr - 1)
assert src == "#{Imgex.url(path, %{dpr: dpr, w: 100})} #{dpr}x"
assert src == "#{Imgex.url(path, dpr: dpr, w: 100)} #{dpr}x"
end)
end
end
Expand Down

0 comments on commit 72e251d

Please sign in to comment.