Skip to content

Commit

Permalink
sports
Browse files Browse the repository at this point in the history
  • Loading branch information
efcasado committed May 12, 2024
1 parent e7b2678 commit 93422ad
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 33 deletions.
10 changes: 0 additions & 10 deletions lib/uof/api/mappings/sports.ex

This file was deleted.

9 changes: 0 additions & 9 deletions lib/uof/api/sports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
47 changes: 47 additions & 0 deletions lib/uof/api/sports/sport.ex
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions test/uof/api/sports/sport_test.exs
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions test/uof/api/sports_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 93422ad

Please sign in to comment.