Skip to content

Commit

Permalink
refactor: typespecs
Browse files Browse the repository at this point in the history
  • Loading branch information
borgoat committed Oct 3, 2022
1 parent ae7be04 commit 47aaa93
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/ex_twelve_data/real_time_prices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,34 @@ defmodule ExTwelveData.RealTimePrices do
@callback handle_price_update(price) :: :ok
end

@type options :: [option]

@type option ::
WebSockex.option()
| {:api_key, binary}
| {:handler, Handler}

@endpoint "wss://ws.twelvedata.com/v1/quotes/price"
@heartbeat_seconds 10
@heartbeat_message Jason.encode!(%{action: "heartbeat"})

@spec start_link(api_key: String.t(), handler: Handler) :: {:error, any} | {:ok, pid}
@spec start_link(options) :: {:error, any} | {:ok, pid}
def start_link(opts) do
Logger.info("~> Connecting to Twelve Data")

api_key = Keyword.get(opts, :api_key)
handler = Keyword.get(opts, :handler)
handler = Keyword.fetch!(opts, :handler)

WebSockex.start_link(
@endpoint,
__MODULE__,
%{handler: handler},
websockex_opts(opts)
)
end

@spec websockex_opts(options) :: options
defp websockex_opts(opts) do
api_key = Keyword.fetch!(opts, :api_key)

ssl_options = [
verify: :verify_peer,
Expand All @@ -49,14 +67,7 @@ defmodule ExTwelveData.RealTimePrices do
{"X-TD-APIKEY", api_key}
]

opts = Keyword.merge([ssl_options: ssl_options, extra_headers: extra_headers], opts)

WebSockex.start_link(
@endpoint,
__MODULE__,
%{handler: handler},
opts
)
Keyword.merge([ssl_options: ssl_options, extra_headers: extra_headers], opts)
end

@doc """
Expand Down

0 comments on commit 47aaa93

Please sign in to comment.