SegmentSpec (segment_spec v0.1.0) View Source

SegmentSpec is a simple data model to represent the events defined in the Segment.com spec.

The parse/1 and parse!/1 functions convert a Segment event in its JSON-decoded format into an appropriate struct. This struct is of type event_struct/0, and contains all fields particular to the event type, as well as all fields common to all Segment events. The common field context is parsed into a SegmentSpec.Common.Context struct during this process.

Each field that is part of a struct is represented as a snake_case atom, regardless of whether the input field was snake_case or camelCase.

Each field that is not part of a struct, such as the contents of the properties or traits, is represented as a string, exactly as it was received.

In other words, we normalize the internal fields that Segment puts in the events, but we do not touch the fields that the Segment user provides.

The normalize/2 and normalize!/2 functions return a normalized version of the given Segment event, with or without null fields. Internal Segment fields are coerced into snake_case as described above.

iex> track = %{
...>   "type" => "track",
...>   "userId" => "xyz",
...>   "event" => "Clicked thing",
...>   "properties" => %{"thingColor" => "red"},
...>   "context" => %{"groupId" => "abc"}
...> }
iex> SegmentSpec.parse!(track)
%SegmentSpec.Track{
  type: "track",
  user_id: "xyz",
  event: "Clicked thing",
  properties: %{"thingColor" => "red"},
  context: %SegmentSpec.Common.Context{group_id: "abc"}
}
iex> SegmentSpec.normalize!(track)
%{
  "type" => "track",
  "user_id" => "xyz",
  "event" => "Clicked thing",
  "properties" => %{"thingColor" => "red"},
  "context" => %{"group_id" => "abc"}
}

Link to this section Summary

Functions

Returns a normalized version of the given Segment event, in which Segment's internal fields have been coerced into snake_case. Pass the include_nil: true option to include empty fields.

Returns a normalized version of the given Segment event, in which Segment's internal fields have been coerced into snake_case. Pass the include_nil: true option to include empty fields.

Parses a Segment event map into the appropriate event_struct/0 struct.

Parses a Segment event map into the appropriate event_struct/0 struct.

Link to this section Types

Link to this section Functions

Link to this function

normalize(event, opts \\ [])

View Source

Specs

normalize(event_struct() | event_map(), Keyword.t()) ::
  {:ok, event_map()} | {:error, String.t()}

Returns a normalized version of the given Segment event, in which Segment's internal fields have been coerced into snake_case. Pass the include_nil: true option to include empty fields.

Link to this function

normalize!(event, opts \\ [])

View Source

Specs

normalize!(event_struct() | event_map(), Keyword.t()) :: event_map()

Returns a normalized version of the given Segment event, in which Segment's internal fields have been coerced into snake_case. Pass the include_nil: true option to include empty fields.

Specs

parse(event_map()) :: {:ok, event_struct()} | {:error, String.t()}

Parses a Segment event map into the appropriate event_struct/0 struct.

Specs

parse!(event_map()) :: event_struct()

Parses a Segment event map into the appropriate event_struct/0 struct.