Skip to content

Commit

Permalink
test that vimeo fetches the html with the oembed.json
Browse files Browse the repository at this point in the history
  • Loading branch information
fatboypunk committed Nov 19, 2018
1 parent 770b869 commit a846c9e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
34 changes: 34 additions & 0 deletions fixture/vcr_cassettes/vimeo_valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"request":{
"body": "",
"headers": [],
"method": "get",
"options": {
"follow_redirect": "true",
"ssl_options": {
"versions": [
"tlsv1.2"
]
}
},
"request_body": "",
"url": "https://vimeo.com/api/oembed.json?url=https%3A%2F%2Fplayer.vimeo.com%2Fvideos%2F123123123%2F"
},
"response": {
"body": "{\"width\":480,\"video_id\":301578941,\"version\":\"1.0\",\"uri\":\"\/videos\/301578941\",\"upload_date\":\"2018-11-19 05:47:26\",\"type\":\"video\",\"title\":\"embed test\",\"thumbnail_width\":295,\"thumbnail_url_with_play_button\":\"https:\/\/i.vimeocdn.com\/filter\/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F740162198_295x166.jpg&src1=https%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png\",\"thumbnail_url\":\"https:\/\/i.vimeocdn.com\/video\/740162198_295x166.jpg\",\"thumbnail_height\":221,\"provider_url\":\"https:\/\/vimeo.com\/\",\"provider_name\":\"Vimeo\",\"is_plus\":\"1\",\"html\":\"<iframe src=\\\"https:\/\/player.vimeo.com\/video\/123123123?app_id=123123\\\" width=\\\"480\\\" height=\\\"360\\\" frameborder=\\\"0\\\" title=\\\"embed test\\\" allow=\\\"autoplay; fullscreen\\\" allowfullscreen><\/iframe>\",\"height\":360,\"duration\":30,\"domain_status_code\":403,\"description\":\"\",\"author_url\":\"https:\/\/vimeo.com\/user123123123\",\"author_name\":\"The origional author\",\"account_type\":\"plus\"}",
"headers": {
"Content-Type": "application/json",
"Cache-Control": "max-age=60",
"Expires": "Mon, 19 Nov 2018 13:01:31 GMT",
"Content-Length": "928",
"Date": "Mon, 19 Nov 2018 13:00:31 GMT",
"Connection": "keep-alive",
"Vary": "User-Agent,Accept-Encoding"
},
"status_code": 200,
"type": "ok"
}
}
]

31 changes: 16 additions & 15 deletions lib/oembed/provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ defmodule OEmbed.Provider do
@behaviour OEmbed.Provider

defp get_oembed(url) do
with {:ok, %HTTPoison.Response{body: body}} <- HTTPoison.get(url, [], [follow_redirect: true, ssl: [{:versions, [:'tlsv1.2']}]]),
{:ok, struct} <- Poison.decode(body),
resource <- get_resource(struct) do
{:ok, resource}
else
_ -> {:error, "oEmbed url not found"}
end
end
with {:ok, %HTTPoison.Response{body: body}} <-
HTTPoison.get(url, [], follow_redirect: true, ssl: [{:versions, [:"tlsv1.2"]}]),
{:ok, struct} <- Poison.decode(body),
resource <- get_resource(struct) do
{:ok, resource}
else
_ -> {:error, "oEmbed url not found"}
end
end

defp get_resource(%{"type" => "link"} = struct), do: Link.new(struct)
defp get_resource(%{"type" => "photo"} = struct), do: Photo.new(struct)
defp get_resource(%{"type" => "rich"} = struct), do: Rich.new(struct)
defp get_resource(%{"type" => "video"} = struct), do: Video.new(struct)
defp get_resource(struct), do: struct
defp get_resource(%{"type" => "link"} = struct), do: Link.new(struct)
defp get_resource(%{"type" => "photo"} = struct), do: Photo.new(struct)
defp get_resource(%{"type" => "rich"} = struct), do: Rich.new(struct)
defp get_resource(%{"type" => "video"} = struct), do: Video.new(struct)
defp get_resource(struct), do: struct
end
end

@callback provides?(String.t) :: boolean
@callback get(String.t) :: struct
@callback provides?(String.t()) :: boolean
@callback get(String.t()) :: struct
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule OEmbed.VimeoProvider do
"""
use OEmbed.Provider

@oembed_endpoint "http://vimeo.com/api/oembed.json?url="
@oembed_endpoint "https://vimeo.com/api/oembed.json?url="

@doc """
Check if this provider supports given URL.
Expand Down
20 changes: 18 additions & 2 deletions test/oembed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,31 @@ defmodule OEmbedTest do

test "gets oembed for valid url with relative oembed api link using http" do
use_cassette "playbuzz_http_valid" do
{:ok, %Rich{} = oembed} = OEmbed.for("http://www.playbuzz.com/avibwx10/every-southerner-must-have-these-eats-at-least-once")
{:ok, %Rich{} = oembed} =
OEmbed.for(
"http://www.playbuzz.com/avibwx10/every-southerner-must-have-these-eats-at-least-once"
)

assert oembed.html =~ "pb_feed"
end
end

test "gets oembed for valid url with relative oembed api link using https" do
use_cassette "playbuzz_https_valid" do
{:ok, %Rich{} = oembed} = OEmbed.for("https://www.playbuzz.com/avibwx10/every-southerner-must-have-these-eats-at-least-once")
{:ok, %Rich{} = oembed} =
OEmbed.for(
"https://www.playbuzz.com/avibwx10/every-southerner-must-have-these-eats-at-least-once"
)

assert oembed.html =~ "pb_feed"
end
end

test "gets rich oembed for valid vimeo url" do
use_cassette "vimeo_valid", match_requests_on: [:query] do
{:ok, %Video{} = oembed} = OEmbed.for("https://player.vimeo.com/videos/123123123/")

assert oembed.html =~ "vimeo"
end
end
end

0 comments on commit a846c9e

Please sign in to comment.