Skip to content

Commit

Permalink
Add DataSchema, docs, tests, config improvements - CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luk-pau-es committed Dec 16, 2024
1 parent b9472f1 commit 3ffd4cf
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/uof_feed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule UofFeed do
"""
@spec connect_and_subscribe(
environemnt :: atom(),
environment :: atom(),
amqp_token :: String.t(),
bookmaker_id :: integer(),
handler :: function()
Expand Down
6 changes: 3 additions & 3 deletions lib/uof_feed/amqp/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule UofFeed.AMQP.Config do
@doc ~S"""
Returns URI and connection options based on provided information.
Available environemnts:
Available environments:
- `:production`
- `:integration` (default)
- `:replay`
Expand All @@ -32,7 +32,7 @@ defmodule UofFeed.AMQP.Config do
- `:proxy_singapore`
- `:proxy_tokyo`
Providing environemnt from outside of above list will result in response `{:error, :invalid_environment}`
Providing environment from outside of above list will result in response `{:error, :invalid_environment}`
## Examples
Expand All @@ -43,7 +43,7 @@ defmodule UofFeed.AMQP.Config do
{:error, :invalid_environment}
"""
@spec amqp_opts(
environemnt :: atom(),
environment :: atom(),
amqp_token :: String.t(),
bookmaker_id :: integer(),
integer()
Expand Down
45 changes: 20 additions & 25 deletions lib/uof_feed/data_schema/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,49 @@ defmodule UofFeed.DataSchema.Utils do
## Examples
iex> UofFeed.DataSchema.Utils.to_text(~c"test")
iex> UofFeed.DataSchema.Utils.to_text("test")
{:ok, "test"}
"""
@spec to_text(data :: charlist()) :: {:ok, String.t()}
def to_text(data), do: {:ok, to_string(data)}
@spec to_text(data :: String.t()) :: {:ok, String.t()}
def to_text(data), do: {:ok, data}

@doc ~S"""
Convert provided charlist to float represented by Decimal.
Convert provided charlist containing a numeral to a Decimal.
## Examples
iex> UofFeed.DataSchema.Utils.to_decimal(~c"1.12")
iex> UofFeed.DataSchema.Utils.to_decimal("1.12")
{:ok, Decimal.new("1.12")}
iex> UofFeed.DataSchema.Utils.to_decimal(~c"invalid")
{:error, "Invalid data provided, expected float as string, received: invalid"}
iex> UofFeed.DataSchema.Utils.to_decimal("invalid")
{:error, "Invalid data provided, expected numeral as string, received: invalid"}
"""
@spec to_decimal(data :: charlist()) :: {:ok, Decimal.t()} | {:error, String.t()}
@spec to_decimal(data :: String.t()) :: {:ok, Decimal.t()} | {:error, String.t()}
def to_decimal(data) do
decimal =
data
|> to_string()
|> maybe_convert(:decimal)
maybe_convert(data, :decimal)

{:ok, decimal}
rescue
Decimal.Error ->
{:error, "Invalid data provided, expected float as string, received: #{data}"}
{:error, "Invalid data provided, expected numeral as string, received: #{data}"}
end

@doc ~S"""
Convert provided charlist to integer value.
## Examples
iex> UofFeed.DataSchema.Utils.to_integer(~c"123")
iex> UofFeed.DataSchema.Utils.to_integer("123")
{:ok, 123}
iex> UofFeed.DataSchema.Utils.to_integer(~c"invalid")
iex> UofFeed.DataSchema.Utils.to_integer("invalid")
{:error, "Invalid data provided, expected integer as string, received: invalid"}
"""
@spec to_integer(data :: charlist()) :: {:ok, integer()} | {:error, String.t()}
@spec to_integer(data :: String.t()) :: {:ok, integer()} | {:error, String.t()}
def to_integer(data) do
number =
data
|> to_string()
|> maybe_convert(:integer)
number = maybe_convert(data, :integer)

{:ok, number}
rescue
Expand All @@ -74,16 +69,16 @@ defmodule UofFeed.DataSchema.Utils do
## Examples
iex> UofFeed.DataSchema.Utils.to_boolean(~c'true')
iex> UofFeed.DataSchema.Utils.to_boolean("true")
{:ok, true}
iex> UofFeed.DataSchema.Utils.to_boolean(~c'false')
iex> UofFeed.DataSchema.Utils.to_boolean("false")
{:ok, false}
"""
@spec to_boolean(data :: charlist()) :: {:ok, boolean()}
def to_boolean(data) when data == ~c"true", do: {:ok, true}
def to_boolean(data) when data == ~c"false", do: {:ok, false}
def to_boolean(_), do: {:ok, nil}
@spec to_boolean(data :: String.t()) :: {:ok, boolean()}
def to_boolean("true"), do: {:ok, true}
def to_boolean("false"), do: {:ok, false}
def to_boolean(d), do: {:ok, nil}

Check warning on line 81 in lib/uof_feed/data_schema/utils.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "d" is unused (if the variable is not meant to be used, prefix it with an underscore)

defp maybe_convert("", _), do: nil
defp maybe_convert(data, :integer), do: String.to_integer(data)
Expand Down
2 changes: 1 addition & 1 deletion lib/uof_feed/handlers/data_schema_inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule UofFeed.Handlers.DataSchemaInspect do
Processing is done via `UofFeed.Mapper.call/1`.
"""
@behaviour UofFeed.Handlers.Behaviour
@behaviour UofFeed.Handler

