# frozen_string_literal: truerequire'posthog/logging'modulePostHogclassFieldParserclass<<selfincludePostHog::UtilsincludePostHog::Logging# In addition to the common fields, capture accepts:## - "event"# - "properties"# - "groups"# - "uuid"defparse_for_capture(fields)common=parse_common_fields(fields)event=fields[:event]properties=fields[:properties]||{}groups=fields[:groups]uuid=fields[:uuid]check_presence!(event,'event')check_is_hash!(properties,'properties')ifgroupscheck_is_hash!(groups,'groups')properties['$groups']=groupsendisoify_dates!propertiescommon['uuid']=uuidifvalid_uuid_for_event_props?uuidcommon.merge({type: 'capture',event: event.to_s,properties: properties.merge(common[:properties]||{})})end# In addition to the common fields, identify accepts:## - "properties"defparse_for_identify(fields)common=parse_common_fields(fields)properties=fields[:properties]||{}check_is_hash!(properties,'properties')isoify_dates!propertiescommon.merge({type: 'identify',event: '$identify','$set':properties,properties: properties.merge(common[:properties]||{})})enddefparse_for_group_identify(fields)properties=fields[:properties]||{}group_type=fields[:group_type]group_key=fields[:group_key]check_presence!(group_type,'group type')check_presence!(group_key,'group_key')check_is_hash!(properties,'properties')fields[:distinct_id]||="$#{group_type}_#{group_key}"common=parse_common_fields(fields)isoify_dates!propertiescommon.merge({event: '$groupidentify',properties: {'$group_type':group_type,'$group_key':group_key,'$group_set':properties.merge(common[:properties]||{})}})end# In addition to the common fields, alias accepts:## - "alias"defparse_for_alias(fields)common=parse_common_fields(fields)distinct_id=common[:distinct_id]# must both be set and move to propertiesalias_field=fields[:alias]check_presence!alias_field,'alias'common.merge({type: 'alias',event: '$create_alias',distinct_id: distinct_id,properties:
{distinct_id: distinct_id,alias: alias_field}.merge(common[:properties]||{})})endprivate# Common fields are:## - "timestamp"# - "distinct_id"# - "message_id"# - "send_feature_flags"defparse_common_fields(fields)timestamp=fields[:timestamp]||Time.newdistinct_id=fields[:distinct_id]message_id=fields[:message_id].to_siffields[:message_id]send_feature_flags=fields[:send_feature_flags]check_timestamp!timestampcheck_presence!distinct_id,'distinct_id'parsed={timestamp: datetime_in_iso8601(timestamp),library: 'posthog-ruby',library_version: PostHog::VERSION.to_s,messageId: message_id,distinct_id: distinct_id,properties: {'$lib'=>'posthog-ruby','$lib_version'=>PostHog::VERSION.to_s}}ifsend_feature_flags&&fields[:feature_variants]feature_variants=fields[:feature_variants]active_feature_variants={}feature_variants.eachdo|key,value|parsed[:properties]["$feature/#{key}"]=valueactive_feature_variants[key]=valueifvalue!=falseendparsed[:properties]['$active_feature_flags']=active_feature_variants.keysendparsedenddefcheck_timestamp!(timestamp)returniftimestamp.is_a?TimeraiseArgumentError,'Timestamp must be a Time'end# private: Ensures that a string is non-empty## obj - String|Number that must be not blank# name - The name of the validated valuedefcheck_presence!(obj,name)returnunlessobj.nil?||(obj.is_a?(String)&&obj.empty?)raiseArgumentError,"#{name} must be given"enddefcheck_is_hash!(obj,name)raiseArgumentError,"#{name} must be a Hash"unlessobj.is_a?Hashend# @param [Object] uuid - the UUID to validate, user provided, so we don't know the type# @return [TrueClass, FalseClass] - true if the UUID is valid or absent, false otherwisedefvalid_uuid_for_event_props?(uuid)returntrueifuuid.nil?unlessuuid.is_a?(String)logger.warn'UUID is not a string. Ignoring it.'returnfalseendis_valid_uuid=uuid.match?(/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i)logger.warn"UUID is not valid: #{uuid}. Ignoring it."unlessis_valid_uuidis_valid_uuidendendendend