Skip to content

Commit e691681

Browse files
committed
Switch to using keyword lists internally
1 parent 9de214f commit e691681

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes to imgex
22

3+
## Unreleased
4+
5+
* [#15 Fix compilation warnings and update deps](https://github.com/ianwalter/imgex/pull/15)
6+
* [#17 Switch to using keyword lists internally](https://github.com/ianwalter/imgex/pull/17)
7+
38
## v0.3.0
49

510
* [#8 Add Imgex.srcset to generate srcset pairs](https://github.com/ianwalter/imgex/pull/8)

lib/imgex.ex

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@ defmodule Imgex do
7171
https://my-social-network.imgix.net/images/lulu.jpg?dpr=3&w=100&s=97d0f1731b4c8d8dd609424dfca2eab5 3x,
7272
https://my-social-network.imgix.net/images/lulu.jpg?dpr=4&w=100&s=b96a02e08eeb50df5a75223c998e46f5 4x,
7373
https://my-social-network.imgix.net/images/lulu.jpg?dpr=5&w=100&s=9ba1ab37db9f09283d9194223fbafb2f 5x"
74-
iex> Imgex.srcset("/images/lulu.jpg", %{ar: "3:4", h: 500})
75-
"https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=1&h=500&s=fa2016a84454271a30c00c93a6d236a2 1x,
76-
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=2&h=500&s=43303719ce9a76e618c6d16ef7b5f30f 2x,
77-
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=3&h=500&s=b1f39589cf13b10a7480c4b90f4dcea4 3x,
78-
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=4&h=500&s=1be6ccb379a227b8e4cfa8ebcbca2b76 4x,
79-
https://my-social-network.imgix.net/images/lulu.jpg?ar=3%3A4&dpr=5&h=500&s=455776036fb49c420f20d93fb59af96e 5x"
80-
74+
iex> Imgex.srcset("/images/lulu.jpg", ar: "3:4", h: 500)
75+
"https://my-social-network.imgix.net/images/lulu.jpg?dpr=1&ar=3%3A4&h=500&s=842a70d9c7ead7417b4a8056f45a88b3 1x,
76+
https://my-social-network.imgix.net/images/lulu.jpg?dpr=2&ar=3%3A4&h=500&s=7cce91f2cd0d2bd1d252ca241523c09b 2x,
77+
https://my-social-network.imgix.net/images/lulu.jpg?dpr=3&ar=3%3A4&h=500&s=509e0045d21a08324811d2db978c874c 3x,
78+
https://my-social-network.imgix.net/images/lulu.jpg?dpr=4&ar=3%3A4&h=500&s=cc5790442b6185768435a48a44e040c9 4x,
79+
https://my-social-network.imgix.net/images/lulu.jpg?dpr=5&ar=3%3A4&h=500&s=cf724f11656961377da13f8608c60b4a 5x"
8180
"""
8281
def srcset(
8382
path,
84-
params \\ %{},
83+
params \\ [],
8584
source \\ configured_source()
8685
)
87-
when is_map(params) do
86+
when (is_list(params) or is_map(params)) do
87+
params = to_list(params)
8888
width = params[:w]
8989
height = params[:h]
9090
aspect_ratio = params[:ar]
@@ -112,24 +112,28 @@ defmodule Imgex do
112112
"https://cannonball.imgix.net/images/jets.png?con=10&s=d982f04bbca4d819971496524aa5f95a"
113113
114114
"""
115-
def url(path, params \\ %{}, source \\ configured_source()) when is_map(params) do
115+
def url(path, params \\ [], source \\ configured_source()) when (is_map(params) or is_list(params)) do
116+
params = to_list(params)
116117
# Add query parameters to the path.
117118
path = path_with_params(path, params)
118119

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

122123
# Append the signature to verify the request is valid and return the URL.
123-
if params == %{} do
124+
if params == [] do
124125
source.domain <> path <> "?s=" <> signature
125126
else
126127
source.domain <> path <> "&s=" <> signature
127128
end
128129
end
129130

130-
defp path_with_params(path, params) when params == %{}, do: path
131+
defp to_list(params) when is_list(params), do: params
132+
defp to_list(params) when is_map(params), do: Map.to_list(params)
133+
134+
defp path_with_params(path, params) when params == [], do: path
131135

132-
defp path_with_params(path, params) when is_map(params) do
136+
defp path_with_params(path, params) when is_list(params) do
133137
path <> "?" <> URI.encode_query(params)
134138
end
135139

@@ -170,18 +174,18 @@ defmodule Imgex do
170174

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

173-
defp build_srcset_pairs(path, params, source) when is_map(params) do
177+
defp build_srcset_pairs(path, params, source) when is_list(params) do
174178
@default_srcset_target_widths
175179
|> Enum.map(fn width ->
176-
updated_params = Map.put(params, :w, width)
180+
updated_params = Keyword.put(params, :w, width)
177181
url(path, updated_params, source) <> " #{width}w"
178182
end)
179183
|> Enum.join(",\n")
180184
end
181185

182-
defp build_dpr_srcset(path, params, source) when is_map(params) do
186+
defp build_dpr_srcset(path, params, source) when is_list(params) do
183187
Enum.map(@default_srcset_target_ratios, fn ratio ->
184-
updated_params = Map.put(params, :dpr, ratio)
188+
updated_params = Keyword.put(params, :dpr, ratio)
185189
url(path, updated_params, source) <> " #{ratio}x"
186190
end)
187191
|> Enum.join(",\n")

test/imgex_test.exs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ defmodule ImgexTest do
1212
"https://my-social-network.imgix.net/images/jets.png?s=7c6a3ef8679f4965f5aaecb66547fa61"
1313
end
1414

15+
test "url/2 supports a map params are an empty map generates an appropriate url" do
16+
url = Imgex.url("/images/jets.png", %{fm: "auto", h: 100})
17+
uri = URI.new!(url)
18+
19+
assert %{"fm" => "auto", "h" => "100"} = URI.decode_query(uri.query)
20+
end
21+
1522
describe "srcset/3" do
1623
@default_srcset_widths ~w(
1724
100 116 134 156 182 210 244 282 328 380 442 512 594 688 798 926 1074
@@ -35,15 +42,15 @@ defmodule ImgexTest do
3542
test "with only a height, generates 31 width pairs" do
3643
path = "/images/lulu.jpg"
3744

38-
srcset = Imgex.srcset(path, %{h: 100})
45+
srcset = Imgex.srcset(path, h: 100)
3946
split = String.split(srcset, ",\n")
4047
assert length(split) == 31
4148

4249
@default_srcset_widths
4350
|> Enum.with_index()
4451
|> Enum.each(fn {width, i} ->
4552
src = Enum.at(split, i)
46-
assert src == "#{Imgex.url(path, %{h: 100, w: width})} #{width}w"
53+
assert src == "#{Imgex.url(path, w: width, h: 100)} #{width}w"
4754
end)
4855
end
4956

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

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

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

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

0 commit comments

Comments
 (0)