require Logger

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule UofFeed.Handlers.Behaviour do
defmodule UofFeed.Handler do
@moduledoc """
Behaviour for message handlers.
Contains default implementation for `handle_message/1`
"""

defmacro __using__(_) do
quote do
@behaviour UofFeed.Handlers.Behaviour
@behaviour UofFeed.Handler
@impl true
def handle_message(_message) do
{:error, :not_implemented}
Expand All @@ -16,5 +16,5 @@ defmodule UofFeed.Handlers.Behaviour do
end
end

@callback handle_message(charlist()) :: :ok
@callback handle_message(String.t()) :: :ok
end
2 changes: 1 addition & 1 deletion lib/uof_feed/handlers/pubsub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule UofFeed.Handlers.PubSub do
Processing is done via `UofFeed.Mapper.call/1`.
"""
@behaviour UofFeed.Handlers.Behaviour
@behaviour UofFeed.Handler
@topic Application.compile_env!(:uof_feed, :pubsub_topic)

alias Phoenix.PubSub
Expand Down
2 changes: 1 addition & 1 deletion lib/uof_feed/handlers/xml_inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule UofFeed.Handlers.XMLInspect do
@moduledoc """
Default message handler, prints RAW XML into the logs.
"""
@behaviour UofFeed.Handlers.Behaviour
@behaviour UofFeed.Handler

require Logger

Expand Down
8 changes: 4 additions & 4 deletions lib/uof_feed/mapper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule UofFeed.Mapper do
## Examples
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <fixture_change start_time="1730223000000" product="1"
...> event_id="sr:match:52371543" timestamp="1729840401716"/>"
Expand All @@ -36,17 +36,17 @@ defmodule UofFeed.Mapper do
}
}
Unsupported messages returns relevant error:
Unsupported messages return a relevant error:
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <other_message start_time="1730223000000" product="1"
...> event_id="sr:match:52371543" timestamp="1729840401716"/>"
...> '''
iex> UofFeed.Mapper.call(input)
{:error, :unsupported_message}
"""
@spec call(xml :: charlist()) ::
@spec call(xml :: String.t()) ::
{:ok, FixtureChange.t() | OddsChange.t() | BetCancel.t() | BetStop.t()}
| {:error, :unsupported_message}
def call(xml) do
Expand Down
6 changes: 3 additions & 3 deletions lib/uof_feed/messages/alive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule UofFeed.Messages.Alive do
## Examples
iex> input = ~c'''
iex> input = '''
...> <alive timestamp="1234579" product="2" subscribed="1"/>
...> '''
iex> UofFeed.Messages.Alive.new(input)
Expand All @@ -38,13 +38,13 @@ defmodule UofFeed.Messages.Alive do
Errors encountered dring data processing will be reported.
iex> input = ~c'''
iex> input = '''
...> <alive timestamp="invalid" product="2" subscribed="1"/>
...> '''
iex> UofFeed.Messages.Alive.new(input)
{:error, {[:timestamp], "Invalid data provided, expected integer as string, received: invalid"}}
"""
@spec new(xml :: charlist()) ::
@spec new(xml :: String.t()) ::
{:ok, __MODULE__.t()} | {:error, {access_path :: list(atom()), message :: String.t()}}
def new(xml) do
xml
Expand Down
6 changes: 3 additions & 3 deletions lib/uof_feed/messages/bet_cancel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule UofFeed.Messages.BetCancel do
## Examples
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <bet_cancel end_time="1564598513000" event_id="sr:match:18941600" product="1"
...> start_time="1564597838000" timestamp="1564602448841">
Expand Down Expand Up @@ -69,7 +69,7 @@ defmodule UofFeed.Messages.BetCancel do
Errors encountered during data processing will be reported.
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <bet_cancel end_time="1564598513000" event_id="sr:match:18941600" product="1"
...> start_time="invalid" timestamp="1564602448841">
Expand All @@ -79,7 +79,7 @@ defmodule UofFeed.Messages.BetCancel do
iex> UofFeed.Messages.BetCancel.new(input)
{:error, {[:start_time], "Invalid data provided, expected integer as string, received: invalid"}}
"""
@spec new(xml :: charlist()) ::
@spec new(xml :: String.t()) ::
{:ok, __MODULE__.t()} | {:error, {access_path :: list(atom()), message :: String.t()}}
def new(xml) do
xml
Expand Down
8 changes: 4 additions & 4 deletions lib/uof_feed/messages/bet_settlement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule UofFeed.Messages.BetSettlement do
## Examples
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <bet_settlement certainty="2" product="3" event_id="sr:match:16807109" timestamp="1547538073717">
...> <outcomes>
Expand Down Expand Up @@ -83,9 +83,9 @@ defmodule UofFeed.Messages.BetSettlement do
}}
Errors encountered dring data processing will be reported.
Errors encountered during data processing will be reported.
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <bet_settlement certainty="invalid" product="3" event_id="sr:match:16807109" timestamp="1547538073717">
...> <outcomes>
Expand All @@ -99,7 +99,7 @@ defmodule UofFeed.Messages.BetSettlement do
iex> UofFeed.Messages.BetSettlement.new(input)
{:error, {[:certainty], "Invalid data provided, expected integer as string, received: invalid"}}
"""
@spec new(xml :: charlist()) ::
@spec new(xml :: String.t()) ::
{:ok, __MODULE__.t()} | {:error, access_path :: list(atom()), message :: String.t()}
def new(xml) do
xml
Expand Down
6 changes: 3 additions & 3 deletions lib/uof_feed/messages/bet_stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule UofFeed.Messages.BetStop do
## Examples
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <bet_stop timestamp="12345" product="3" event_id="sr:match:471123" groups="all"/>
...> '''
Expand All @@ -47,14 +47,14 @@ defmodule UofFeed.Messages.BetStop do
Errors encountered during data processing will be reported.
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <bet_stop timestamp="12345" product="invalid" event_id="sr:match:471123" groups="all"/>
...> '''
iex> UofFeed.Messages.BetStop.new(input)
{:error, {[:product], "Invalid data provided, expected integer as string, received: invalid"}}
"""
@spec new(xml :: charlist()) ::
@spec new(xml :: String.t()) ::
{:ok, __MODULE__.t()} | {:error, {access_path :: list(atom()), message :: String.t()}}
def new(xml) do
xml
Expand Down
8 changes: 4 additions & 4 deletions lib/uof_feed/messages/fixture_change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule UofFeed.Messages.FixtureChange do
## Examples
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <fixture_change start_time="1730223000000" product="1" event_id="sr:match:52371543"
...> timestamp="1729840401716" change_type="1" />
Expand All @@ -58,7 +58,7 @@ defmodule UofFeed.Messages.FixtureChange do
Optional fields like `change_type` don't have to be present in the input structure:
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <fixture_change start_time="1730223000000" product="1"
...> event_id="sr:match:52371543" timestamp="1729840401716"/>"
Expand All @@ -77,15 +77,15 @@ defmodule UofFeed.Messages.FixtureChange do
Errors encoutered during data processing will be reported.
NOTE: Only the first error is reported.
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <fixture_change start_time="invalid" product="1"
...> event_id="sr:match:52371543" timestamp="1729840401716"/>
...> '''
iex> UofFeed.Messages.FixtureChange.new(input)
{:error, {[:start_time], "Invalid data provided, expected integer as string, received: invalid"}}
"""
@spec new(xml :: charlist()) ::
@spec new(xml :: String.t()) ::
{:ok, __MODULE__.t()} | {:error, {access_path :: list(atom()), message :: String.t()}}
def new(xml) do
xml
Expand Down
6 changes: 3 additions & 3 deletions lib/uof_feed/messages/odds_change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule UofFeed.Messages.OddsChange do
## Examples
iex> input = ~c'''
iex> input = '''
...> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...> <odds_change event_id="sr:match:1234" timestamp="1234" product="2">
...> <sport_event_status status="1" reporting="1" match_status="1" home_score="2" away_score="0">
Expand Down Expand Up @@ -63,7 +63,7 @@ defmodule UofFeed.Messages.OddsChange do
clock: %UofFeed.Messages.Clock{
match_time: "10:00",
remaining_time: "50:00",
stopped: nil
stopped: true
},
},
odds: %UofFeed.Messages.Odds{
Expand Down Expand Up @@ -94,7 +94,7 @@ defmodule UofFeed.Messages.OddsChange do
}
}}
"""
@spec new(xml :: charlist()) ::
@spec new(xml :: String.t()) ::
{:ok, __MODULE__.t()} | {:error, {access_path :: list(atom()), message :: String.t()}}
def new(xml) do
xml
Expand Down
4 changes: 2 additions & 2 deletions test/uof_feed/handlers/data_schema_inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ defmodule UofFeed.Handlers.DataSchemaInspectTest do

describe "DataSchemaInspect handle_message/1" do
test "displays message" do
input = ~c'''
input = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<fixture_change start_time="1730223000000" product="1" event_id="sr:match:52371543" timestamp="1729840401716" change_type="1" />
'''
"""

assert capture_log(fn ->
assert :ok ==
Expand Down
Loading

0 comments on commit 3ffd4cf

Please sign in to comment.