-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
40 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,92 @@ | ||
# https://docs.betradar.com/pages/viewpage.action?pageId=116065083 | ||
defmodule UOF.API.Sports.Category do | ||
@moduledoc """ | ||
Categories are a generic classification term Betradar uses to subclassify a | ||
particular sport (e.g. for Tennis the categories can be ATP-Tour, WTA-Tour, | ||
David Cup, etc., for soccer the categories are the various countries). | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
import UOF.API.EctoHelpers | ||
|
||
@doc """ | ||
List all the available categories for the given sport. | ||
""" | ||
@spec by_sport(sport :: String.t(), lang :: String.t()) :: list(__MODULE__.t()) | ||
def by_sport(sport, lang \\ "en") do | ||
case UOF.API.get("/sports/#{lang}/sports/#{sport}/categories.xml") do | ||
{:ok, %_{status: 200, body: resp}} -> | ||
resp | ||
|> Map.get("sport_categories") | ||
|> changeset | ||
|
||
# |> Map.get("categories") | ||
# |> Map.get("category") | ||
# |> Enum.map(fn x -> | ||
# {:ok, x} = changeset(x) | ||
# x | ||
# end) | ||
|
||
{:error, _} = error -> | ||
error | ||
end | ||
end | ||
|
||
@type sport :: %UOF.API.Sports.Category.Sport{ | ||
id: String.t(), | ||
name: String.t() | ||
} | ||
|
||
@type category :: %UOF.API.Sports.Category.Category{ | ||
id: String.t(), | ||
name: String.t(), | ||
country_code: String.t() | ||
} | ||
|
||
@type t :: %__MODULE__{ | ||
sport: sport(), | ||
categories: list(category()) | ||
} | ||
|
||
@primary_key false | ||
|
||
embedded_schema do | ||
embeds_one :sport, Sport, primary_key: false do | ||
field :id, :string | ||
field :name, :string | ||
end | ||
|
||
embeds_many :categories, Category, primary_key: false do | ||
field :id, :string | ||
field :name, :string | ||
field :country_code, :string | ||
end | ||
end | ||
|
||
@doc false | ||
def changeset(model \\ %__MODULE__{}, params) | ||
|
||
def changeset(%__MODULE__{} = model, params) do | ||
params = bubble_up(params, "categories", "category") | ||
|
||
model | ||
|> cast(params, []) | ||
|> cast_embed(:sport, with: &changeset/2) | ||
|> cast_embed(:categories, with: &changeset/2) | ||
|> apply | ||
end | ||
|
||
def changeset(%UOF.API.Sports.Category.Sport{} = model, params) do | ||
params = sanitize(params) | ||
|
||
model | ||
|> cast(params, [:id, :name]) | ||
end | ||
|
||
def changeset(%UOF.API.Sports.Category.Category{} = model, params) do | ||
params = sanitize(params) | ||
|
||
model | ||
|> cast(params, [:id, :name, :country_code]) | ||
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,31 @@ | ||
defmodule UOF.API.Sports.Category.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/categories.xml") | ||
%Tesla.Env{status: 200, headers: [{"content-type", "application/xml"}], body: resp} | ||
end) | ||
|
||
:ok | ||
end | ||
|
||
test "can parse UOF.API.Sports.Category.by_sport/{1, 2} response" do | ||
{:ok, sport_categories} = UOF.API.Sports.Category.by_sport("sr:sport:1") | ||
|
||
# sport | ||
sport = sport_categories.sport | ||
assert sport.id == "sr:sport:1" | ||
assert sport.name == "Soccer" | ||
# categories | ||
categories = sport_categories.categories | ||
category = hd(categories) | ||
assert Enum.count(categories) == 224 | ||
assert category.id == "sr:category:1" | ||
assert category.name == "England" | ||
assert category.country_code == "ENG" | ||
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