Question

SendGrid is changing their eventhook system to post JSON to a web page. When the data is deserialized it's a collection of arrays, which each contain potentially different keys based on the event. I need to process and add entries to our e-mail tracking database based on the event, containing the data is the arrays or an array within the array.

I'm looking for a way to efficiently process these returned arrays. My initial thought was to do a cfswitch with the 'event' as the expression. If I know an event=bounced, I know what other keys to expect within the array.

While I'm sure that will work, I'm wondering if there isn't a better methodology or approach instead of creating a switch/case for 8 different events that return different arrays.

enter image description here

Was it helpful?

Solution

I would compartmentalize my code into an object that can determine what should/shouldn't be done. So you would have something like this:

<cfcomponent displayname="SendGridProcessor">

    <cffunction name="processEvent" access="public" returntype="void" output="false">
        <cfargument name="EventData" type="struct" required="true" />

        <cfswitch expression="#Arguments.EventData.Event#">

            <cfcase value="processed">
                <cfset processEventProcessed( Arguments.EventData ) />
            </cfcase>

            <cfcase value="dropped">
                <cfset processEventDropped( Arguments.EventData ) />
            </cfcase>

        </cfswitch>

    </cffunction>

    <cffunction name="processEventProcessed" access="private" output="false" returntype="void">
        <cfargument name="EventData" type="struct" required="true" />
        <!--- do your processing --->
    </cffunction>

    <cffunction name="processEventDropped" access="private" output="false" returntype="void">
        <cfargument name="EventData" type="struct" required="true" />
        <!--- do your processing --->
    </cffunction>

</cfcomponent>

This allows you to have a single method that you can call as you loop through your events, and an easy way to separate out your logic based on the event that's being passed from SendGrid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top