Class: Segment::Analytics::FieldParser

Inherits:
Object
  • Object
show all
Extended by:
Utils
Defined in:
lib/segment/analytics/field_parser.rb

Overview

Handles parsing fields according to the Segment Spec

Constant Summary

Constants included from Utils

Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON

Class Method Summary collapse

Methods included from Utils

date_in_iso8601, datetime_in_iso8601, formatted_offset, isoify_dates, isoify_dates!, seconds_to_utc_offset, stringify_keys, symbolize_keys, symbolize_keys!, time_in_iso8601, uid

Class Method Details

.parse_for_alias(fields) ⇒ Object

In addition to the common fields, alias accepts:

  • “previous_id”



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/segment/analytics/field_parser.rb', line 53

def parse_for_alias(fields)
  common = parse_common_fields(fields)

  previous_id = fields[:previous_id]
  check_presence!(previous_id, 'previous_id')

  common.merge({
    :type => 'alias',
    :previousId => previous_id
  })
end

.parse_for_group(fields) ⇒ Object

In addition to the common fields, group accepts:

  • “group_id”

  • “traits”



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/segment/analytics/field_parser.rb', line 69

def parse_for_group(fields)
  common = parse_common_fields(fields)

  group_id = fields[:group_id]
  traits = fields[:traits] || {}

  check_presence!(group_id, 'group_id')
  check_is_hash!(traits, 'traits')

  isoify_dates! traits

  common.merge({
    :type => 'group',
    :groupId => group_id,
    :traits => traits
  })
end

.parse_for_identify(fields) ⇒ Object

In addition to the common fields, identify accepts:

  • “traits”



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/segment/analytics/field_parser.rb', line 37

def parse_for_identify(fields)
  common = parse_common_fields(fields)

  traits = fields[:traits] || {}
  check_is_hash!(traits, 'traits')
  isoify_dates! traits

  common.merge({
    :type => 'identify',
    :traits => traits
  })
end

.parse_for_page(fields) ⇒ Object

In addition to the common fields, page accepts:

  • “name”

  • “properties”



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/segment/analytics/field_parser.rb', line 91

def parse_for_page(fields)
  common = parse_common_fields(fields)

  name = fields[:name] || ''
  properties = fields[:properties] || {}

  check_is_hash!(properties, 'properties')

  isoify_dates! properties

  common.merge({
    :type => 'page',
    :name => name.to_s,
    :properties => properties
  })
end

.parse_for_screen(fields) ⇒ Object

In addition to the common fields, screen accepts:

  • “name”

  • “properties”

  • “category” (Not in spec, retained for backward compatibility“



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/segment/analytics/field_parser.rb', line 113

def parse_for_screen(fields)
  common = parse_common_fields(fields)

  name = fields[:name]
  properties = fields[:properties] || {}
  category = fields[:category]

  check_presence!(name, 'name')
  check_is_hash!(properties, 'properties')

  isoify_dates! properties

  parsed = common.merge({
    :type => 'screen',
    :name => name,
    :properties => properties
  })

  parsed[:category] = category if category

  parsed
end

.parse_for_track(fields) ⇒ Object

In addition to the common fields, track accepts:

  • “event”

  • “properties”



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/segment/analytics/field_parser.rb', line 16

def parse_for_track(fields)
  common = parse_common_fields(fields)

  event = fields[:event]
  properties = fields[:properties] || {}

  check_presence!(event, 'event')
  check_is_hash!(properties, 'properties')

  isoify_dates! properties

  common.merge({
    :type => 'track',
    :event => event.to_s,
    :properties => properties
  })
end