From 93422ad6fa07428391ef64a648eacba568a13b1e Mon Sep 17 00:00:00 2001 From: Enrique Fernandez Date: Sun, 12 May 2024 19:13:27 +0200 Subject: [PATCH] sports --- lib/uof/api/mappings/sports.ex | 10 ------- lib/uof/api/sports.ex | 9 ------ lib/uof/api/sports/sport.ex | 47 ++++++++++++++++++++++++++++++ test/uof/api/sports/sport_test.exs | 23 +++++++++++++++ test/uof/api/sports_test.exs | 14 --------- 5 files changed, 70 insertions(+), 33 deletions(-) delete mode 100644 lib/uof/api/mappings/sports.ex create mode 100644 lib/uof/api/sports/sport.ex create mode 100644 test/uof/api/sports/sport_test.exs diff --git a/lib/uof/api/mappings/sports.ex b/lib/uof/api/mappings/sports.ex deleted file mode 100644 index b63ae9c..0000000 --- a/lib/uof/api/mappings/sports.ex +++ /dev/null @@ -1,10 +0,0 @@ -defmodule UOF.API.Mappings.Sports do - @moduledoc false - use Saxaboom.Mapper - - alias UOF.API.Mappings.Sport - - document do - elements(:sport, as: :sports, into: %Sport{}) - end -end diff --git a/lib/uof/api/sports.ex b/lib/uof/api/sports.ex index c45a213..dfab5f3 100644 --- a/lib/uof/api/sports.ex +++ b/lib/uof/api/sports.ex @@ -130,15 +130,6 @@ defmodule UOF.API.Sports do HTTP.get(%UOF.API.Mappings.Timeline{}, endpoint) end - @doc """ - List all the available sports. - """ - def sports(lang \\ "en") do - endpoint = ["sports", lang, "sports.xml"] - - HTTP.get(%UOF.API.Mappings.Sports{}, endpoint) - end - def tournaments(lang \\ "en") do endpoint = ["sports", lang, "tournaments.xml"] diff --git a/lib/uof/api/sports/sport.ex b/lib/uof/api/sports/sport.ex new file mode 100644 index 0000000..7ae491e --- /dev/null +++ b/lib/uof/api/sports/sport.ex @@ -0,0 +1,47 @@ +defmodule UOF.API.Sports.Sport do + @moduledoc """ + """ + use Ecto.Schema + import Ecto.Changeset + import UOF.API.EctoHelpers + + @doc """ + List all the available sports. + """ + @spec all(lang :: String.t()) :: list(__MODULE__.t()) + def all(lang \\ "en") do + case UOF.API.get("/sports/#{lang}/sports.xml") do + {:ok, %_{status: 200, body: resp}} -> + resp + |> Map.get("sports") + |> Map.get("sport") + |> Enum.map(fn x -> + {:ok, x} = changeset(x) + x + end) + + {:error, _} = error -> + error + end + end + + @type t :: %__MODULE__{ + id: String.t(), + name: String.t() + } + + @primary_key false + + embedded_schema do + field :id, :string + field :name, :string + end + + def changeset(model \\ %__MODULE__{}, params) do + params = sanitize(params) + + model + |> cast(params, [:id, :name]) + |> apply + end +end diff --git a/test/uof/api/sports/sport_test.exs b/test/uof/api/sports/sport_test.exs new file mode 100644 index 0000000..17d15c9 --- /dev/null +++ b/test/uof/api/sports/sport_test.exs @@ -0,0 +1,23 @@ +defmodule UOF.API.Sports.Sport.Test do + use ExUnit.Case + + setup do + :ok = Application.put_env(:tesla, UOF.API, adapter: Tesla.Mock) + + Tesla.Mock.mock(fn + %{method: :get} -> + resp = File.read!("test/data/sports.xml") + %Tesla.Env{status: 200, headers: [{"content-type", "application/xml"}], body: resp} + end) + + :ok + end + + test "can parse UOF.API.Sports.Sport.all/{0, 1} response" do + sports = UOF.API.Sports.Sport.all() + + assert Enum.count(sports) == 204 + assert hd(sports).id == "sr:sport:143" + assert hd(sports).name == "7BallRun" + end +end diff --git a/test/uof/api/sports_test.exs b/test/uof/api/sports_test.exs index 7842346..cea582c 100644 --- a/test/uof/api/sports_test.exs +++ b/test/uof/api/sports_test.exs @@ -39,10 +39,6 @@ defmodule UOF.API.Sports.Test do {:ok, File.read!("test/data/tournaments.xml")} end - defp fetch_mock_data(["sports", _lang, "sports.xml"]) do - {:ok, File.read!("test/data/sports.xml")} - end - defp fetch_mock_data(["sports", _lang, "tournaments", _tournament, "info.xml"]) do {:ok, File.read!("test/data/tournament_info.xml")} end @@ -451,16 +447,6 @@ defmodule UOF.API.Sports.Test do assert season_coverage.min_coverage_level == "silver" end - test "can parse UOF.API.Sports.sports/{0, 1} response" do - {:ok, data} = UOF.API.Sports.sports() - - sport = hd(data.sports) - - assert Enum.count(data.sports) == 204 - assert sport.id == "sr:sport:143" - assert sport.name == "7BallRun" - end - test "can parse UOF.API.Sports.tournament/{1, 2} response" do {:ok, info} = UOF.API.Sports.tournament("sr:tournament:7")