Current section
Files
Jump to
Current section
Files
lib/schemas/geoblock.ex
defmodule Polymarket.Schemas.Geoblock do
@moduledoc """
The geoblock status for the caller, as returned by `GET /api/geoblock` on the
Polymarket site (https://polymarket.com).
Reports whether the request is `blocked`, along with the detected `ip`,
`country`, and `region`. The keys arrive already lower-cased, so `from_attrs/1`
normalisation is a no-op here but kept for consistency with the other schemas.
"""
use TypedEctoSchema
alias Polymarket.JsonUtil
alias Polymarket.Schemas.Coerce
@primary_key false
@derive Jason.Encoder
typed_embedded_schema do
field(:blocked, :boolean)
field(:ip, :string)
field(:country, :string)
field(:region, :string)
end
@required [:blocked]
@doc """
Builds a `Geoblock` from the raw (JSON-decoded) attributes returned by the
geoblock endpoint. Keys may be in `camelCase` (atom or string); they are
normalised to the `snake_case` fields. Returns `{:error, {:missing_fields,
fields}}` when the `blocked` flag is absent.
"""
@spec from_attrs(map()) :: {:ok, t()} | {:error, {:missing_fields, [atom()]}}
def from_attrs(attrs) do
attrs = JsonUtil.snake_case_keys(attrs)
%__MODULE__{
blocked: attrs["blocked"],
ip: attrs["ip"],
country: attrs["country"],
region: attrs["region"]
}
|> Coerce.require_fields(@required)
end
end