Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify logic using req #3

Merged
merged 5 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 17 additions & 30 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,33 @@ on:
- main

jobs:
check-format:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1
- uses: erlef/setup-beam@v1
with:
otp-version: 24.0.1
elixir-version: 1.12.1
- run: mix deps.get
- run: mix format --check-formatted

test:
name: OTP ${{matrix.erlang}} / Elixir ${{matrix.elixir}}
name: OTP ${{matrix.pair.otp}} / Elixir ${{matrix.pair.elixir}}
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
include:
- erlang: 24.0.1
elixir: 1.12.1
- erlang: 23.3
elixir: 1.11.4
- erlang: 23.3
elixir: 1.10.4
- erlang: 22.3
elixir: 1.9.4
- erlang: 21.3
elixir: 1.8.2
- erlang: 21.3
elixir: 1.7.4
- pair:
elixir: "1.13"
otp: "24.3.4.10"
- pair:
elixir: "1.17"
otp: "27.0.1"
lint: lint

env:
MIX_ENV: test
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
- uses: actions/checkout@v4
- uses: erlef/setup-beam@main
with:
otp-version: ${{matrix.erlang}}
elixir-version: ${{matrix.elixir}}
- run: mix deps.get
- run: mix test
otp-version: ${{ matrix.pair.otp }}
elixir-version: ${{ matrix.pair.elixir }}
version-type: strict

- run: mix deps.get --check-locked
- run: mix format --check-formatted
- run: mix test --slowest 5
- run: mix coveralls.github
9 changes: 9 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Config

if config_env() == :test do
config :ogp,
req_options: [
plug: {Req.Test, MyStub},
retry: false
]
end
36 changes: 19 additions & 17 deletions lib/open_graph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,30 @@ defmodule OpenGraph do
"""

@spec fetch(String.t()) :: {:ok, OpenGraph.t()} | {:error, OpenGraph.Error.t()}
def fetch(url) do
case Finch.build(:get, url) |> Finch.request(OpenGraph.Finch) do
{:ok, %Finch.Response{status: status} = response} when status in 200..299 ->
{:ok, parse(response.body)}
def fetch(url, req_options \\ []) do
options = Keyword.merge(req_options, Application.get_env(:ogp, :req_options, []))

{:ok, %Finch.Response{status: status} = response} when status in 300..399 ->
case List.keyfind(response.headers, "location", 0) do
{_, location} ->
fetch(location)
case Req.get(url, options) do
{:ok, %Req.Response{status: status} = response} when status in 200..299 ->
{:ok, parse(response.body)}

nil ->
reason = {:missing_redirect_location, status}
{:error, %OpenGraph.Error{reason: reason}}
end
{:ok, %Req.Response{status: status}} when status in 300..399 ->
{:error,
%OpenGraph.Error{
reason: {:missing_redirect_location, status}
}}

{:ok, %Finch.Response{status: status}} ->
reason = {:unexpected_status_code, status}
{:error, %OpenGraph.Error{reason: reason}}
{:ok, %Req.Response{status: status}} ->
{:error,
%OpenGraph.Error{
reason: {:unexpected_status_code, status}
}}

{:error, error} ->
reason = {:request_error, Exception.message(error)}
{:error, %OpenGraph.Error{reason: reason}}
{:error,
%OpenGraph.Error{
reason: {:request_error, Exception.message(error)}
}}
end
end

Expand Down
19 changes: 0 additions & 19 deletions lib/open_graph/application.ex

This file was deleted.

15 changes: 7 additions & 8 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule OpenGraph.MixProject do
[
app: :ogp,
version: "1.0.2",
elixir: "~> 1.7",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps(),
test_coverage: [tool: ExCoveralls],
Expand Down Expand Up @@ -37,19 +37,18 @@ defmodule OpenGraph.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {OpenGraph.Application, []}
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ex_doc, "~> 0.24", only: :dev, runtime: false},
{:bypass, "~> 2.1", only: :test},
{:excoveralls, "~> 0.10", only: :test},
{:floki, "~> 0.27"},
{:finch, "~> 0.6"}
{:req, "~> 0.5.6"},
{:floki, "~> 0.36.2"},
{:plug, "~> 1.16", only: :test},
{:excoveralls, "~> 0.18.2", only: :test},
{:ex_doc, "~> 0.34.2", only: :docs}
]
end
end
Loading