Skip to content

Commit

Permalink
feat(Rest): add get_guild_vanity_invite/1
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC committed Nov 9, 2019
1 parent 6adecb3 commit 9b6dc0b
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 3 deletions.
24 changes: 24 additions & 0 deletions lib/mix/tasks/bangify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Mix.Tasks.Bangify do
defmacro __using__(:functions) do
quote location: :keep do
require Version
__functions__
end
end
Expand All @@ -30,13 +31,15 @@ defmodule Mix.Tasks.Bangify do
@callback_template """
@doc "The same as \`c:__name__/__arity__\`, but raises an exception if it fails."
__version__
__maybe_deprecated__
__callback__
"""

@function_template """
@doc "See \`c:Crux.Rest.__name__/__arity__\`"
__maybe_version__
__maybe_spec__
__maybe_deprecated__
def __name__(__arguments_with_defaults__) do
Crux.Rest.Functions.__name__(__arguments__)
|> Crux.Rest.apply_options(@opts)
Expand All @@ -46,6 +49,7 @@ defmodule Mix.Tasks.Bangify do
@doc "The same as \`c:Crux.Rest.__name__/__arity__\`, but raises an exception if it fails."
__maybe_version__
__maybe_spec!__
__maybe_deprecated__
def __name__!(__arguments_with_defaults__) do
Crux.Rest.Functions.__name__(__arguments__)
|> Crux.Rest.apply_options(@opts)
Expand Down Expand Up @@ -113,11 +117,21 @@ defmodule Mix.Tasks.Bangify do
version = Map.get(meta, :since) || raise "Missing since for #{name}/#{arity}"
version = ~s{Version.since("#{version}")}

maybe_deprecated =
case meta do
%{deprecated: deprecated} ->
~s{Version.deprecated("#{deprecated}")}

_ ->
""
end

callback =
@callback_template
|> String.replace("__name__", name |> to_string())
|> String.replace("__arity__", arity |> to_string())
|> String.replace("__version__", version)
|> String.replace("__maybe_deprecated__", maybe_deprecated)
|> String.replace("__callback__", callback)

optional = "#{Atom.to_string(name)}!: #{arity}"
Expand All @@ -143,6 +157,15 @@ defmodule Mix.Tasks.Bangify do
{"", ""}
end

maybe_deprecated =
case meta do
%{deprecated: deprecated} ->
~s{Version.deprecated("#{deprecated}")}

_ ->
""
end

since =
case meta do
%{since: since} ->
Expand All @@ -161,6 +184,7 @@ defmodule Mix.Tasks.Bangify do
@function_template
|> String.replace("__maybe_spec__", maybe_spec)
|> String.replace("__maybe_spec!__", maybe_spec!)
|> String.replace("__maybe_deprecated__", maybe_deprecated)
|> String.replace("__name__", name |> to_string())
|> String.replace("__arity__", arity |> to_string())
|> String.replace("__arguments_with_defaults__", signature)
Expand Down
8 changes: 8 additions & 0 deletions lib/rest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1379,10 +1379,18 @@ defmodule Crux.Rest do
Gets the vanity url of a guild, if any
"""
Version.since("0.2.0")
Version.deprecated("Use get_guild_vanity_invite/1 instead")

@callback get_guild_vanity_url(guild :: Guild.id_resolvable()) ::
{:ok, String.t()} | {:error, term()}

@doc """
Gets the vanity invite of a guild, if any
"""
Version.since("0.2.1")
@callback get_guild_vanity_invite(guild :: Guild.id_resolvable()) ::
{:ok, Invite.t()} | {:error, term()}

### End Guild

### Start Webhook
Expand Down
18 changes: 16 additions & 2 deletions lib/rest/functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ defmodule Crux.Rest.Functions do

@behaviour Crux.Rest

alias Crux.Rest.{Endpoints, Request, Util}
alias Crux.Rest.{Endpoints, Request, Util, Version}

require Version

alias Crux.Structs.{
AuditLog,
Expand Down Expand Up @@ -937,6 +939,7 @@ defmodule Crux.Rest.Functions do
|> Request.set_transform(&Structs.Util.raw_data_to_map(&1, Invite, :code))
end

Version.deprecated("Use get_guild_vanity_invite/1 instead")
@impl true
def get_guild_vanity_url(guild) do
guild_id = resolve_not_nil(guild, Guild)
Expand All @@ -945,7 +948,18 @@ defmodule Crux.Rest.Functions do

:get
|> Request.new(path)
|> Request.set_transform(&Map.get(&1, :code))
|> Request.set_transform(&Map.get(&1, "code"))
end

@impl true
def get_guild_vanity_invite(guild) do
guild_id = resolve_not_nil(guild, Guild)

path = Endpoints.guild_invites(guild_id)

:get
|> Request.new(path)
|> Request.set_transform(Invite)
end

### End Guild Invite
Expand Down

0 comments on commit 9b6dc0b

Please sign in to comment.