mirror of
https://github.com/amineo/t2_server_query_elixir.git
synced 2026-04-28 23:55:23 +00:00
refactor(spec): add type specs
This commit is contained in:
parent
0c785ccb39
commit
a500fea095
2 changed files with 8 additions and 4 deletions
|
|
@ -70,6 +70,7 @@ defmodule T2ServerQuery do
|
||||||
}}
|
}}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@spec query(String.t(), integer(), integer()) :: {atom(), %T2ServerQuery.QueryResult{}}
|
||||||
def query(server_ip, port \\ 28_000, timeout \\ 3_500) do
|
def query(server_ip, port \\ 28_000, timeout \\ 3_500) do
|
||||||
Logger.info "query: #{server_ip}"
|
Logger.info "query: #{server_ip}"
|
||||||
case is_valid_ip?(server_ip) do
|
case is_valid_ip?(server_ip) do
|
||||||
|
|
@ -78,9 +79,8 @@ defmodule T2ServerQuery do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec handle_query(String.t(), integer(), integer()) :: {atom(), %T2ServerQuery.QueryResult{}}
|
||||||
defp handle_query(server_ip, port, timeout) do
|
defp handle_query(server_ip, port, timeout) do
|
||||||
|
|
||||||
{:ok, socket} = :gen_udp.open(0, [:binary, {:active, false}])
|
{:ok, socket} = :gen_udp.open(0, [:binary, {:active, false}])
|
||||||
|
|
||||||
# Convert a string ip from "127.0.0.1" into {127, 0, 0, 1}
|
# Convert a string ip from "127.0.0.1" into {127, 0, 0, 1}
|
||||||
|
|
@ -106,7 +106,7 @@ defmodule T2ServerQuery do
|
||||||
PacketParser.init(hex_info_packet, hex_status_packet)
|
PacketParser.init(hex_info_packet, hex_status_packet)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec is_valid_ip?(any()) :: boolean()
|
||||||
defp is_valid_ip?(nil), do: false
|
defp is_valid_ip?(nil), do: false
|
||||||
defp is_valid_ip?(server_ip) do
|
defp is_valid_ip?(server_ip) do
|
||||||
case Regex.match?(~r/^([1-2]?[0-9]{1,2}\.){3}([1-2]?[0-9]{1,2})$/, server_ip) do
|
case Regex.match?(~r/^([1-2]?[0-9]{1,2}\.){3}([1-2]?[0-9]{1,2})$/, server_ip) do
|
||||||
|
|
@ -116,6 +116,7 @@ defmodule T2ServerQuery do
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@spec handle_udp_response(tuple(), String.t(), integer()) :: tuple() | String.t()
|
||||||
defp handle_udp_response({:ok, {_ip, port, packet}}, _server_ip, port) do
|
defp handle_udp_response({:ok, {_ip, port, packet}}, _server_ip, port) do
|
||||||
packet
|
packet
|
||||||
|> Base.encode16
|
|> Base.encode16
|
||||||
|
|
|
||||||
|
|
@ -43,9 +43,9 @@ defmodule T2ServerQuery.PacketParser do
|
||||||
|
|
||||||
Refer to `T2ServerQuery.QueryResult` for what a typical struct would look like.
|
Refer to `T2ServerQuery.QueryResult` for what a typical struct would look like.
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
alias T2ServerQuery.QueryResult
|
alias T2ServerQuery.QueryResult
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
@ -53,6 +53,7 @@ defmodule T2ServerQuery.PacketParser do
|
||||||
Normally you wouldn't need to run this function manually since it's called in a pipeline from the main `T2ServerQuery.query`
|
Normally you wouldn't need to run this function manually since it's called in a pipeline from the main `T2ServerQuery.query`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@spec init({:error, String.t()}, any()) :: {:error, map()}
|
||||||
def init({:error, host}, _) do
|
def init({:error, host}, _) do
|
||||||
results = %QueryResult{}
|
results = %QueryResult{}
|
||||||
|
|
||||||
|
|
@ -65,6 +66,7 @@ defmodule T2ServerQuery.PacketParser do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec init(binary(), binary()) :: {:ok, %QueryResult{}}
|
||||||
def init(info_packet, status_packet) when is_binary(info_packet) and is_binary(status_packet) do
|
def init(info_packet, status_packet) when is_binary(info_packet) and is_binary(status_packet) do
|
||||||
|
|
||||||
info_results = info_packet
|
info_results = info_packet
|
||||||
|
|
@ -81,6 +83,7 @@ defmodule T2ServerQuery.PacketParser do
|
||||||
pack_results({:ok, status_results, info_results})
|
pack_results({:ok, status_results, info_results})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec pack_results({:ok, map(), map()}) :: {:ok, %QueryResult{}}
|
||||||
defp pack_results({:ok, status_results, info_results}) do
|
defp pack_results({:ok, status_results, info_results}) do
|
||||||
results = %QueryResult{}
|
results = %QueryResult{}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue