Skip to content

Commit

Permalink
Apply elixir format to all files
Browse files Browse the repository at this point in the history
  • Loading branch information
r8 committed Nov 22, 2018
1 parent 7305633 commit 7e8c59e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 48 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

10 changes: 5 additions & 5 deletions lib/oembed/providers/discoverable_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ defmodule OEmbed.DiscoverableProvider do
"""
def get(url) do
with {:ok, href} <- discover(url),
{:ok, oembed} <- get_oembed(href) do
{:ok, oembed}
{:ok, oembed} <- get_oembed(href) do
{:ok, oembed}
else
_ ->
{:error, "oEmbed not found"}
end
end

defp discover(url) do
with {:ok, %HTTPoison.Response{body: html}} <- HTTPoison.get(url, [], [follow_redirect: true, ssl: [{:versions, [:'tlsv1.2']}]]),
with {:ok, %HTTPoison.Response{body: html}} <-
HTTPoison.get(url, [], follow_redirect: true, ssl: [{:versions, [:"tlsv1.2"]}]),
[_ | _] = tags <- Floki.find(html, "head link[type$='json+oembed']"),
{"link", attributes, _} <- List.first(tags),
%{"href" => href} <- Enum.into(attributes, %{}),
oembed_url = %URI{} <- URI.merge(url, href)
do
oembed_url = %URI{} <- URI.merge(url, href) do
{:ok, URI.to_string(oembed_url)}
else
_ -> {:error, "oEmbed url not found"}
Expand Down
5 changes: 4 additions & 1 deletion lib/oembed/providers/instagram_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ defmodule OEmbed.InstagramProvider do
Check if this provider supports given URL.
"""
def provides?(url) do
Regex.match?(~r/^(?:http|https):\/\/(?:www\.)?(?:instagram.com|instagr.am)\/(?:[A-Za-z0-9-_\/]+)$/i, url)
Regex.match?(
~r/^(?:http|https):\/\/(?:www\.)?(?:instagram.com|instagr.am)\/(?:[A-Za-z0-9-_\/]+)$/i,
url
)
end

@doc """
Expand Down
22 changes: 13 additions & 9 deletions lib/oembed/providers/pinterest_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@ defmodule OEmbed.PinterestProvider do
def get(url) do
case get_page_type(url) do
{:ok, "pin"} ->
oembed = Rich.new(%{
html: get_pin_html(url)
})
oembed =
Rich.new(%{
html: get_pin_html(url)
})

{:ok, oembed}

_ ->
{:error, "Error getting oEmbed"}
end
end

defp get_page_type(url) do
with {:ok, %HTTPoison.Response{body: html}} <- HTTPoison.get(url, [], [follow_redirect: true, ssl: [{:versions, [:'tlsv1.2']}]]),
[_ | _] = tags <- Floki.find(html, "head meta[property='og:type']"),
{"meta", attributes, _} <- List.first(tags),
%{"content" => content} <- Enum.into(attributes, %{}),
%{"type" => type} <- Regex.named_captures(~r/^pinterestapp\:(?<type>.+)/, content) do
{:ok, type}
with {:ok, %HTTPoison.Response{body: html}} <-
HTTPoison.get(url, [], follow_redirect: true, ssl: [{:versions, [:"tlsv1.2"]}]),
[_ | _] = tags <- Floki.find(html, "head meta[property='og:type']"),
{"meta", attributes, _} <- List.first(tags),
%{"content" => content} <- Enum.into(attributes, %{}),
%{"type" => type} <- Regex.named_captures(~r/^pinterestapp\:(?<type>.+)/, content) do
{:ok, type}
else
_ -> {:error, "Error getting page type"}
end
Expand Down
18 changes: 13 additions & 5 deletions lib/oembed/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ defmodule OEmbed.Resource do
quote do
@derive [Poison.Encoder]

@common_keys [type: nil, version: "1.0", title: nil,
author_name: nil, author_url: nil,
provider_name: nil, provider_url: nil, cache_age: nil,
thumbnail_url: nil, thumbnail_width: nil,
thumbnail_height: nil]
@common_keys [
type: nil,
version: "1.0",
title: nil,
author_name: nil,
author_url: nil,
provider_name: nil,
provider_url: nil,
cache_age: nil,
thumbnail_url: nil,
thumbnail_width: nil,
thumbnail_height: nil
]

defstruct @common_keys ++ @keys

Expand Down
66 changes: 38 additions & 28 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ defmodule OEmbed.Mixfile do
@version "0.3.0"

def project do
[app: :oembed,
version: @version,
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
description: description(),
package: package(),
docs: docs(),
deps: deps()]
[
app: :oembed,
version: @version,
elixir: "~> 1.3",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
description: description(),
package: package(),
docs: docs(),
deps: deps()
]
end

# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[applications: [:httpoison, :exconstructor],
extra_applications: [:logger],
env: [providers: []]]
[
applications: [:httpoison, :exconstructor],
extra_applications: [:logger],
env: [providers: []]
]
end

# Dependencies can be Hex packages:
Expand All @@ -34,22 +38,26 @@ defmodule OEmbed.Mixfile do
#
# Type "mix help deps" for more examples and options
defp deps do
[{:httpoison, ">= 0.9.0"},
{:floki, ">= 0.9.0"},
{:poison, ">= 1.5.0"},
{:exconstructor, ">= 1.0.0"},
{:exvcr, "~> 0.9", only: :test},
{:inch_ex, ">= 0.0.0", only: :docs},
{:earmark, ">= 0.0.0", only: :dev},
{:ex_doc, "~> 0.16", only: :dev, runtime: false},
{:credo, "~> 0.8", only: [:dev, :test], runtime: false}]
[
{:httpoison, ">= 0.9.0"},
{:floki, ">= 0.9.0"},
{:poison, ">= 1.5.0"},
{:exconstructor, ">= 1.0.0"},
{:exvcr, "~> 0.9", only: :test},
{:inch_ex, ">= 0.0.0", only: :docs},
{:earmark, ">= 0.0.0", only: :dev},
{:ex_doc, "~> 0.16", only: :dev, runtime: false},
{:credo, "~> 0.8", only: [:dev, :test], runtime: false}
]
end

defp docs do
[source_url: "https://github.com/r8/elixir-oembed",
source_ref: "v#{@version}",
main: "readme",
extras: ["README.md"]]
[
source_url: "https://github.com/r8/elixir-oembed",
source_ref: "v#{@version}",
main: "readme",
extras: ["README.md"]
]
end

defp description do
Expand All @@ -59,8 +67,10 @@ defmodule OEmbed.Mixfile do
end

defp package do
[maintainers: ["Sergey Storchay"],
licenses: ["MIT"],
links: %{"Github" => "https://github.com/r8/elixir-oembed"}]
[
maintainers: ["Sergey Storchay"],
licenses: ["MIT"],
links: %{"Github" => "https://github.com/r8/elixir-oembed"}
]
end
end

0 comments on commit 7e8c59e

Please sign in to comment.