-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from felipelincoln/dev
Release v0.8.0
- Loading branch information
Showing
61 changed files
with
2,480 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/_build/ | ||
/deps/ | ||
/cover/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alias Publishing.Repo | ||
alias Publishing.{Article, Blog} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Used by "mix format" | ||
[ | ||
import_deps: [:ecto, :phoenix], | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
blog-*.tar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Publishing | ||
|
||
**TODO: Add description** | ||
|
||
## Installation | ||
|
||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed | ||
by adding `publishing` to your list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:publishing, "~> 0.1.0"} | ||
] | ||
end | ||
``` | ||
|
||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) | ||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can | ||
be found at [https://hexdocs.pm/publishing](https://hexdocs.pm/publishing). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"skip_files": [ | ||
"lib/publishing/application.ex", | ||
"lib/publishing/release.ex", | ||
"lib/publishing/repo.ex", | ||
"test/support" | ||
], | ||
"coverage_options": { | ||
"treat_no_relevant_lines_as_covered": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Publishing.Application do | ||
@moduledoc false | ||
|
||
use Application | ||
|
||
def start(_type, _args) do | ||
children = [Publishing.Repo] | ||
|
||
Supervisor.start_link(children, strategy: :one_for_one, name: Publishing.Supervisor) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Publishing.Helper do | ||
@moduledoc """ | ||
Helpers for the publishing application. | ||
""" | ||
|
||
@current_year Date.utc_today().year | ||
@last_year @current_year - 1 | ||
|
||
@doc """ | ||
Formats a datetime into "Month. day" format | ||
Examples | ||
iex> format_date(~D[2021-06-15]) | ||
"Jun 15" | ||
iex> format_date(~D[2020-06-21]) | ||
"Last year" | ||
iex> format_date(~D[2019-12-12]) | ||
"2 years ago" | ||
""" | ||
def format_date(%{year: @current_year} = datetime) do | ||
Timex.format!(datetime, "%b %e", :strftime) | ||
end | ||
|
||
def format_date(%{year: @last_year}) do | ||
"Last year" | ||
end | ||
|
||
def format_date(%{year: year}) do | ||
n = @current_year - year | ||
"#{n} years ago" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Publishing.Integration do | ||
@moduledoc """ | ||
Integrations modules extract information from code hosting platforms. | ||
""" | ||
|
||
alias Publishing.Integration.Github | ||
|
||
@callback get_content(String.t()) :: {:ok, String.t()} | {:error, integer} | ||
@callback get_username(String.t()) :: {:ok, String.t() | {:error, :username}} | ||
@callback get_blog_data(String.t()) :: {:ok, map} | {:error, :blog} | ||
|
||
@doc """ | ||
Returns the `url`'s integration module. | ||
## Currently supported integrations | ||
* Github: `Publishing.Integration.Github` | ||
Examples: | ||
iex> service("https://github.com/teste") | ||
{:ok, Publishing.Integration.Github} | ||
iex> service("https://gitlab.com/teste") | ||
{:error, :integration} | ||
""" | ||
@spec service(String.t()) :: {:ok, module} | {:error, :integration} | ||
def service(url) do | ||
case URI.parse(url) do | ||
%URI{host: "github.com"} -> {:ok, Github} | ||
_ -> {:error, :integration} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
defmodule Publishing.Integration.Github do | ||
@moduledoc """ | ||
Integrates with github. | ||
""" | ||
|
||
use Tesla | ||
|
||
@behaviour Publishing.Integration | ||
|
||
plug Tesla.Middleware.BaseUrl, "https://api.github.com/" | ||
plug Tesla.Middleware.Headers, [{"Authorization", "Bearer #{token()}"}] | ||
plug Tesla.Middleware.Headers, [{"User-Agent", "branchpage"}] | ||
plug Tesla.Middleware.JSON | ||
|
||
defp token, do: Application.get_env(:publishing, __MODULE__)[:token] | ||
|
||
def get_blog_data(username) do | ||
body = %{ | ||
query: "query($login: String!){user(login: $login){bio name avatarUrl}}", | ||
variables: %{login: username} | ||
} | ||
|
||
case post("graphql", body) do | ||
{:ok, %{status: 200, body: response}} -> | ||
%{ | ||
"data" => %{ | ||
"user" => %{ | ||
"name" => name, | ||
"bio" => bio, | ||
"avatarUrl" => avatar_url | ||
} | ||
} | ||
} = response | ||
|
||
data = %{fullname: name, bio: bio, avatar_url: avatar_url} | ||
|
||
{:ok, data} | ||
|
||
_ -> | ||
{:error, :blog} | ||
end | ||
end | ||
|
||
@doc """ | ||
Returns the GitHub username from the `url`. | ||
Examples: | ||
iex> get_username("https://github.com/felipelincoln/branchpage/blob/main/README.md") | ||
{:ok, "felipelincoln"} | ||
iex> get_username("https://github.com/") | ||
{:error, :username} | ||
""" | ||
@spec get_username(String.t()) :: {:ok, String.t()} | {:error, :username} | ||
def get_username(url) when is_binary(url) do | ||
case decompose(url) do | ||
[username, _] -> {:ok, username} | ||
[] -> {:error, :username} | ||
end | ||
end | ||
|
||
@doc """ | ||
Retrieve the raw content of a resource's `url` from github. | ||
""" | ||
@spec get_content(String.t()) :: {:ok, Stream.t()} | {:error, integer} | ||
def get_content(""), do: {:error, 404} | ||
|
||
def get_content(url) when is_binary(url) do | ||
raw = | ||
url | ||
|> decompose() | ||
|> raw_url() | ||
|
||
case Tesla.get(raw) do | ||
{:ok, %{status: 200, body: body}} -> | ||
{:ok, body} | ||
|
||
{:ok, %{status: code}} -> | ||
{:error, code} | ||
end | ||
end | ||
|
||
defp decompose(url) do | ||
with %URI{path: path} when is_binary(path) <- URI.parse(url), | ||
["", username, repository, "blob" | tail] <- String.split(path, "/"), | ||
resource <- Enum.join([repository] ++ tail, "/"), | ||
true <- not_empty_string(username), | ||
true <- not_empty_string(resource) do | ||
[username, resource] | ||
else | ||
_ -> [] | ||
end | ||
end | ||
|
||
defp raw_url([username, resource]) do | ||
"https://raw.githubusercontent.com/#{username}/#{resource}" | ||
end | ||
|
||
defp raw_url([]), do: "" | ||
|
||
defp not_empty_string(str), do: is_binary(str) and str != "" | ||
end |
Oops, something went wrong.