1 00:00:00,600 --> 00:00:03,510 In the last section we spoke about our processing pipeline. 2 00:00:03,680 --> 00:00:08,150 We're now going to install two different modules that are going to help us implement this entire pipeline 3 00:00:08,150 --> 00:00:08,990 right here. 4 00:00:09,060 --> 00:00:10,930 So I'm going to change on over to my terminal. 5 00:00:11,000 --> 00:00:17,180 I'm going to kill my running server and I'm going to run NPM install dash dash save and we're going 6 00:00:17,180 --> 00:00:23,230 to grab something called Low dash and something called Path dash parser. 7 00:00:23,640 --> 00:00:24,020 OK. 8 00:00:24,020 --> 00:00:25,790 And these are only going to take a second to install. 9 00:00:25,790 --> 00:00:32,360 So let's just let it do its thing and then we will start our server backup the load ash library something 10 00:00:32,360 --> 00:00:34,860 that we use a lot for doing any type of iteration. 11 00:00:34,880 --> 00:00:37,150 It's got a whole bunch of helper methods inside of it. 12 00:00:37,220 --> 00:00:42,050 And then the path parser library is something that's going to help us extract just the survey ID and 13 00:00:42,050 --> 00:00:44,230 the choice pieces out of the you are out. 14 00:00:44,540 --> 00:00:49,760 So after those are done installing will start back up with NPM run death. 15 00:00:49,810 --> 00:00:53,370 Now let that just do it start up and run in the background. 16 00:00:53,720 --> 00:01:00,080 And while that's going to flip back over to my code editor and I'm going to find my survey routes file 17 00:01:00,110 --> 00:01:02,880 inside of the routes directory inside of here. 18 00:01:02,930 --> 00:01:07,450 We've got our post request handler for API surveys web hooks. 19 00:01:07,700 --> 00:01:13,250 So right here is where we're going to want to do all this processing logic to look at the incoming array 20 00:01:13,310 --> 00:01:19,580 of events which remember exists on the body property and do all this processing over that. 21 00:01:19,640 --> 00:01:23,910 Now before we start to add any logic to this event handler we're assuming this route handler at all 22 00:01:24,230 --> 00:01:27,800 let's import one or two libraries at the very top of the file. 23 00:01:28,160 --> 00:01:32,750 So at the very top We're going to import actually in total three different libraries. 24 00:01:32,750 --> 00:01:39,740 The first one is going to be the load ash library that we just installed convention by convention. 25 00:01:39,770 --> 00:01:44,050 Whenever we import load ash we assign the library to the underscore character right here. 26 00:01:44,090 --> 00:01:46,850 So underscore is a valid javascript variable. 27 00:01:46,880 --> 00:01:52,520 Again it's just convention that we use underscore for loadout library. 28 00:01:52,580 --> 00:01:58,760 Next we're also going to install or require an I should say the path library that we just installed 29 00:01:58,980 --> 00:02:01,210 can the path Dasch parser. 30 00:02:01,760 --> 00:02:10,330 And then finally we're also going to install or require in you r l and we're going to get that from 31 00:02:10,450 --> 00:02:12,220 the u r l module. 32 00:02:12,610 --> 00:02:16,280 Now this one right here doesn't look much like anything that we've required in before. 33 00:02:16,420 --> 00:02:24,280 So yes you're correct we did not just install a library called you r l l is a default or integrated 34 00:02:24,280 --> 00:02:26,450 module in the no js system. 35 00:02:26,560 --> 00:02:29,320 So you don't have to install you are l you get it for FREE. 36 00:02:29,320 --> 00:02:35,800 Whenever you make use of node the your library has a bunch of different helpers on it for helping us 37 00:02:35,800 --> 00:02:38,530 parse you URLs inside of our application. 38 00:02:38,530 --> 00:02:42,670 The part of this library that actually helps us deal with this stuff is this. 39 00:02:42,680 --> 00:02:45,850 You r l capital U R L function right here. 40 00:02:45,850 --> 00:02:49,390 So this requires in a big a library with a bunch of different helpers. 41 00:02:49,540 --> 00:02:54,240 The only helper that we care about is this capital-T you are L-1 which is why we are destructuring it 42 00:02:54,250 --> 00:02:56,290 off of the L library. 43 00:02:56,800 --> 00:02:57,130 OK. 44 00:02:57,190 --> 00:03:02,800 So let's get to start inside of our robot handler now. 45 00:03:02,950 --> 00:03:04,590 So here's our web hooks handler. 46 00:03:04,900 --> 00:03:09,400 I'm going to remove the stuff that's in here right now and we're going to replace it with a little bit 47 00:03:09,400 --> 00:03:10,700 of logic. 48 00:03:10,720 --> 00:03:16,210 So the first thing that we're going to do is we're going to map over let's look at our diagram rather 49 00:03:16,210 --> 00:03:17,810 than me just say it. 50 00:03:17,830 --> 00:03:23,170 So we're going to map over our list of events and we're going to attempt to do these extraction steps. 51 00:03:23,320 --> 00:03:25,420 So let's try that first. 52 00:03:25,420 --> 00:03:34,320 So inside of here we'll say Konst I don't know is Culdee events and we'll use underscore dot map series 53 00:03:34,320 --> 00:03:40,320 low dashes map function to iterate over Reyk dot body. 54 00:03:40,540 --> 00:03:42,790 So that's my list of events. 55 00:03:42,940 --> 00:03:47,280 Now as the second argument to this function will pop pop in our iterator function. 56 00:03:47,440 --> 00:03:54,640 So for every element in Wreck-Gar body run this function right here will take in the given event and 57 00:03:54,640 --> 00:04:00,040 we're going to do a little bit of processing on it where we attempt to extract the path and then the 58 00:04:00,040 --> 00:04:04,330 actual pieces of data that we really care about. 59 00:04:04,390 --> 00:04:06,880 Now again this is a two step process. 60 00:04:06,910 --> 00:04:13,120 The first step where we process the event we're going to make sure that we are extracting just the route 61 00:04:13,240 --> 00:04:14,290 of the you are out. 62 00:04:14,380 --> 00:04:15,420 We don't care about the domain. 63 00:04:15,430 --> 00:04:17,120 We just want the route. 64 00:04:17,310 --> 00:04:23,080 So to make sure that we only get the route we're going to use that you are el helper that we just imported 65 00:04:23,080 --> 00:04:25,110 and right here. 66 00:04:25,240 --> 00:04:32,650 So inside the map function I'm going to say new capital L and then the event or the you are all that 67 00:04:32,650 --> 00:04:35,560 we want to pass into this is event don't you are. 68 00:04:35,740 --> 00:04:40,820 Remember that's the u r l that we really care about here is obvious event object. 69 00:04:41,030 --> 00:04:44,250 So say event Daut you are out like so. 70 00:04:44,690 --> 00:04:50,950 And now to make sure that we only get the route where the pathname part of the you are l will then say 71 00:04:51,070 --> 00:04:54,400 Dot pathname like so. 72 00:04:54,460 --> 00:05:00,220 So this line right here is going to implement step number 1 where we take the entire fully qualified 73 00:05:00,220 --> 00:05:04,070 you are l and we extract off just this route. 74 00:05:04,090 --> 00:05:10,100 Now step number two and let's assign this to a temporary variable right now. 75 00:05:10,150 --> 00:05:15,910 Step number two is going to be where we start to make use of this path parser library. 76 00:05:15,910 --> 00:05:20,200 So we're going to look at the pathname that we just extracted and we're going to try to pull off just 77 00:05:20,200 --> 00:05:23,950 the survey ID and the choice to do so. 78 00:05:23,950 --> 00:05:31,290 We're going to first create a parser object so we'll say Konst P is a new path. 79 00:05:31,410 --> 00:05:36,580 So remember path right here is the path parser library that we just installed and then we pass in the 80 00:05:36,580 --> 00:05:41,980 string to kind of give this thing the pattern of the different variables that we're trying to extract 81 00:05:42,220 --> 00:05:44,380 from this pathname right here. 82 00:05:44,380 --> 00:05:50,670 So the pattern that we are simply trying to extract is slash API slash surveys. 83 00:05:51,100 --> 00:05:54,180 So that's going to match up with API slash surveys. 84 00:05:54,370 --> 00:05:58,510 And then this next little thing that goes between these next two slashes right here. 85 00:05:58,510 --> 00:06:02,950 That is a variable that we want to somehow extract out of our pathname. 86 00:06:03,160 --> 00:06:09,280 So to tell the PAF library that we want to extract this next part of the you are all going to say colon 87 00:06:09,850 --> 00:06:17,880 survey ID and then to also extract the choice will say Colan choice like so. 88 00:06:18,320 --> 00:06:23,230 Okay I know that I'm going kind of quick here but the thing about all this extraction stuff is that 89 00:06:23,260 --> 00:06:27,460 once we actually kind of run this code and test it you're going to have a much better sense of what 90 00:06:27,460 --> 00:06:28,520 is actually going on here. 91 00:06:28,540 --> 00:06:33,640 So I know we're going kind of quick but again as long as we just kind of like see the result you'll 92 00:06:33,640 --> 00:06:37,770 have a really good idea of exactly what's happening. 93 00:06:37,780 --> 00:06:43,180 So again when we created this new path object right here and assigned it to p p is now an object that 94 00:06:43,180 --> 00:06:49,360 we can use to look at the pathname and somehow extract out a survey ID and a choice. 95 00:06:49,360 --> 00:06:55,520 So now we will run the dot test pathname. 96 00:06:55,740 --> 00:07:00,280 And I'm thinking that right now how about we just canceled this thing out to get a better sense of what's 97 00:07:00,280 --> 00:07:01,190 going on. 98 00:07:01,670 --> 00:07:02,180 Okay. 99 00:07:03,530 --> 00:07:03,910 All right. 100 00:07:04,010 --> 00:07:10,250 So at this point we're looking at the list of events for every event we extract just the path or just 101 00:07:10,250 --> 00:07:11,720 the route portion of the L.. 102 00:07:12,050 --> 00:07:14,820 We then create this little matchers thing right here. 103 00:07:14,900 --> 00:07:20,570 We then attempt to use the matcher to extract the survey ID and the choice from the pathname and then 104 00:07:20,570 --> 00:07:22,230 we cancel it out all out. 105 00:07:22,250 --> 00:07:24,980 So let's give this thing a little test and see how we're doing. 106 00:07:25,280 --> 00:07:29,590 So going to flip on over to my e-mail accounts and I'm just going to click on one of the buttons in 107 00:07:29,600 --> 00:07:31,180 here. 108 00:07:31,200 --> 00:07:37,040 So that's going to issue a web request over to our application which should hit the post request route 109 00:07:37,130 --> 00:07:39,330 handler that that we've been working on. 110 00:07:39,380 --> 00:07:43,630 So if we now flip on over to the terminal remember it always takes a little bit of time to see this 111 00:07:43,630 --> 00:07:45,340 stuff actually pop up in the terminal. 112 00:07:45,530 --> 00:07:46,970 So let's take a quick break right now. 113 00:07:46,970 --> 00:07:51,230 We'll wait for this thing to pop up and it will continue in the next section and say yes it looks like 114 00:07:51,230 --> 00:07:52,740 we're going down the right path right now. 115 00:07:52,850 --> 00:07:54,790 So quick break and I'll see you in just a minute.