Skip to content

Commit 173758c

Browse files
authored
Merge pull request #4 from r8/fixes
Fixes
2 parents 324a522 + 0fc428f commit 173758c

File tree

13 files changed

+125
-30
lines changed

13 files changed

+125
-30
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Change Log
2+
3+
## [0.1.0] - 2017-02-25
4+
5+
Initial release.

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
[![Inline docs](http://inch-ci.org/github/r8/elixir-oembed.svg?branch=master&style=flat)](http://inch-ci.org/github/r8/elixir-oembed)
66
[![Ebert](https://ebertapp.io/github/r8/elixir-oembed.svg)](https://ebertapp.io/github/r8/elixir-oembed)
77

8-
Work in progress
8+
oEmbed consumer library for Elixir applications.
99

10-
oEmbed consumer for Elixir
10+
> oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly.
11+
> -- See [oembed.com](http://oembed.com) for more info about the protocol.
12+
13+
This library supports any discoverable oEmbed endpoint and some other services via custom adapters.
14+
Among them:
15+
16+
- Instagram
17+
- Pinterest
18+
19+
## Installation
20+
21+
Add `oembed` to your list of dependencies in `mix.exs`:
22+
23+
```elixir
24+
def deps do
25+
[{:oembed, "~> 0.1.0"}]
26+
end
27+
```
28+
29+
## Usage
30+
31+
```elixir
32+
{:ok, result} = OEmbed.for("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
33+
```

lib/oembed.ex

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
defmodule OEmbed do
22
@moduledoc """
3-
oEmbed consumer for Elixir.
3+
oEmbed consumer library for Elixir applications.
4+
5+
## Usage
6+
7+
```elixir
8+
{:ok, result} = OEmbed.for("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
9+
```
410
"""
511

12+
@core_providers [OEmbed.InstagramProvider,
13+
OEmbed.PinterestProvider,
14+
OEmbed.DiscoverableProvider]
15+
16+
@doc """
17+
Get oEmbed structure for given URL.
18+
19+
## Example
20+
21+
```elixir
22+
{:ok, result} = OEmbed.for("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
23+
```
24+
"""
625
def for(url) do
726
case Enum.find(get_providers(), fn(provider) -> provider.provides?(url) end) do
827
nil ->
@@ -13,8 +32,6 @@ defmodule OEmbed do
1332
end
1433

1534
defp get_providers do
16-
[OEmbed.InstagramProvider,
17-
OEmbed.PinterestProvider,
18-
OEmbed.DiscoverProvider]
35+
@core_providers
1936
end
2037
end

lib/oembed/provider.ex

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
defmodule OEmbed.Provider do
2+
@moduledoc """
3+
oEmbed provider behaviour.
4+
"""
5+
26
defmacro __using__(_) do
37
quote do
48
import OEmbed.Provider
59

6-
@behaviour OEmbed.Provider
7-
end
8-
end
10+
alias OEmbed.Link
11+
alias OEmbed.Photo
12+
alias OEmbed.Rich
13+
alias OEmbed.Video
914

10-
alias OEmbed.{Link, Photo, Rich, Video}
15+
@behaviour OEmbed.Provider
1116

12-
@callback provides?(String.t) :: boolean
17+
defp get_oembed(url) do
18+
with {:ok, %HTTPoison.Response{body: body}} <- HTTPoison.get(url, [], [follow_redirect: true, ssl: [{:versions, [:'tlsv1.2']}]]),
19+
{:ok, struct} <- Poison.decode(body),
20+
resource <- get_resource(struct) do
21+
{:ok, resource}
22+
else
23+
_ -> {:error, "oEmbed url not found"}
24+
end
25+
end
1326

14-
def get_oembed(url) do
15-
with {:ok, %HTTPoison.Response{body: body}} <- HTTPoison.get(url, [], [follow_redirect: true, ssl: [{:versions, [:'tlsv1.2']}]]),
16-
{:ok, struct} <- Poison.decode(body),
17-
resource <- get_resource(struct) do
18-
{:ok, resource}
19-
else
20-
_ -> {:error, "oEmbed url not found"}
27+
defp get_resource(%{"type" => "link"} = struct), do: Link.new(struct)
28+
defp get_resource(%{"type" => "photo"} = struct), do: Photo.new(struct)
29+
defp get_resource(%{"type" => "rich"} = struct), do: Rich.new(struct)
30+
defp get_resource(%{"type" => "video"} = struct), do: Video.new(struct)
31+
defp get_resource(struct), do: struct
2132
end
2233
end
2334

24-
defp get_resource(%{"type" => "link"} = struct), do: Link.new(struct)
25-
defp get_resource(%{"type" => "photo"} = struct), do: Photo.new(struct)
26-
defp get_resource(%{"type" => "rich"} = struct), do: Rich.new(struct)
27-
defp get_resource(%{"type" => "video"} = struct), do: Video.new(struct)
28-
defp get_resource(struct), do: struct
29-
end
35+
@callback provides?(String.t) :: boolean
36+
@callback get(String.t) :: struct
37+
end

lib/oembed/providers/discover_provider.ex renamed to lib/oembed/providers/discoverable_provider.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
defmodule OEmbed.DiscoverProvider do
1+
defmodule OEmbed.DiscoverableProvider do
2+
@moduledoc """
3+
oEmbed provider for discoverable endpoints.
4+
"""
5+
26
use OEmbed.Provider
37

8+
@doc """
9+
Check if this provider supports given URL.
10+
"""
411
def provides?(_) do
512
true
613
end
714

15+
@doc """
16+
Get oEmbed result for given URL.
17+
"""
818
def get(url) do
919
with {:ok, href} <- discover(url),
1020
{:ok, oembed} <- get_oembed(href) do
@@ -15,7 +25,7 @@ defmodule OEmbed.DiscoverProvider do
1525
end
1626
end
1727

18-
def discover(url) do
28+
defp discover(url) do
1929
with {:ok, %HTTPoison.Response{body: html}} <- HTTPoison.get(url, [], [follow_redirect: true, ssl: [{:versions, [:'tlsv1.2']}]]),
2030
[_ | _] = tags <- Floki.find(html, "head link[type='application/json+oembed']"),
2131
{"link", attributes, _} <- List.first(tags),

lib/oembed/providers/instagram_provider.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
defmodule OEmbed.InstagramProvider do
2+
@moduledoc """
3+
oEmbed provider for Instagram URLs.
4+
"""
25
use OEmbed.Provider
36

47
@oembed_endpoint "https://api.instagram.com/oembed?url="
58

9+
@doc """
10+
Check if this provider supports given URL.
11+
"""
612
def provides?(url) do
713
Regex.match?(~r/^(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.com|instagr.am)\/([A-Za-z0-9-_]+)/im, url)
814
end
915

16+
@doc """
17+
Get oEmbed result for given URL.
18+
"""
1019
def get(url) do
1120
get_oembed(@oembed_endpoint <> url)
1221
end

lib/oembed/providers/pinterest_provider.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
defmodule OEmbed.PinterestProvider do
2+
@moduledoc """
3+
oEmbed provider for Pinterest URLs.
4+
"""
25
use OEmbed.Provider
36

47
alias OEmbed.Rich
58

9+
@doc """
10+
Check if this provider supports given URL.
11+
"""
612
def provides?(url) do
713
Regex.match?(~r/^(?:(?:http|https):\/\/)?(?:[A-Za-z0-9-_]+\.)*?(?:pinterest.com)\/(?:pin)?\/(?:[0-9-_]+)/im, url)
814
end
915

16+
@doc """
17+
Get oEmbed result for given URL.
18+
"""
1019
def get(url) do
1120
oembed = Rich.new(%{
1221
html: get_pin_html(url)
@@ -15,7 +24,7 @@ defmodule OEmbed.PinterestProvider do
1524
{:ok, oembed}
1625
end
1726

18-
def get_pin_html(url) do
27+
defp get_pin_html(url) do
1928
"""
2029
<a data-pin-do="embedPin" data-pin-width="large" href="#{url}"></a>
2130
<script async defer src="//assets.pinterest.com/js/pinit.js"></script>

lib/oembed/resource.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule OEmbed.Resource do
22
@moduledoc """
3-
oEmbed resource
3+
oEmbed resource.
44
"""
55

66
defmacro __using__(_) do

lib/oembed/resources/link.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
defmodule OEmbed.Link do
2+
@moduledoc """
3+
oEmbed Link resource.
4+
"""
25

36
@keys [type: "link"]
47

lib/oembed/resources/photo.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
defmodule OEmbed.Photo do
2+
@moduledoc """
3+
oEmbed Photo resource.
4+
"""
25

36
@keys [type: "photo", url: nil, width: nil, height: nil]
47

0 commit comments

Comments
 (0)