<?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 Sahil Prajapati on Medium]]></title>
        <description><![CDATA[Stories by Sahil Prajapati on Medium]]></description>
        <link>https://medium.com/@sahilprjpt?source=rss-5417b0a445f9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UzjvbypwgqQ8CTSW.</url>
            <title>Stories by Sahil Prajapati on Medium</title>
            <link>https://medium.com/@sahilprjpt?source=rss-5417b0a445f9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 08:03:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sahilprjpt/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[Micro Frontends]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://sahilprjpt.medium.com/micro-frontends-eb25f335cfae?source=rss-5417b0a445f9------2"><img src="https://cdn-images-1.medium.com/max/1536/1*dHAXrEGMBaWFDuE7ccClnQ.jpeg" width="1536"></a></p><p class="medium-feed-snippet">Diving Deeper in Micro Frontend World</p><p class="medium-feed-link"><a href="https://sahilprjpt.medium.com/micro-frontends-eb25f335cfae?source=rss-5417b0a445f9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://sahilprjpt.medium.com/micro-frontends-eb25f335cfae?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb25f335cfae</guid>
            <category><![CDATA[micro-frontends]]></category>
            <category><![CDATA[web-design]]></category>
            <category><![CDATA[react-microfrontend]]></category>
            <category><![CDATA[website-design]]></category>
            <category><![CDATA[web-app-development]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Thu, 22 Aug 2024 16:11:57 GMT</pubDate>
            <atom:updated>2024-08-22T16:11:57.302Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How we ship code 300x faster ]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://sahilprjpt.medium.com/how-we-ship-code-300x-faster-2d254be972f?source=rss-5417b0a445f9------2"><img src="https://cdn-images-1.medium.com/max/2000/1*WawkcH8m3VTLin-dOnqUhw.jpeg" width="2000"></a></p><p class="medium-feed-snippet">We all know how fast the package installation is and the speed at which specs get completed, when the project footprint is small. With&#x2026;</p><p class="medium-feed-link"><a href="https://sahilprjpt.medium.com/how-we-ship-code-300x-faster-2d254be972f?source=rss-5417b0a445f9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://sahilprjpt.medium.com/how-we-ship-code-300x-faster-2d254be972f?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d254be972f</guid>
            <category><![CDATA[gitlab-ci]]></category>
            <category><![CDATA[yarn]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[node]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Sun, 20 Sep 2020 08:52:04 GMT</pubDate>
            <atom:updated>2024-09-10T12:28:05.470Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Faster Asset syncs with AWS Lambda]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://sahilprjpt.medium.com/faster-asset-syncs-with-aws-lambda-3021695c7c46?source=rss-5417b0a445f9------2"><img src="https://cdn-images-1.medium.com/max/2600/1*8e1p05KqM5rnNFQmuVRjmA.jpeg" width="6326"></a></p><p class="medium-feed-snippet">In almost all the projects we have assets which need to be pushed to the cloud which then can be served by a CDN. Serving via CDN not only&#x2026;</p><p class="medium-feed-link"><a href="https://sahilprjpt.medium.com/faster-asset-syncs-with-aws-lambda-3021695c7c46?source=rss-5417b0a445f9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://sahilprjpt.medium.com/faster-asset-syncs-with-aws-lambda-3021695c7c46?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3021695c7c46</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[gitlab-ci]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[javascript-development]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Sun, 20 Sep 2020 08:50:09 GMT</pubDate>
            <atom:updated>2020-09-20T08:57:43.838Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Improving Development experience for Node based with package integrity check]]></title>
            <link>https://sahilprjpt.medium.com/improving-development-experience-for-node-based-with-package-integrity-check-401e1771041d?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/401e1771041d</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[yarn-package-manager]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[yarn]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Sun, 13 Sep 2020 16:11:34 GMT</pubDate>
            <atom:updated>2020-09-18T13:41:00.659Z</atom:updated>
            <content:encoded><![CDATA[<h3>Improving Development experience for Node projects with package integrity check</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kA8yGETnDYPhhrtdagL-Aw.jpeg" /><figcaption><a href="http://www.freepik.com/">Designed by rawpixel.com / Freepik</a></figcaption></figure><p>Over past years I have worked in Ruby on Rails (RoR) projects and Node based projects. There were a few things which I really liked about RoR.</p><p>One feature which I really liked was an auto check done by RoR while starting rails server. For example, if a developer had added a new gem in the project and another developer would pull the code and try to start the rails server, it would immediately fail will asking to install the latest gems by executing <strong>bundle install.</strong></p><p>I found this behaviour missing in a node-based project, so I decided to add the same. This was possible by using the <strong>yarn check — integrity</strong> command.</p><p>I created a simple shell script which would run as a prehook to the development server command.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66cde37a8850202431a5103014ba774c/href">https://medium.com/media/66cde37a8850202431a5103014ba774c/href</a></iframe><p>This can be then added in the package.json as shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71cc519b97aeb0e02bae835a9d6fccc1/href">https://medium.com/media/71cc519b97aeb0e02bae835a9d6fccc1/href</a></iframe><p>Now, if a developer tries to start the server locally and there was a new package added which is not currently installed, <strong>yarn dev </strong>command would exit by printing <strong>Run yarn install.</strong></p><p>Let me know how you have improved the development experience for your projects in the comments below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=401e1771041d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Installing private Github NPM packages]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://sahilprjpt.medium.com/installing-private-github-npm-packages-ee7fd79c7d1d?source=rss-5417b0a445f9------2"><img src="https://cdn-images-1.medium.com/max/800/1*C6KqIOLCzoDpottbBxmEZA.png" width="800"></a></p><p class="medium-feed-snippet">Normally any package can be installed using the following command:</p><p class="medium-feed-link"><a href="https://sahilprjpt.medium.com/installing-private-github-npm-packages-ee7fd79c7d1d?source=rss-5417b0a445f9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://sahilprjpt.medium.com/installing-private-github-npm-packages-ee7fd79c7d1d?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee7fd79c7d1d</guid>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[yarn]]></category>
            <category><![CDATA[github-package]]></category>
            <category><![CDATA[node]]></category>
            <category><![CDATA[npm-registry]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Mon, 01 Jun 2020 15:20:53 GMT</pubDate>
            <atom:updated>2021-02-27T21:40:31.894Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Writing and Maintaining API layer in React/React Native]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://sahilprjpt.medium.com/writing-and-maintaining-api-layer-in-react-react-native-1929fc16f611?source=rss-5417b0a445f9------2"><img src="https://cdn-images-1.medium.com/max/2600/1*wXvay8r1j9Z0DbNAqG4GOg@2x.jpeg" width="8000"></a></p><p class="medium-feed-snippet">If you are starting a new app or already have an app but are not sure how to add API layer to your application then this is the right&#x2026;</p><p class="medium-feed-link"><a href="https://sahilprjpt.medium.com/writing-and-maintaining-api-layer-in-react-react-native-1929fc16f611?source=rss-5417b0a445f9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://sahilprjpt.medium.com/writing-and-maintaining-api-layer-in-react-react-native-1929fc16f611?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/1929fc16f611</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[axios]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[api-management]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Sun, 05 May 2019 16:25:43 GMT</pubDate>
            <atom:updated>2020-10-03T00:47:05.730Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Migration guide from ex-navigation to react-navigation]]></title>
            <link>https://sahilprjpt.medium.com/migration-guide-from-ex-navigation-to-react-navigation-32860220a77d?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/32860220a77d</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react-navigation]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Mon, 02 Apr 2018 10:16:47 GMT</pubDate>
            <atom:updated>2018-04-02T13:43:52.465Z</atom:updated>
            <content:encoded><![CDATA[<p>I will be updating this guide frequently as I am currently migrating a project from ex-navigation to react-navigation. You can always refer to <a href="https://gist.github.com/sahil290791/25b0a0414d94d198fa8350f754aaafd0">this gist</a> to track the changes of this blog.<br>Please let me know if there are any mistakes or if it could be done in a better way.</p><p>Hope this guide helps everyone who ever is planning to migrate or even starting with <strong>react-navigation</strong>.</p><blockquote><strong>react-navigation</strong>: 1.5.2 and <strong>react-native</strong>: 0.45.1</blockquote><h4>In react-navigation header consists of 3 parts:</h4><p>1. <em>headerLeft</em> (allows us to specify custom component)<br>2. <em>headerTitle</em> (the centre portion of the navbar)<br>3. <em>headerRight</em> (can be used to add a profile switcher or any other button if needed)</p><ul><li>Adding header <strong>logo/title</strong> in centre:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1eb71aad27bf0772d317cf50bbe0c09/href">https://medium.com/media/a1eb71aad27bf0772d317cf50bbe0c09/href</a></iframe><ul><li>Hiding back button alone on a particular screen:</li></ul><p>In iOS the <strong>headerTitle</strong> appears in centre but in Android the header moves towards the left if there is no navigation history <em>(as the header layout is aligned towards left)</em>. So in some cases we would want the <em>headerLeft</em> to just occupy left portion, just to make the <strong>headerTitle</strong> centred.</p><pre>const PlaceholderButton = () =&gt; {<br>  return (<br>   &lt;View style={{ backgroundColor: ‘#fff’ }} /&gt;<br>  );<br> };</pre><pre>const Stack = StackNavigator({<br>     home: {<br>      screen: Home,<br>      navigationOptions: {<br>      headerTitle: &lt;Header /&gt;,<br>      headerLeft: &lt;PlaceholderButton /&gt;,<br>      headerRight: &lt;ProfileIcon /&gt;,<br>     }<br>   },<br> });</pre><h4>Hiding header for a stack:</h4><ul><li><strong>ex-navigation</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b4f52016660c895378755b644b34e8d/href">https://medium.com/media/1b4f52016660c895378755b644b34e8d/href</a></iframe><ul><li><strong>react-navigation</strong>:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6960b46498ab0911514788f4bbcec45/href">https://medium.com/media/f6960b46498ab0911514788f4bbcec45/href</a></iframe><h4>Replacement for NavigationProvider, StackNavigation:</h4><ul><li>There is no NavigationProvider in react-navigation its all about stacks.</li><li>Declaring a stack in <strong>ex-navigation</strong>:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/952fc7d8de681d4126a6c032248e666b/href">https://medium.com/media/952fc7d8de681d4126a6c032248e666b/href</a></iframe><ul><li>Declaring a stack in <strong>react-navigation</strong>:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7c75234ac478b35ca600e546902116b/href">https://medium.com/media/c7c75234ac478b35ca600e546902116b/href</a></iframe><h4>Passing navigation prop from stack to a screen’s navigation-bar in a nested stack:</h4><p>We would normally need to pass navigation prop to a nested stack in-order to close the child stack and return to parent stack:</p><ul><li><strong>react-navigation</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c78806c6144de2f5724422f93699bc9/href">https://medium.com/media/4c78806c6144de2f5724422f93699bc9/href</a></iframe><h4>Passing route params</h4><ul><li><strong>ex-navigation</strong></li></ul><pre>// passing params `name: foo`<br>this.props.navigator.push(‘home’, { name: ‘foo’ });</pre><pre>// accessing name from params<br>this.props.route.params.name;</pre><ul><li><strong>react-navigation</strong></li></ul><pre>// passing params `name: foo`<br>this.props.navigation.navigate(‘home’, { name: ‘foo’ });</pre><pre>// accessing name from params<br>this.props.navigation.state.params.name;</pre><h4>Dynamically passing initialRouteName, initialRouteParams</h4><ul><li><strong>ex-navigation</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b0196d4401ee8520bbd49c2d4744b17/href">https://medium.com/media/1b0196d4401ee8520bbd49c2d4744b17/href</a></iframe><ul><li><strong>react-navigation</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcdd1394f3c8d645547f7b35323d521a/href">https://medium.com/media/dcdd1394f3c8d645547f7b35323d521a/href</a></iframe><h4>Nested TabNavigator’s example:</h4><p>You can see the demo over here:</p><p><a href="https://snack.expo.io/rJZk7ocuM">Snack</a></p><p>In iOS nested TabNavigators work as they you expect it to be, but if you try doing the same thing in android, you will see both top and bottom Tabs but only the bottom tab is going to work. Its because by default in Android when you implement a Tab it has a sliding gesture to it.</p><p>Now by having two Tabs and having sliding gestures enabled for both makes the top bar break.</p><p>The workaround here is that you disable swipe gesture for any of the two tabs:</p><pre>const BottomStack = TabNavigator({<br>  home: {<br>    screen: TopStack,<br>  },<br>  bookmarks: {<br>    screen: () =&gt; &lt;CustomComponent screenName={&#39;Bookmarks&#39;} /&gt;<br>  }<br>}, {<br>  tabBarPosition: &#39;bottom&#39;,<br>  swipeEnabled: false,<br>  animationEnabled: false,<br>});</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32860220a77d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React portal in versions prior to React 16]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://sahilprjpt.medium.com/react-portal-in-versions-prior-to-react-16-f0f1b9507c7?source=rss-5417b0a445f9------2"><img src="https://cdn-images-1.medium.com/max/640/1*FL0eKODdSr5gkP_JzjvxZw.jpeg" width="640"></a></p><p class="medium-feed-snippet">React 16 was out sometime back with cool new features like fragments, portals, custom DOM attributes and more.</p><p class="medium-feed-link"><a href="https://sahilprjpt.medium.com/react-portal-in-versions-prior-to-react-16-f0f1b9507c7?source=rss-5417b0a445f9------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://sahilprjpt.medium.com/react-portal-in-versions-prior-to-react-16-f0f1b9507c7?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0f1b9507c7</guid>
            <category><![CDATA[react-portal]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-unstable-portal]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Sat, 02 Dec 2017 09:21:36 GMT</pubDate>
            <atom:updated>2019-05-05T16:27:50.185Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Learning Rails AJAX with a simple example]]></title>
            <link>https://sahilprjpt.medium.com/learning-rails-ajax-with-a-simple-example-a2a0ca9bd393?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2a0ca9bd393</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ajax]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Mon, 16 Jan 2017 05:31:07 GMT</pubDate>
            <atom:updated>2017-01-17T15:06:36.109Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A8zconaIW973NTLq8ghxaA.jpeg" /></figure><p>I have been an active user on <a href="http://stackoverflow.com/users/3863146/sahil">StackOverflow</a> and have noticed that many of the developers face difficulties in implementing Rails AJAX. So I thought of creating a demo app and explaining how it works.</p><p>At any point in time if you want to refer to the code, here is the link to my <a href="https://github.com/sahil290791/voting_app">git repo</a>.</p><p>I will be building a Voting app using <a href="https://github.com/ryanto/acts_as_votable">act_as_votable</a> gem which is a good way to understand how AJAX works.</p><p>We begin by creating an app:</p><pre>rails new voting_app -d mysql</pre><p>We need to add our database password in database.yml file and the following gems in the Gemfile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc430eb06e1508a4d9a29cb8d243c7f5/href">https://medium.com/media/bc430eb06e1508a4d9a29cb8d243c7f5/href</a></iframe><p>Run bundle install, and then run the following commands to setup friendly id, user model, post model and controller.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e13f3acfd4a7b4bd648170ba55d7ddac/href">https://medium.com/media/e13f3acfd4a7b4bd648170ba55d7ddac/href</a></iframe><p>Once we have run the migrations we can start our app server using rails s.</p><p>Establish one to many relationship between user and post model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b5256035cff5531ff75a0bef2b7ad26/href">https://medium.com/media/3b5256035cff5531ff75a0bef2b7ad26/href</a></iframe><p>Update the create method in `posts_controller.rb` as we would want to link a post with a user:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7ed106330506c5a68d89a89f4ff1b5f/href">https://medium.com/media/e7ed106330506c5a68d89a89f4ff1b5f/href</a></iframe><p>Also, in order to be sure that posts can only be created by authenticated users, add this line inside <strong>application_controller.rb </strong>as shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1ae625c6a53737a23f33eb8e01360ad/href">https://medium.com/media/a1ae625c6a53737a23f33eb8e01360ad/href</a></iframe><p>Open <strong>localhost:3000 </strong>in your favorite browser and create your first post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V2Tpmj0hgRgp3dNJaz8vOg.png" /></figure><p>Now we need to create a new method that will handle the AJAX calls:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9aa6b1e0734912a799e1858606461e1b/href">https://medium.com/media/9aa6b1e0734912a799e1858606461e1b/href</a></iframe><p>Update the <strong>routes.rb</strong> file with the new method that we just created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/447fa99d81640db0bab8806c27a4ab2b/href">https://medium.com/media/447fa99d81640db0bab8806c27a4ab2b/href</a></iframe><p>Now we can add the HTML part needed to complete the flow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a05de1f639f0db1609cc7137a6ad5a8/href">https://medium.com/media/7a05de1f639f0db1609cc7137a6ad5a8/href</a></iframe><p>We need to create just one more file: <strong>vote.js.erb</strong> (remember convention over configuration, we should always try to name views same as the controller method’s name).</p><p>If you try to run the app and click on the vote button you will see that nothing happens as we have not added code yet in our <strong>vote</strong> method. So let us add some code into it.</p><p>Our vote method should check if the post is already liked by the user or not and then unlike or like based on the condition.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02743737c12010ff293583944416edbf/href">https://medium.com/media/02743737c12010ff293583944416edbf/href</a></iframe><p>Add the following code in <strong>vote.js.erb</strong>, which will update the view accordingly. Here we add a class <strong>liked</strong> to the voting button which changes its color to blue if a user has liked the blog and remove the <strong>liked</strong> class if the user unliked it.</p><p>And finally update the likes count on the page. The code should like this in <strong>vote.js.erb</strong> file now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7704840e887014219d9e32b0b6f0415/href">https://medium.com/media/d7704840e887014219d9e32b0b6f0415/href</a></iframe><p>You can see in the below screenshots that how the votes button will get updated upon liking or unliking:</p><p><em>Before liking the post:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*di-RyoZKRAOeg_U75ym6_Q.png" /></figure><p><em>After unliking the post:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*yF1BRAkVpFjqEQixtdZ7IA.png" /></figure><h4><strong>Few Important points:</strong></h4><p><em>Whenever you see that nothing happens on performing an action on your page even though you have added js code into your </em><strong><em>js.erb</em></strong><em> file, it is because of an error which occurs in js file and therefore the js code does not get executed.</em></p><p><em>Always remember to refresh your page whenever you are debugging an AJAX based functionality in your app as your new code changes do not reflect until you refresh the page.</em></p><p>Please feel free to ask any doubts you have in implementing AJAX in your Rails app.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2a0ca9bd393" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Twitter Bot using Sinatra]]></title>
            <link>https://blog.chatbotslife.com/twitter-bot-using-sinatra-2679dbd9afad?source=rss-5417b0a445f9------2</link>
            <guid isPermaLink="false">https://medium.com/p/2679dbd9afad</guid>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[twitter]]></category>
            <category><![CDATA[sinatra]]></category>
            <category><![CDATA[how-to]]></category>
            <dc:creator><![CDATA[Sahil Prajapati]]></dc:creator>
            <pubDate>Fri, 16 Dec 2016 06:06:57 GMT</pubDate>
            <atom:updated>2016-12-28T01:18:25.596Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sw3AtvB_MpcDO1_U4DmbRA.png" /></figure><p>I am sure you would have come across the term twitter-bots, it&#39;s used by almost everyone to automate tasks like, liking a tweet if your follower tweets by mentioning you in the tweet, retweeting someone’s tweet, following someone who uses a particular term etc.</p><p>It’s another important use case is fetching tweets from twitter to do sentiment analysis or to understand the current trend in twitter.</p><p>So I thought it will be good to write a tutorial on how we can make a bot using Ruby ( <em>Ofcourse as I am a Ruby developer :</em><strong><em>)</em></strong> ).</p><p>Start building this bot with me even if you are not aware of Ruby, I guarantee you will be able to catch up or learn something new out of this. May be you can make your own bot in Python or anyother language.</p><h4>Prerequisites:</h4><ol><li>Ruby</li><li>HTML</li><li>a little bit of CSS</li></ol><p>I could have made this twitter bot on <a href="http://rubyonrails.org/">Ruby on Rails</a>(RoR), but it would not be a wise decision to use it for such small projects. So I decided to use <a href="http://www.sinatrarb.com/">Sinatra</a>, a micro web framework, it has just 1000 lines of code compared to RoR which has around 100000 lines of code.</p><p><strong>It’s fast. It’s simple. It’s efficient.</strong></p><p>Features can be easily added by adding gems similar to how you would do in a NodeJS app.</p><p>So before we start making our app we need to install Ruby. The best way to install Ruby and RubyGems is with Ruby Version Manager, or RVM.</p><p>Follow the steps mentioned below to do so:</p><pre>\curl -L <a href="https://get.rvm.io/">https://get.rvm.io</a> | bash -s stable --ruby=2.3.1</pre><p><em>You can put any version number for the Ruby version.</em></p><p>To install systemwide, prepend sudo before bash:</p><pre>\curl -L <a href="https://get.rvm.io/">https://get.rvm.io</a> | sudo bash -s stable --ruby=2.3.1</pre><p>Now check the ruby version by entering ruby -v, if it is not the same which you installed then do rvm use 2.3.1.</p><p>Now create a folder named as <strong>twitter_bot</strong>.</p><pre>mkdir twitter_bot <br>cd twitter_bot</pre><h4>Note:</h4><p>You can access the code at anytime <a href="https://github.com/sahil290791/re-twitter">here</a> on Github.</p><p>Open this folder in your favorite text editor, I prefer using <a href="https://www.sublimetext.com/">Sublime</a> because of its simplicity.</p><p>Now we are going to put the app structure and basic setup in place. Run the following commands:</p><pre>mkdir -p config/initializers lib views public/css</pre><pre>touch config/initializers/secrets.rb config/initializers/twitter.rb public/css/main.css Gemfile Rakefile config.ru init.rb views/index.erb lib/bot.rb</pre><p>Now you should have the app structure like this:</p><pre>twitter_bot/<br>        ----config/<br>            ----initializers/<br>                ----secrets.rb<br>                ----twitter.rb<br>        ----lib/<br>            ----bot.rb<br>        ----public/<br>            ----css/<br>                ----main.css<br>        ----views/<br>            ----index.erb<br>        ----Gemfile<br>        ----Rakefile<br>        ----config.ru<br>        ----init.rb</pre><p>Add the following gems in the <strong>Gemfile</strong> which will allow us to add features to our app.</p><h4><a href="https://github.com/rack/rack">rack</a>:</h4><p>Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.</p><h4><a href="https://github.com/sferik/twitter">twitter</a></h4><p>A ruby interface for the Twitter API.</p><h4><a href="http://www.sinatrarb.com/">sinatra</a></h4><p>A micro web framework.</p><h4><a href="https://github.com/sinatra/sinatra-contrib">sinatra-contrib</a></h4><p>Auto restarting the app after every change.</p><h4><a href="https://github.com/macournoyer/thin">thin</a></h4><p>A small and fast Ruby web server.</p><h4>json</h4><p>This is a implementation of the JSON specification according to RFC 7159. It allows us to deal with JSON.</p><p>Gemfile should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a0671ff88d97dd63f237c56a59b8987/href">https://medium.com/media/0a0671ff88d97dd63f237c56a59b8987/href</a></iframe><p>Now run bundle install to install all the gems specified in the Gemfile, bundle install</p><p>Now we will add the modules that should be loaded when the app starts in config.ru.</p><pre>require &#39;./config/initializers/secrets&#39; <br>require &#39;./config/initializers/twitter&#39; <br>require &#39;./init&#39; <br>run Sinatra::Application</pre><p>Now we need to have a developer account in twitter to be able to fetch tweets from Twitter. So let’s create it now.</p><ol><li>Go to <a href="https://dev.twitter.com/apps/new">https://dev.twitter.com/apps/new</a> and log in, if necessary.</li><li>Supply the necessary required fields, accept the TOS, and solve the <strong>CAPTCHA</strong>.</li><li>Submit the form</li><li>Copy the <strong>consumer key</strong> (API key) and consumer secret from the screen into your application</li><li>If you also need the access token representing your own account’s relationship with the application.</li><li>Ensure that your application is configured correctly with the permission level you need (read-only, read-write, read-write-with-direct messages).</li><li>On the application’s detail page, invoke the “Your access token” feature to automatically negotiate the access token at the permission level you need.</li><li>Copy the indicated access token and access token secret from the screen into your application</li></ol><p>We need these four keys in order to communicate with Twitter API:</p><ul><li>CONSUMER_KEY</li><li>CONSUMER_SECRET</li><li>ACCESS_TOKEN</li><li>ACCESS_TOKEN_SECRET</li></ul><p>In the <strong>config/initializers/secrets.rb</strong> file add the following keys:</p><pre>ENV[&quot;CONSUMER_KEY&quot;]        = &quot;xxxkJhDxxxxxxxxxxxxxxxzxx0&quot; ENV[&quot;CONSUMER_SECRET&quot;]     = &quot;0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXV&quot; ENV[&quot;ACCESS_TOKEN&quot;]        = &quot;xxxxxxxxxxxxxxxxxxxxxx&quot; ENV[&quot;ACCESS_TOKEN_SECRET&quot;] = &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;</pre><p>Now add the following code to authenticate and get access to fetch tweets from Twitter in <strong>config/initializers/twitter.rb</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98141b29dc681501f7c97b0785e48a79/href">https://medium.com/media/98141b29dc681501f7c97b0785e48a79/href</a></iframe><p>Now we will create a Bot class where we would be able to define certain methods that the bot should do.</p><p>Now lets define what our app is going to do.</p><ul><li>Favorite tweets which w want to by providing certain terms if it has not been favourited by our bots.</li><li>Retweet tweets if it has not been retweeted by bots.</li></ul><p>Our bot should be able to fetch the tweets based on the search terms provided by us.</p><p><strong>search:</strong> client.search(search_string, lang: &quot;en&quot;)</p><p><strong>favorite:</strong> client.favorite(&lt;tweet_id&gt;)</p><p><strong>retweet:</strong> client.retweet(&lt;tweet_id&gt;)</p><p>You definitely do not want to get in trouble by your bot liking a sensitive tweet. So twitter provides a method to check that too.</p><h4>possibly_sensitive?</h4><p>Call this method on the tweet, tweet.possibly_sensitive?. This returns true or false.</p><p>Similarly the twitter API also provides methods like retweeted_tweet?, favorited?. These methods can be directly called on the tweets. You can read more about the twitter gem methods <a href="http://www.rubydoc.info/gems/twitter">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4867e8c95d8a39cb50f10295cc37d885/href">https://medium.com/media/4867e8c95d8a39cb50f10295cc37d885/href</a></iframe><p>Now we need to create actions which can be called from the view to perform a certain action.</p><p>Open init.rb file,</p><pre># storing the root location in a constant. <br>APP_ROOT = File.dirname(__FILE__) <br>require &#39;sinatra&#39; <br>require &#39;json&#39; <br># loading the bot class require <br>File.join(APP_ROOT,&quot;lib&quot;,&quot;bot&quot;)</pre><p>So we have done the basic setup, let us create an index page.</p><p>Open views/index.erb, and add the following HTML code, you have your own layout.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f26ee0af4e2c9afa240a2b837ddb790/href">https://medium.com/media/7f26ee0af4e2c9afa240a2b837ddb790/href</a></iframe><p>Above we have added two buttons to retweet and favorite tweets which we like.</p><p>Let us define the actions which will help us in rendering the pages and performing certain actions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd163360204b4e853736b46be989bd0f/href">https://medium.com/media/dd163360204b4e853736b46be989bd0f/href</a></iframe><p>You can read about more methods provided by the twitter gem <a href="http://www.rubydoc.info/gems/twitter">here</a>.</p><p><strong>Test your app locally:</strong></p><pre>bundle exec rackup -p 9292 config.ru &amp;</pre><p><strong>Deploying App to Heroku:</strong></p><p>You can read more about deploying rack based applications on <a href="https://www.heroku.com/">Heroku</a> <a href="https://devcenter.heroku.com/articles/rack">here</a>.</p><pre>git init <br>git add . <br>git commit -m &#39;pure rack app&#39; <br>heroku create <br>git push heroku master</pre><p>The app is now deployed to Heroku. Test by executing heroku open or by visiting your app’s URL in your browser. You should see the twitter bot app.</p><p>There is still a lot that can be done like adding listeners or running the bot at specific intervals. Please share your thoughts, improvements in the comments below, also let me know if you have created a twitter bot before.</p><p><em>Originally published at </em><a href="https://gist.github.com/sahil290791/0d13f9f2e49d3bb6fbc95ecae572a22d"><em>gist.github.com</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*bQlRSzFHJEmF4Q7PyrLgng.gif" /></figure><figure><a href="https://docs.google.com/forms/d/e/1FAIpQLSeWqtQR0M5J8kRY9fs18qAu746nJJvFyfjc7I2r_El_nuvCBw/viewform?c=0&amp;w=1"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*6XUspT9JOSq0w0Fi35HIaA.png" /></a></figure><figure><a href="https://m.me/ChatbotsLife"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*c1LDMH5vbnIz9rmAka8Hwg.png" /></a></figure><figure><a href="https://chatbotslife.com/how-to-get-a-free-chatbot-b1fb9dfe109#.z9dtp2sy0"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*D0Jf3dI6ZThtqcfwDYY7mg.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2679dbd9afad" width="1" height="1" alt=""><hr><p><a href="https://blog.chatbotslife.com/twitter-bot-using-sinatra-2679dbd9afad">Twitter Bot using Sinatra</a> was originally published in <a href="https://blog.chatbotslife.com/">Chatbots Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>