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”



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

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”



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

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”



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

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”



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

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“



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

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”



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

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