@@ -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 " )
0 commit comments