ReqEmbed (ReqEmbed v0.1.2)

View Source

oEmbed plugin for Req.

Supports discovery and 300+ providers.

Usage

Mix.install([
  {:req, "~> 0.5"},
  {:req_embed, "~> 0.1"}
])

req = Req.new() |> ReqEmbed.attach()

Req.get!(req, url: "https://www.youtube.com/watch?v=XfELJU1mRMg").body
#=>
# %ReqEmbed.Video{
#   type: "video",
#   version: "1.0",
#   title: "Rick Astley - Never Gonna Give You Up (Official Music Video)",
#   author_name: "Supirorguy508",
#   author_url: "https://www.youtube.com/@supirorguy5086",
#   provider_name: "YouTube",
#   provider_url: "https://www.youtube.com/",
#   cache_age: nil,
#   thumbnail_url: "https://i.ytimg.com/vi/XfELJU1mRMg/hqdefault.jpg",
#   thumbnail_width: 480,
#   thumbnail_height: 360,
#   html: "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/XfELJU1mRMg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen title=\"Rick Astley - Never Gonna Give You Up (Official Music Video)\"></iframe>",
#   width: 200,
#   height: 113
# }

When successful, the response body will contain either one of the following structs representing the oEmbed type:

Summary

Functions

Attach the oEmbed plugin into Req.

Functions

attach(request, options \\ [])

@spec attach(
  Req.Request.t(),
  keyword()
) :: Req.Request.t()

Attach the oEmbed plugin into Req.

Options

  • :query (map/0) - Defaults to %{}. The query parameters to be sent to the oEmbed endpoint, like :maxwidth, :maxheight, and others supported by the provider. The parameters :url and :format are managed by the plugin, you can't override them. See section 2.2 Consumer Request for more info.

  • :discover (boolean/0) - Defaults to true. When enabled, it will first attempt to auto-discover the oEmbed endpoint by looking for the link tag in the HTML response. If no link tag is found, it will fallback to searching for a known provider endpoint. When disabled, it will skip the HTML parsing step and only use the known provider endpoints.

Examples

iex> req = Req.new() |> ReqEmbed.attach()
iex> Req.get!(req, url: "https://x.com/ThinkingElixir/status/1848702455313318251").body
iex> %ReqEmbed.Rich{
       type: "rich",
       version: "1.0",
       author_name: "ThinkingElixir",
       author_url: "https://twitter.com/ThinkingElixir",
       html: "<blockquote class="twitter-tweet"><p lang="en" dir="ltr">News includes upcoming Elixir v1.18 ...
       ...
     }