-
Notifications
You must be signed in to change notification settings - Fork 64
/
events.ex
45 lines (39 loc) · 1.08 KB
/
events.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
defmodule RigInboundGateway.Events do
@moduledoc """
Utility functions used in more than one controller.
"""
alias UUID
alias Rig.Connection
alias Rig.Subscription
alias RIG.Tracing
alias RigCloudEvents.CloudEvent
@spec welcome_event(pid | nil) :: CloudEvent.t()
def welcome_event(pid \\ self()) do
connection_token = Connection.Codec.serialize(pid)
rig_event(
"rig.connection.create",
%{connection_token: connection_token}
)
end
@spec subscriptions_set([Subscription.t()]) :: CloudEvent.t()
def subscriptions_set(subscriptions) do
rig_event(
"rig.subscriptions_set",
Enum.map(subscriptions, fn %Subscription{event_type: event_type, constraints: constraints} ->
%{"eventType" => event_type, "oneOf" => constraints}
end)
)
end
defp rig_event(type, data) do
%{
specversion: "0.2",
type: type,
source: "rig",
id: UUID.uuid4(),
time: Timex.now() |> Timex.format!("{RFC3339}"),
data: data
}
|> Tracing.append_context(Tracing.context())
|> CloudEvent.parse!()
end
end