@@ -71,20 +71,20 @@ defmodule Imgex do
71
71
https://my-social-network.imgix.net/images/lulu.jpg?dpr=3&w=100&s=97d0f1731b4c8d8dd609424dfca2eab5 3x,
72
72
https://my-social-network.imgix.net/images/lulu.jpg?dpr=4&w=100&s=b96a02e08eeb50df5a75223c998e46f5 4x,
73
73
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"
81
80
"""
82
81
def srcset (
83
82
path ,
84
- params \\ % { } ,
83
+ params \\ [ ] ,
85
84
source \\ configured_source ( )
86
85
)
87
- when is_map ( params ) do
86
+ when ( is_list ( params ) or is_map ( params ) ) do
87
+ params = to_list ( params )
88
88
width = params [ :w ]
89
89
height = params [ :h ]
90
90
aspect_ratio = params [ :ar ]
@@ -112,24 +112,28 @@ defmodule Imgex do
112
112
"https://cannonball.imgix.net/images/jets.png?con=10&s=d982f04bbca4d819971496524aa5f95a"
113
113
114
114
"""
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 )
116
117
# Add query parameters to the path.
117
118
path = path_with_params ( path , params )
118
119
119
120
# Use a md5 hash of the path and secret token as a signature.
120
121
signature = Base . encode16 ( :erlang . md5 ( source . token <> path ) , case: :lower )
121
122
122
123
# Append the signature to verify the request is valid and return the URL.
123
- if params == % { } do
124
+ if params == [ ] do
124
125
source . domain <> path <> "?s=" <> signature
125
126
else
126
127
source . domain <> path <> "&s=" <> signature
127
128
end
128
129
end
129
130
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
131
135
132
- defp path_with_params ( path , params ) when is_map ( params ) do
136
+ defp path_with_params ( path , params ) when is_list ( params ) do
133
137
path <> "?" <> URI . encode_query ( params )
134
138
end
135
139
@@ -170,18 +174,18 @@ defmodule Imgex do
170
174
171
175
@ default_srcset_target_ratios [ 1 , 2 , 3 , 4 , 5 ]
172
176
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
174
178
@ default_srcset_target_widths
175
179
|> Enum . map ( fn width ->
176
- updated_params = Map . put ( params , :w , width )
180
+ updated_params = Keyword . put ( params , :w , width )
177
181
url ( path , updated_params , source ) <> " #{ width } w"
178
182
end )
179
183
|> Enum . join ( ",\n " )
180
184
end
181
185
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
183
187
Enum . map ( @ default_srcset_target_ratios , fn ratio ->
184
- updated_params = Map . put ( params , :dpr , ratio )
188
+ updated_params = Keyword . put ( params , :dpr , ratio )
185
189
url ( path , updated_params , source ) <> " #{ ratio } x"
186
190
end )
187
191
|> Enum . join ( ",\n " )
0 commit comments