In the last section we spoke about our processing pipeline.
We're now going to install two different modules that are going to help us implement this entire pipeline
right here.
So I'm going to change on over to my terminal.
I'm going to kill my running server and I'm going to run NPM install dash dash save and we're going
to grab something called Low dash and something called Path dash parser.
OK.
And these are only going to take a second to install.
So let's just let it do its thing and then we will start our server backup the load ash library something
that we use a lot for doing any type of iteration.
It's got a whole bunch of helper methods inside of it.
And then the path parser library is something that's going to help us extract just the survey ID and
the choice pieces out of the you are out.
So after those are done installing will start back up with NPM run death.
Now let that just do it start up and run in the background.
And while that's going to flip back over to my code editor and I'm going to find my survey routes file
inside of the routes directory inside of here.
We've got our post request handler for API surveys web hooks.
So right here is where we're going to want to do all this processing logic to look at the incoming array
of events which remember exists on the body property and do all this processing over that.
Now before we start to add any logic to this event handler we're assuming this route handler at all
let's import one or two libraries at the very top of the file.
So at the very top We're going to import actually in total three different libraries.
The first one is going to be the load ash library that we just installed convention by convention.
Whenever we import load ash we assign the library to the underscore character right here.
So underscore is a valid javascript variable.
Again it's just convention that we use underscore for loadout library.
Next we're also going to install or require an I should say the path library that we just installed
can the path Dasch parser.
And then finally we're also going to install or require in you r l and we're going to get that from
the u r l module.
Now this one right here doesn't look much like anything that we've required in before.
So yes you're correct we did not just install a library called you r l l is a default or integrated
module in the no js system.
So you don't have to install you are l you get it for FREE.
Whenever you make use of node the your library has a bunch of different helpers on it for helping us
parse you URLs inside of our application.
The part of this library that actually helps us deal with this stuff is this.
You r l capital U R L function right here.
So this requires in a big a library with a bunch of different helpers.
The only helper that we care about is this capital-T you are L-1 which is why we are destructuring it
off of the L library.
OK.
So let's get to start inside of our robot handler now.
So here's our web hooks handler.
I'm going to remove the stuff that's in here right now and we're going to replace it with a little bit
of logic.
So the first thing that we're going to do is we're going to map over let's look at our diagram rather
than me just say it.
So we're going to map over our list of events and we're going to attempt to do these extraction steps.
So let's try that first.
So inside of here we'll say Konst I don't know is Culdee events and we'll use underscore dot map series
low dashes map function to iterate over Reyk dot body.
So that's my list of events.
Now as the second argument to this function will pop pop in our iterator function.
So for every element in Wreck-Gar body run this function right here will take in the given event and
we're going to do a little bit of processing on it where we attempt to extract the path and then the
actual pieces of data that we really care about.
Now again this is a two step process.
The first step where we process the event we're going to make sure that we are extracting just the route
of the you are out.
We don't care about the domain.
We just want the route.
So to make sure that we only get the route we're going to use that you are el helper that we just imported
and right here.
So inside the map function I'm going to say new capital L and then the event or the you are all that
we want to pass into this is event don't you are.
Remember that's the u r l that we really care about here is obvious event object.
So say event Daut you are out like so.
And now to make sure that we only get the route where the pathname part of the you are l will then say
Dot pathname like so.
So this line right here is going to implement step number 1 where we take the entire fully qualified
you are l and we extract off just this route.
Now step number two and let's assign this to a temporary variable right now.
Step number two is going to be where we start to make use of this path parser library.
So we're going to look at the pathname that we just extracted and we're going to try to pull off just
the survey ID and the choice to do so.
We're going to first create a parser object so we'll say Konst P is a new path.
So remember path right here is the path parser library that we just installed and then we pass in the
string to kind of give this thing the pattern of the different variables that we're trying to extract
from this pathname right here.
So the pattern that we are simply trying to extract is slash API slash surveys.
So that's going to match up with API slash surveys.
And then this next little thing that goes between these next two slashes right here.
That is a variable that we want to somehow extract out of our pathname.
So to tell the PAF library that we want to extract this next part of the you are all going to say colon
survey ID and then to also extract the choice will say Colan choice like so.
Okay I know that I'm going kind of quick here but the thing about all this extraction stuff is that
once we actually kind of run this code and test it you're going to have a much better sense of what
is actually going on here.
So I know we're going kind of quick but again as long as we just kind of like see the result you'll
have a really good idea of exactly what's happening.
So again when we created this new path object right here and assigned it to p p is now an object that
we can use to look at the pathname and somehow extract out a survey ID and a choice.
So now we will run the dot test pathname.
And I'm thinking that right now how about we just canceled this thing out to get a better sense of what's
going on.
Okay.
All right.
So at this point we're looking at the list of events for every event we extract just the path or just
the route portion of the L..
We then create this little matchers thing right here.
We then attempt to use the matcher to extract the survey ID and the choice from the pathname and then
we cancel it out all out.
So let's give this thing a little test and see how we're doing.
So going to flip on over to my e-mail accounts and I'm just going to click on one of the buttons in
here.
So that's going to issue a web request over to our application which should hit the post request route
handler that that we've been working on.
So if we now flip on over to the terminal remember it always takes a little bit of time to see this
stuff actually pop up in the terminal.
So let's take a quick break right now.
We'll wait for this thing to pop up and it will continue in the next section and say yes it looks like
we're going down the right path right now.
So quick break and I'll see you in just a minute.