<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yuriy Plugatariov on Medium]]></title>
        <description><![CDATA[Stories by Yuriy Plugatariov on Medium]]></description>
        <link>https://medium.com/@makefunstuff?source=rss-2e1c722d9d5e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*a_b0FZflw_oB0rWVzaTjSA.jpeg</url>
            <title>Stories by Yuriy Plugatariov on Medium</title>
            <link>https://medium.com/@makefunstuff?source=rss-2e1c722d9d5e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 12:16:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@makefunstuff/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using ‘Flow’ for a Data Import Pipeline]]></title>
            <link>https://medium.com/@makefunstuff/using-flow-for-data-import-pipeline-969b5fa35480?source=rss-2e1c722d9d5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/969b5fa35480</guid>
            <category><![CDATA[flow]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Yuriy Plugatariov]]></dc:creator>
            <pubDate>Mon, 09 Apr 2018 08:53:34 GMT</pubDate>
            <atom:updated>2018-04-09T09:48:27.064Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y_cXXvUtC19tX0A4-xbnZA.jpeg" /></figure><p><em>Quite a long time ago, Jose Valim announced new elixir APIs that can be used for processing collections of data in batches. In this post I would like to share how we use </em><a href="https://github.com/elixir-lang/flow"><em>Flow</em></a><em> to process data for our internal import service.</em></p><h3>Describing a problem</h3><p>We have the following collection of data stored in our database:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f959bd6a95547cd6dfb7288cbb992763/href">https://medium.com/media/f959bd6a95547cd6dfb7288cbb992763/href</a></iframe><p>We need to have a simple worker that polls the database at specific intervals, takes a batch of tickets, fetches dependant data from messages_url and customer_url, then after the fetch is done updates the ticket flag from <strong><em>imported: false </em></strong><em>to</em><strong><em> imported: true.</em></strong></p><h3>Naive solution</h3><p>Pretty simple task isn’t it? At first a very naive solution could be the following:</p><ul><li>Fetch batch database, let’s say we are fetching the first 100 records</li><li>For every record spawn asyncronous task leveraging, Task.async</li><li>Await for all the results and start again</li></ul><p>So, let’s describe this in pseudocode below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7c7ea577102746e0b9a37ec61c0b3f8/href">https://medium.com/media/b7c7ea577102746e0b9a37ec61c0b3f8/href</a></iframe><p>This example will work, but the problem is that all work is done sequentially in one asynchronous task. We have noticed that <strong>fetch_messages</strong> and <strong>fetch_customer</strong> should be done concurrently and we also have to make sure that we are updating the <strong>ticket</strong> record properly.</p><h3>How can we achieve that?</h3><p>Looking at this problem, we have noticed that <a href="https://github.com/elixir-lang/gen_stage"><strong>GenStage</strong></a><strong> — </strong>and to be precise <a href="https://github.com/elixir-lang/flow"><strong>Flow</strong></a><strong> — </strong>already has all the toolset we need to achieve that task in a way we would like.</p><p>There are a lot of examples that show how to build a data processing pipeline in a sequential manner, e.g. step-by-step (<strong>[step 1] -&gt; [step 2] -&gt; [finalize]</strong>), I would also recommend <a href="http://teamon.eu/2016/tuning-elixir-genstage-flow-pipeline-processing//">this awesome blogpost</a> on how to build pipelines like this.</p><p>There were no examples on how to process data using multiple concurrent flows. We had the following idea in mind:</p><ul><li>concurrently fetch messages from the 3rd party API, from source A</li><li>concurrently fetch customer from 3rd party, from source B</li><li>wait for both concurrent work and update state of shared data</li></ul><p>Fortunately for us, flow gives this to us out of the box, you can achieve that by leveraging <a href="https://hexdocs.pm/flow/Flow.html#bounded_join/7">bounded_join/7</a>.</p><p>And we could refactor our sequential processing into separate flows, so now our processing pipeline can be described as in the pseudocode below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/665e35baf360cd1fee3aa496de19b5a5/href">https://medium.com/media/665e35baf360cd1fee3aa496de19b5a5/href</a></iframe><p>In this example, we have build 2 separate flows that processes both ticket data and customer data concurrently, and when the work is done, we can update our ticket record without worrying about data conflicts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=969b5fa35480" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bots on Rails, Part Two: Building Conversational Flow]]></title>
            <link>https://medium.com/chat-bots/bots-on-rails-part-two-building-conversational-flow-db096f6eceac?source=rss-2e1c722d9d5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/db096f6eceac</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[facebook-messenger]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[chatbots]]></category>
            <dc:creator><![CDATA[Yuriy Plugatariov]]></dc:creator>
            <pubDate>Thu, 16 Mar 2017 17:08:15 GMT</pubDate>
            <atom:updated>2018-08-02T08:58:03.998Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BDKvaNYpUTEewtV1wssb3g.png" /></figure><p>In the <a href="https://medium.com/@strangeworks/bots-on-rails-part-one-looking-for-an-idea-and-setting-things-up-2e80e6526947">first section</a> of this series we developed an idea and a high level spec of our bot functionality. We also bootstrapped our rails app and created a dummy “hello world” bot, that was basically a skeleton to keep moving. Today, we will make our bot a more useful by adding the most common functionalities, according to our high-level spec that we developed. Here’s what we are actually going to do:</p><ul><li>First, we need to define our rails models that will give us a manageable persistence layer for our bot, e.g. will help us to store settings and daily notes for future storing and querying.</li><li>The next thing that is needed is to develop UI, based on conversational flow using existing facebook messenger api goodies, such as quick replies, postbacks, and persistent menus.</li></ul><h3>Defining Our Data Models</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*usg-fK51Kr-wIQ0LBRImxQ.png" /></figure><p>Referring to our high level spec, at first we need to store the state of our main actor e.g. <strong>User</strong>, so we will introduce a <strong>user model</strong>. The next thing that we would need is our main actor state after he or she received a “question” from the bot, so here is where the next entity comes in, let’s call it an <strong>Update.</strong></p><p>In order to make sure that our bot sends a “question” to the correct recipient we need to store. Fortunately Facebook Messenger webhook provides a bunch of metadata that gives information about our current user, in our case all we need is to have is an existing <strong>facebook_id.</strong> Also our bot “needs” to know when exactly it should “ask” a question, for that purpose let’s introduce a <strong>notification_time </strong>field. And that’s it for User model.<em> Note: to simplify our logic for this tutorial, let’s assume that new user is that one who hasn’t setup </em><strong><em>notification_time</em></strong><em> yet.</em></p><p>Ok, we have now brainstormed what kind of field we need for the user, so what about an update model? Let’s think about this entity on a higher level:</p><ul><li>First the bot “asks” about a user’s mood, and the user will receive a prompt of 3 emojis illustrating corresponding mood(good, normal or bad).</li><li>Next, the bot may prompt a user to describe days in several sentences.</li><li>Later on we would like to have weekly and monthly summaries from the bot.</li></ul><p>So obviously according to this high level spec our Update model should have the following fields:</p><ul><li><strong>mood</strong> — which is integer in our case, because in this case we can leverage the awesomeness of built-in rails <a href="http://api.rubyonrails.org/classes/ActiveRecord/Enum.html">ActiveRecord::Enum</a> api.</li><li><strong>message</strong> — just a simple text field that will store text message with user’s daily update</li></ul><p>So according to this information we can define following models in our rails app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb5de96f563ad0518bab494d71703a3f/href">https://medium.com/media/bb5de96f563ad0518bab494d71703a3f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fe127d03c2f0a7df7bdf0edd82bd5de/href">https://medium.com/media/4fe127d03c2f0a7df7bdf0edd82bd5de/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82b408e4e49207d27c97842a460ef6e7/href">https://medium.com/media/82b408e4e49207d27c97842a460ef6e7/href</a></iframe><p>I think you know how to generate migrations in rails, but just in case you should run the following for each corresponding model:</p><ul><li>for userrails generate migration CreateUsers , after that we need to add proper fields:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17e44bd6f00cfe4b7ac76ada37e5ed2d/href">https://medium.com/media/17e44bd6f00cfe4b7ac76ada37e5ed2d/href</a></iframe><ul><li>for updates rails generate migration Create Updates</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/488e518f95c93cecc284f0c0b3a8c0de/href">https://medium.com/media/488e518f95c93cecc284f0c0b3a8c0de/href</a></iframe><h3>Building Persistent Menu and Adding a “Get Started” Button</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*gFCjf9EGx-3dyF85FAjfXg.png" /></figure><p>The Facebook Messenger api offers a feature of persistent menus for having settings and providing proper onboarding routines for new users. We need to define when we should be notified, and also we would like to give our user some kind of reset settings functionality in order to remove or change our notification. As this thing should be triggered only once we write a simple rake task that handles this routine for us using Facebook Messenger gem functionality.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d71b183f2c21c5d7b110894dacd022c/href">https://medium.com/media/7d71b183f2c21c5d7b110894dacd022c/href</a></iframe><h3>It Can Talk!</h3><p>After we finish preparations (hope you are not tired yet), we can finally get our hands dirty into bot code.</p><p>We have already added on-boarding controls to our Facebook bot, but how can we handle it? Fortunately for us the Facebook Messenger libary gives us a functionality of handling postback messages, so let’s write a simple handler.</p><p>Open app/bot/messenger_bot.rb , that we already created after first section and add the following lines.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9289d866161f03c30f4b5f8a4f06b76b/href">https://medium.com/media/9289d866161f03c30f4b5f8a4f06b76b/href</a></iframe><p>In this code we simply handled the getting started button and added a dummy handler of our menu item that we will implement in the next part of this tutorial.</p><p>After you successfully added this, you can test your controls in Facebook Messenger by clicking the “Getting Started” button. You should see a greeting message like in screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*1dlLp67hLOOQkCqnd6hJlw.png" /></figure><p>We have sent pretty dummy messages, but we need to start setup procedure, so let’s slightly change our greeting message and control that will lead user to continue dialog with our bot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dcc81cdcde66051c3e7e4df67bb148c/href">https://medium.com/media/6dcc81cdcde66051c3e7e4df67bb148c/href</a></iframe><p>As you may have noticed, we have added a quick reply item into our ‘SETUP_BOT’ handler code that will show a button with quick reply message to our user. In order to test this code, first remove all conversation history and click ‘get started’ button again and you should see the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*wFCwmzGzITgIcinhbnU5pg.png" /></figure><p>Great! We have already have a button that will lead us to setup procedure what should we do next?</p><ul><li>First of all we need to create a user record in our database after user has clicked the button.</li><li>The second is we need to prompt our user when it should be notified, in order to simplify this part let’s introduce two autocomplete options: ‘morning’ and ‘evening’.</li><li>After user gave all the details to the bot, we send some outro message and finish our conversation.</li></ul><p>As you see in <em>quick_reply</em> property that we were sending to facebook api was a field <strong>payload, </strong>the purpose of this field looks very reasonable to use to determine what step user did in our bot ui, let’s leverage this data and handle this payload in our message handler.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dcc81cdcde66051c3e7e4df67bb148c/href">https://medium.com/media/6dcc81cdcde66051c3e7e4df67bb148c/href</a></iframe><p>We were also added two more additional payloads that we will handle in next part of our tutorial series.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KRN1-8EguZ7QSUF_h4pUOw.gif" /></figure><p>Now let’s test our bot, after you did everything as I described below you should see the flow above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*wwX4dcaWrYIBirkBMv7jpA.gif" /></figure><h4>Recap</h4><p>In this article we have built the core functionality of our bot, introduced several small rake tasks, and even played with some Facebook Messenger UI goodies. You may notice that codebase is growing, and it seems that design of existing codebase looks, not that great. Don’t worry, in future parts we will focus even on this and introduce some major refactoring techniques(of course we will write tests). As for the the next section, we will “teach” our bot to send notifications according to our date and time settings, see you in a week! If you wan’t to play with complete version of this tutorial you can also checkout to a branch adding-setup-routine from the repo below.</p><p><a href="https://github.com/strangeworks/lifelog-bot">strangeworks/lifelog-bot</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db096f6eceac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chat-bots/bots-on-rails-part-two-building-conversational-flow-db096f6eceac">Bots on Rails, Part Two: Building Conversational Flow</a> was originally published in <a href="https://medium.com/chat-bots">Chatbots Magazine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bots on Rails, Part One: Looking For An Idea & Setting Things Up]]></title>
            <link>https://medium.com/chat-bots/bots-on-rails-part-one-looking-for-an-idea-and-setting-things-up-2e80e6526947?source=rss-2e1c722d9d5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e80e6526947</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[facebook-messenger]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Yuriy Plugatariov]]></dc:creator>
            <pubDate>Thu, 09 Mar 2017 16:58:37 GMT</pubDate>
            <atom:updated>2017-03-21T17:07:49.015Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CSymPh8axe0CkKTiKKedoA.png" /></figure><p>Messenger bots are still one of the most discussed topics in the tech community, and buzzwords such as “conversational commerce” can not be ignored.</p><p>So what is a Messenger bot, really? Conceptually, Messenger bots are simple webhook handling services that can be used on any technology you prefer. I personally prefer Rails, and in this introductory tutorial I would like to share some concepts on how fun it can be to build Facebook messenger bots on Rails.</p><h3>Everything Starts From An Idea</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*kNL8-oUjlq7viav1O8xG-A.png" /></figure><p>Currently there are a bunch of naive bot examples that show weather forecasts, deliver food, etc. I wouldn’t say that weather forecast bot examples are boring, but I would like to build something where I can play with as many interactions as possible and touch some fancy stuff from the Facebook Messenger API.</p><p>So what are we going to build? We want to build something that’s not boring yet is “doable” at the same time. The first thing that came to my mind was the concept of a “life log” where you are prompted with a simple question and respond with a small sentence about your day. Sounds pretty simple, right? Let’s start by describing our user flow:</p><ul><li>When I write message to the bot, I would like to start the conversation by getting an introductory message.</li><li>After that, I would like to see what the bot can do.</li><li>Then, I would like to <strong>setup</strong> it.</li><li>During this <strong>setup phase, </strong>the bot will prompt me <strong>when</strong> I would like to be prompted to give life log sentence each day.</li><li>Then, I would like to be asked about my mood and be allowed to pick from an emoji list.</li><li>After that, the bot will respond with some clever answer and store this message for future reference.</li></ul><p>This flow looks very simple and straightforward, so how can we make it fancier? Let’s add an additional requirement. I would like to ask the bot in <strong>natural language, </strong>“What I did two days ago?” and have the bot send me a clickable button that will open <strong>web view</strong> of my activity page.</p><h3>Preparing Our Toolbox</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ofz7KMhP3ZcO6HlxfwuuGw.png" /></figure><p>Thanks to the great Ruby and Rails community, we will not start building our own Facebook messenger API payload parsers, but instead use a very good library <a href="https://github.com/hyperoslo/facebook-messenger"><strong>hyperoslo/facebook-messenger</strong></a>, which gives the most important toolset out of the box. Returning to our requirements, we will also need to find out how to parse natural language and provide proper responses from the bot to the end user. Fortunately, for this we can leverage some awesomeness of <a href="http://wit.ai/"><strong>wit.ai</strong></a><strong> </strong>which<strong> </strong>gives a great natural language api absolutely for free.</p><h3>Building Our Dummy Bot</h3><p>We have the needed toolset, so now the main question is, “How do we get started?” Let’s think about our checklist:</p><ul><li>Facebook requires us to have a separate Facebook page, and a Facebook app that can be created in the Facebook developer center.</li><li>To start processing messages, we will need a service that will handle webhooks sent from Facebook. For this purpose will write our Rails app.</li><li>Our bot requires a persistence layer to “remember” all settings and data the we will provide from conversation flow, so we will need a database, in our case we will use heroku.</li></ul><h3>Setting Everything Up On Facebook’s Side</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fpsKJk99-b8Bn6U1gS87Yg.png" /></figure><ul><li>First, you need to create your own Facebook page which will represent your bot. You can do that by visiting this <a href="https://www.facebook.com/pages/create/">link</a>.</li><li>In order to start working with webhook processing we need to create a Facebook app. This can be done by following <a href="http://developers.facebook.com/">developers.facebook.com</a>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zS6i0OqXAEx1JLFz4M4c_Q.png" /></figure><ul><li>After you have created your app, you will need to add Messenger platform support. This can be done by clicking the “add product” menu item at the sidebar. Now you have almost everything you need from Facebook’s side, we will return to the developer center a bit later. Now we need to dive into our Rails app.</li></ul><h4>Building A Simple Rails App</h4><p><a href="https://github.com/strangeworks/lifelog-bot">strangeworks/lifelog-bot</a></p><p>I will not describe everything that you need to do for setting up a Rails app because I expect you are already familiar with Rails and can create new Rails app by yourself. If you don’t want to do this, you can also clone the repo listed above where all the starter files are already provided. After you setup rails and all required dependencies, you will need to make sure that all required dependencies works properly.</p><p>Make sure that you’ve added gem &#39;facebook-messenger&#39; to your Gemfile as we are using a custom directory for bot files, so we need to make sure that Rails “knows” about it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbdcae9c01355bbd0c43e8d18be8c355/href">https://medium.com/media/cbdcae9c01355bbd0c43e8d18be8c355/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47d009f385008095a2e1dfcd08c8e014/href">https://medium.com/media/47d009f385008095a2e1dfcd08c8e014/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0835e910f9b1e0b92cf56aec8cfac6ce/href">https://medium.com/media/0835e910f9b1e0b92cf56aec8cfac6ce/href</a></iframe><p>In order to successfully communicate with Facebook’s api from your dev environment, you need to share access to your local machine somehow. For this purpose, I would suggest using the tool <a href="https://ngrok.com/">ngrok</a>. Run your Rails server using bundle exec rails s and then open new shell and run ngrok http 3000 where 3000 it can be any port where your rails app is currently bound. <strong>Quick note: </strong>if you are starting your app at first time don’t forget to run rails db:create</p><p>The next thing you will need are tokens from Facebook’s developer console. You need to get: a page access token, a verify token for incoming webhooks, and an app token. You can get all of these tokens from the developer console.</p><p>As I have provided .env support to the starter repo you can add .env file and include the following keys there.</p><pre>ACCESS_TOKEN=YOURTOKEN<br>APP_SECRET=YOURSECRET<br>VERIFY_TOKEN=YOURVERIFYTOKEN</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J9gl_ZksN0L4JVYoHF3U1w.png" /></figure><p>After you have successfully obtained all the required tokens, you should add your URL provided by ngrok to the Facebook webhook settings.</p><p>Now, let’s write some basic code to test if everything works.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02567b9f913e0ecfaf8c2cfd22075a8a/href">https://medium.com/media/02567b9f913e0ecfaf8c2cfd22075a8a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/257/1*knWSFY2DS56XCsRGdaR2RA.png" /></figure><p>We can restart our Rails server, find our Facebook page name in Messenger, and simply write a message. In our case, let’s use hello bot. According to the code example, we should respond with a simple: hello human</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*03JVJhYH9CmrWY9eWiLizA.png" /></figure><p>Yay! We have basic skeleton for our bot!</p><h3>What’s Next?</h3><p>In this article, we have successfully defined the scope of our small project, provided basic bot structure, and prepared all required dependencies so we can move forward. In the next part of the series, we will deep dive into implementation of the setup phase. We will touch different Messenger specific UI elements and even implement basic natural language processing support. See you in the next part!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e80e6526947" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chat-bots/bots-on-rails-part-one-looking-for-an-idea-and-setting-things-up-2e80e6526947">Bots on Rails, Part One: Looking For An Idea &amp; Setting Things Up</a> was originally published in <a href="https://medium.com/chat-bots">Chatbots Magazine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On verbosity of express endpoints.]]></title>
            <link>https://medium.com/@makefunstuff/on-verbosity-of-express-endpoints-dbff03f581a2?source=rss-2e1c722d9d5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbff03f581a2</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[expressjs]]></category>
            <dc:creator><![CDATA[Yuriy Plugatariov]]></dc:creator>
            <pubDate>Sat, 04 Mar 2017 23:33:04 GMT</pubDate>
            <atom:updated>2017-03-05T10:37:54.033Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*F3V9dTXGV35GRJ3WSMoeoQ.jpeg" /><figcaption>borrowed from <a href="https://www.flickr.com/photos/austinfoodjournal/3560196091/in/photolist-6qAWge-8FUFhD-rgcANa-5Ss3ZD-bionK8-ax7pfw-9uvJdC-quo6Dq-9YT6xK-9uPuGj-6MAce2-c3GGZ7-c3GHsj-83zVeJ-gherLx-9HH1JR-bv8EJF-dQCypV-hjF4rP-di8Jms-Dm9gMq-97s5t5-kvnSqz-gH4FMP-9Qwhhj-dp1Y1a-abJ8G6-awibez-stcB6v-otJuf8-fCgn8M-dtr3Tp-76zcdA-dMuse8-kiMrnU-93fokP-8pGooR-5rRthz-b1BFRD-7xg6AT-awib6g-apcSrr-nqaXE5-8oruFg-b9CvZK-BVB5d1-6qB89K-arcjtv-9kG1zU-ah6g7q">flickr</a></figcaption></figure><p>There is a very high risk of writing JavaScript bolognese in express endpoints, and sometimes we can’t notice how flexible express can be even with this minimalistic toolset that it gives out of the box. In this post I would like to share some thoughts about proper definition of express endpoint and what can be delegated to middlewares to make codebase as much reusable as possible.</p><p>Before starting small <strong><em>disclaimer</em></strong>: these ideas is applicable in case when you know that that your express app will grow in terms of codebase if not this post will be more harmful rather than useful. After this quick warning moment let’s get started.</p><p>Sometimes you have some custom ecosystem of logging, error responses, success responses. Below you can see the most common express endpoint flow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/0*7pJc_fPMGV0JE5GE.png" /></figure><h3>Welcome to the spaghetti world</h3><p>In any express project we always have endpoints, where we have the following flow: client initiates simple http request and expects some json data. Under the hood we have bunch of operations such as asynchronous database calls, fetching some data from 3rd party api, logging success state, tracking data to some 3rd party apis and responding with success payload, in our example let’s assume that client expects son only when everything is ok.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dd3f8645727e371905888e88b64f0b8/href">https://medium.com/media/7dd3f8645727e371905888e88b64f0b8/href</a></iframe><p>Looking at this small example you may notice that some parts of this endpoints leaks more details that may seems that it is out this endpoint responsibility?</p><p>After working with dozens of different express apps I have noticed that middleware layer is pretty underused, almost everywhere it is used only as some tiny request processing tool. Yes obviously it is intended to do such thing and it does it pretty well but sometimes I can see that there is a bit chance to produce code duplications and start testing more than needed. What express routes intended to do? Does express route need to know more than a single thing, how can we leverage single responsive principle in our example? In our first example the responsibility of the endpoint is to send processed data entry by certain identifier provided by client. If we have only this endpoint this code looks more or less ok. Let’s assume that we also need to fetch raw data entry without initiating any processing service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18d7eba463c8b8d418b8f9b17a65f679/href">https://medium.com/media/18d7eba463c8b8d418b8f9b17a65f679/href</a></iframe><p>As you may notice we are introducing more complexity in our codebase by adding duplication which sounds like a great target for refactoring. How can we DRY this endpoints. First of all let’s pay attention to error handling in both examples? You probably may notice that first we can do is provide utility function for error handling and require it in both modules. But as we are using express why no to leverage built-in middleware layer in this case.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca6defb0680db7021d65241b921933c9/href">https://medium.com/media/ca6defb0680db7021d65241b921933c9/href</a></iframe><h3>What did we achieve after that?</h3><p>First of all we got rid out of one external dependency that we may stub in our tests, and obviously this dependency tell us nothing about real intention of this endpoint. When you are ordering a cup of coffee your outcome should be mug of coffee and you don’t want to hear from they guy who helps barista to repair coffee machine because of N reasons, in our case repair guy is external dependency that we are encapsulating in middleware that does one thing and does it properly. Let’s look at the outcome.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50034f345c5a9b71baf27f26284ed00b/href">https://medium.com/media/50034f345c5a9b71baf27f26284ed00b/href</a></iframe><p>This refactoring looks very tiny but it doesn’t mean that benefit is tiny as well, in this case you don’t need to test things that are out of ones responsibility.</p><h3>What can we do next?</h3><p>You may noticed that fetching from params and next from database is also looks like copy paste driven development, which will also may cause possible issues. Let’s assume that we plan to add more endpoints that will lookup by entry_id parameter, will we constantly copy and paste this data fetching behaviour? Is there any express way to solve this problem? If we pay attention to express documentation express gives us nifty feature of declaring <a href="http://expressjs.com/en/api.html#app.param">param</a> based middleware why not to use this? But before let’s think about conceptual idea of request lifecycle in express app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/0*vPpglbZ_Tb9-SIu4.png" /></figure><p>In express we can have as much middlewares as it is is possible which means that we can pre-process our request object adding additional keys and values before it goes to final destination e.g. client. So what does it mean for us? It means that we can make loose coupled middleware functions that can be reused all over our application logic which will lead us to less code when our system will be extended.</p><p>So let’s define our param handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec5f56ba0e4e9b6346abdea6e43a1b7f/href">https://medium.com/media/ec5f56ba0e4e9b6346abdea6e43a1b7f/href</a></iframe><p>Looking at this small code you may notice that we provided additional metadata to our request object plus mixed in result of database query. So after we extracted common data fetching from route param we can extract common code to DRY our express endpoints and the result will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af5749aaeb4dc842e5064da3b463290c/href">https://medium.com/media/af5749aaeb4dc842e5064da3b463290c/href</a></iframe><p>After this changes applied we removed bunch of duplications from both our endpoints plus we have introduced single responsibility principle, our endpoints does only one thing. But where is response and logging and tracking you may ask? And here is the most controversial part of this post. According to our diagram we can pass request using by calling next function to other middlewares and work with our request until we hit req.end or finalise its lifecycle in one of our middlewares.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f56b84f7c3c64b60cc3a634bc3f5ce9/href">https://medium.com/media/0f56b84f7c3c64b60cc3a634bc3f5ce9/href</a></iframe><p>So after this changes we will have decoupled tiny middlewares and we can and make our subsystems as much reusable as possible. I’m not sure that it is the best idea ever but in terms of testability of each subcomponent it looks more than reasonable to do this for bigger express applications.</p><p>If you are interested and wan’t to play with code examples you can use this <a href="https://gist.github.com/strangeworks/fe9b279ca6c00f0923cad94135af2de6">gist</a>, thank you for your time spent of this long read, I hope it was useful</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbff03f581a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>