Page MenuHomePhorge

No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/decode_rels.ex b/lib/tesla/middleware/decode_rels.ex
index 6b6874d..3aee3a6 100644
--- a/lib/tesla/middleware/decode_rels.ex
+++ b/lib/tesla/middleware/decode_rels.ex
@@ -1,54 +1,61 @@
defmodule Tesla.Middleware.DecodeRels do
@moduledoc """
Decode `Link` Hypermedia HTTP header into `opts[:rels]` field in response.
## Example usage
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.DecodeRels
end
env = MyClient.get("/...")
env.opts[:rels]
# => %{"Next" => "http://...", "Prev" => "..."}
```
"""
@behaviour Tesla.Middleware
@impl Tesla.Middleware
def call(env, next, _opts) do
env
|> Tesla.run(next)
|> parse_rels
end
defp parse_rels({:ok, env}), do: {:ok, parse_rels(env)}
defp parse_rels({:error, reason}), do: {:error, reason}
defp parse_rels(env) do
if link = Tesla.get_header(env, "link") do
Tesla.put_opt(env, :rels, rels(link))
else
env
end
end
defp rels(link) do
link
|> String.split(",")
|> Enum.map(&String.trim/1)
|> Enum.map(&rel/1)
+ |> List.flatten
|> Enum.into(%{})
end
defp rel(item) do
- [url, param] = String.split(item, ";")
- [_attr, value] = param |> String.trim() |> String.split("=")
- {value |> String.trim("'") |> String.trim("\""), url |> String.trim(">") |> String.trim("<")}
+ parts = String.split(item, ";")
+ {url, attrs} = List.pop_at(parts, 0)
+
+ Enum.map(attrs, fn param ->
+ [_attr, value] = param |> String.trim() |> String.split("=")
+
+ {value |> String.trim("'") |> String.trim("\""),
+ url |> String.trim(">") |> String.trim("<")}
+ end)
end
end

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 12:34 PM (1 d, 10 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39778
Default Alt Text
(1 KB)

Event Timeline