<?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 Alex Weidmann on Medium]]></title>
        <description><![CDATA[Stories by Alex Weidmann on Medium]]></description>
        <link>https://medium.com/@Effektz?source=rss-3c0248197ccf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cNt6cwV3Q-7YxyGw.jpg</url>
            <title>Stories by Alex Weidmann on Medium</title>
            <link>https://medium.com/@Effektz?source=rss-3c0248197ccf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:03:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Effektz/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[GitMonitor on Elixir]]></title>
            <link>https://medium.com/gitmonitor/gitmonitor-on-elixir-92a87c7eb382?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/92a87c7eb382</guid>
            <category><![CDATA[phoenix]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[gitmonitor]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Mon, 08 Aug 2016 14:08:00 GMT</pubDate>
            <atom:updated>2016-08-24T21:11:50.088Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*4k6Kxiv6QoaZA1wuWv0uPA.png" /></figure><p>The engineering team here at GitMonitor has been excited about the <a href="http://elixir-lang.org/">Elixir programming language</a> for a while, for multiple reasons. It’s <a href="http://www.littlelines.com/blog/2014/07/08/elixir-vs-ruby-showdown-phoenix-vs-rails/">blazing fast</a>, it’s <a href="http://joearms.github.io/2016/03/13/Managing-two-million-webservers.html">extremely scalable</a>, and it’s <a href="http://blog.plataformatec.com.br/2016/05/beyond-functional-programming-with-elixir-and-erlang/">very resilient</a>.</p><p>Those things make Elixir a great candidate for our rule violation check service. This service processes large amounts of data from git hosting services such as GitHub, and Elixir has allowed us to handle large influxes in requests very easily. And using <a href="http://erlang.org/doc/design_principles/applications.html">Erlang’s OTP principles</a> ensures that if something goes wrong for one person, it doesn’t go wrong for everyone.</p><p>And today I’m happy to announce that our rule violation check service, written in Elixir, is complete and deployed to production! All new repositories generated in GitMonitor will use the new Elixir endpoints automatically. And for our current customers, the old webhooks URL will reverse proxy to the new Elixir app, so you don’t have to make any changes to any of your repositories on GitHub.</p><p>In addition to the rule violation check service, we also migrated our notification service to Elixir as well. Now all your Slack &amp; Flowdock notifications, email alerts, and <a href="https://gitmonitor.com/apps">push notifications</a> go through our Elixir codebase as well. You may have noticed that those are two separate microservices, but we are only using one codebase. This is accomplished through the concept of <a href="http://elixir-lang.org/getting-started/mix-otp/dependencies-and-umbrella-apps.html">umbrella projects</a>.</p><p>Umbrella projects are a way to create multiple applications that may or may not depend on each other. They live in the same Elixir project, and the same git repository. They are simply separated by subfolders, but run as separate applications. It is a really great concept that allows us to have multiple microservices living in the same codebase, while giving us the flexibility to scale each service separately.</p><h4>Immediate wins</h4><p>The first thing we were able to do after deploying our production Elixir services was downsize the servers running the old Rails services, which obviously is a big money saver for any startup. We were able to get rid of some servers, and downsize the remaining servers.</p><p>Response times are down as well. From how long it takes to receive and process a webhook from GitHub, to how long it takes to send notifications of a rule violation, everything is faster.</p><p>Development and deployment are also faster. Because everything is housed in one umbrella application, making changes to a specific service or deploying a specific service is now no different than your normal workflow. It’s also fun to learn a new language, and Elixir really helps you think about solving problems in a different way, and our developers love it.</p><h4>The future of Elixir at GitMonitor</h4><p>Obviously, we are fans of Elixir, but we’re also fans of being pragmatic as well. We believe in “the right tool for the job”. For example, our website is still running Ruby on Rails, and probably will be for a while. The amount of traffic it receives, compared to the rule violation check service, is very small. And the majority of the use on GitMonitor is passive (you generally don’t come to the website for updates, we push updates out to you).</p><p>However, with the performance increases and multiple other wins we’ve seen converting two of our major services to Elixir, it would be hard not to put it at the top of the list for all our new projects moving forward.</p><h3><a href="http://gitmonitor.com/">GitMonitor.com</a></h3><h4><a href="http://gitmonitor.com/">Instant email, Slack message &amp; push notification when:</a></h4><ul><li>Any of your protected branches are force pushed to</li><li>Any commits are made directly to protected branches</li><li>A new team member of collaborator is added to the repository</li><li>A pull request is merged with no comments</li><li>A pull request is merged without an “LGTM” comment</li><li>A pull request is merged by an unauthorized user</li><li>A protected file is modified or deleted in any branch</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7n_iJxnzCsMvtVwCgoFymA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92a87c7eb382" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gitmonitor/gitmonitor-on-elixir-92a87c7eb382">GitMonitor on Elixir</a> was originally published in <a href="https://medium.com/gitmonitor">GitMonitor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quickly checkout your last git branch]]></title>
            <link>https://medium.com/gitmonitor/quickly-checkout-your-last-git-branch-e80b284e0e1d?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/e80b284e0e1d</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[branch]]></category>
            <category><![CDATA[checkout]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Fri, 15 Jul 2016 13:23:19 GMT</pubDate>
            <atom:updated>2016-07-15T13:23:59.834Z</atom:updated>
            <content:encoded><![CDATA[<p>Checking out and switching between branches, for most developers, happens multiple times a day. And depending on the tools and flows of your team, some branches can get convoluted and not very simple to type out, especially multiple times a day. Thankfully, there is a quick and easy shortcut to toggle between branches, just checkout “-”</p><h3>Use a dash to checkout the previous branch</h3><pre>$ git status<br>On branch bug/this-is-a-long-branch-name-847176483</pre><pre>$ git checkout master<br>Switched to branch &#39;master&#39;</pre><pre>git checkout -<br>Switched to branch &#39;bug/this-is-a-long-branch-name-847176483&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7n_iJxnzCsMvtVwCgoFymA.png" /></figure><h3><a href="http://gitmonitor.com/">GitMonitor.com</a></h3><h4><a href="http://gitmonitor.com/">Instant email, Slack message &amp; push notification when:</a></h4><ul><li>Any of your protected branches are force pushed to</li><li>Any commits are made directly to protected branches</li><li>A new team member of collaborator is added to the repository</li><li>A pull request is merged with no comments</li><li>A pull request is merged without an “LGTM” comment</li><li>A pull request is merged by an unauthorized user</li><li>A protected file is modified or deleted in any branch</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e80b284e0e1d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gitmonitor/quickly-checkout-your-last-git-branch-e80b284e0e1d">Quickly checkout your last git branch</a> was originally published in <a href="https://medium.com/gitmonitor">GitMonitor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Checkout an old version of a file in git]]></title>
            <link>https://medium.com/gitmonitor/checkout-an-old-version-of-a-file-in-git-23d3a0ff5ce2?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/23d3a0ff5ce2</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Wed, 13 Jul 2016 13:33:06 GMT</pubDate>
            <atom:updated>2016-07-13T13:33:06.306Z</atom:updated>
            <content:encoded><![CDATA[<p>There are a number of reasons why you might want or need find and checkout an old version of a file in your git repository. Maybe you edited a file in your feature branch, and it is no longer needed, and you want to just reset that file back to the HEAD branch state. Maybe there is a bug in production, and you want to look at a version of that file before the most recent production push. So let’s take a look how you can get ahold of an old version of a file in git.</p><h3>Working with HEAD and a feature branch</h3><p>For the sake of this article, our HEAD branch will be master. Like good git flow dictates, we will branch off of <strong>master</strong> into a new feature branch, <strong>calledbug/api_search_error</strong>. During our search to find the API bug we edit a few files, re-write a few methods, and finally we find the cause of the bug. But it was a 1-file, 1-line change, and your <strong>git status</strong> shows 4 changes files. You could go through one by one and copy the old HEAD version back into your feature branch, or you can just checkout the files you want to reset, from HEAD:</p><pre>git checkout app/api/file1.rb</pre><p>This will take <strong>file1.rb</strong> from the <strong>master</strong> branch, and copy it to your feature branch, thus un-staging and resetting that file. This is all good for code that has not yet made it into a master branch. What if you ned to find and checkout a file from a commit that was merged days, weeks or months ago?</p><h3>Working with already-merged commits</h3><p>As we saw earlier, using the <strong>git checkout</strong> command and supplying a filename as the first and only argument will checkout that file from HEAD. If you need to checkout a file version farther back than the HEAD branch, you can supply two arguments instead. The first will be the commit SHA you want to check the file out from, and the second will be the filename:</p><pre>git checkout 72fh81s app/api/file1.rb</pre><p>If you’re not sure what commit SHA to use, first use <strong>git log</strong> and look for a commit message or time-span where you know the file you’re looking for is in the state you expect it to be in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7n_iJxnzCsMvtVwCgoFymA.png" /></figure><h3><a href="http://gitmonitor.com/">GitMonitor.com</a></h3><h4><a href="http://gitmonitor.com/">Instant email, Slack message &amp; push notification when:</a></h4><ul><li>Any of your protected branches are force pushed to</li><li>Any commits are made directly to protected branches</li><li>A new team member of collaborator is added to the repository</li><li>A pull request is merged with no comments</li><li>A pull request is merged without an “LGTM” comment</li><li>A pull request is merged by an unauthorized user</li><li>A protected file is modified or deleted in any branch</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23d3a0ff5ce2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gitmonitor/checkout-an-old-version-of-a-file-in-git-23d3a0ff5ce2">Checkout an old version of a file in git</a> was originally published in <a href="https://medium.com/gitmonitor">GitMonitor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Protected files]]></title>
            <link>https://medium.com/gitmonitor/protected-files-55842bb2a9fc?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/55842bb2a9fc</guid>
            <category><![CDATA[gitmonitor]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Wed, 13 Jul 2016 13:12:38 GMT</pubDate>
            <atom:updated>2016-07-13T13:12:38.459Z</atom:updated>
            <content:encoded><![CDATA[<p>There are some files in every project that require some level of protection, for various reasons. A Rails project owner might want to be notified when sometime commits a change to the <strong>Gemfile</strong>. A Node project owner might want to be alerted right away if a commit deletes their <strong>app.js</strong> file. Or maybe you want to monitor your entire <strong>spec/*</strong> folder so you can double check that any specs that get removed don’t affect overall test coverage in your project. You are probably thinking of some files in your repository you’d like to protect, so let’s set it up!</p><h3>Setup protected files</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*Jp8JQ4O-D5nEswwd.png" /></figure><p>To setup a list of protected files, first login to your GitMonitor account and pull up one of your repositories. Protected files are split into two separate categories. The first is a list of files to protect against modification. The second is a list of files to protect against deletion. They are two separate lists because in most cases, you will have certain files that can be modified, but shouldn’t be deleted, and separating the protected files lists gives you that flexibility.</p><p>You can watch for any file, such as <strong>Gemfile</strong>, or <strong>config/secrets.yml</strong>.</p><p>You can use wildcards to watch for specific file types. For example, <strong>app/models/*.rb</strong> will watch for any .rb files in your app/models directory.</p><p>You can use wildcards to watch entire directories (and sub-directories). For example, <strong>spec/*</strong> will watch for all files in the spec folder, and all sub-folders, such as <strong>spec/models/concerns/test.rb</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7n_iJxnzCsMvtVwCgoFymA.png" /></figure><h3><a href="http://gitmonitor.com/">GitMonitor.com</a></h3><h4><a href="http://gitmonitor.com/">Instant email, Slack message &amp; push notification when:</a></h4><ul><li>Any of your protected branches are force pushed to</li><li>Any commits are made directly to protected branches</li><li>A new team member of collaborator is added to the repository</li><li>A pull request is merged with no comments</li><li>A pull request is merged without an “LGTM” comment</li><li>A pull request is merged by an unauthorized user</li><li>A protected file is modified or deleted in any branch</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55842bb2a9fc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gitmonitor/protected-files-55842bb2a9fc">Protected files</a> was originally published in <a href="https://medium.com/gitmonitor">GitMonitor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recover a lost or deleted git commit]]></title>
            <link>https://medium.com/gitmonitor/recover-a-lost-or-deleted-git-commit-c06ff06e4447?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/c06ff06e4447</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Mon, 11 Jul 2016 12:53:21 GMT</pubDate>
            <atom:updated>2016-07-11T17:16:43.393Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever had a lost commit? Perhaps you did a git reset and then realized you actually lost something important? Maybe you deleted an entire branch thinking all the work was done and merged, only to discover a commit missing in master. Luckily, you can usually go back through your <strong>git reflog</strong> and scan through it until you find the commit you’re looking for.</p><h3>The Reflog</h3><p>The reflog command in git is very powerful. Anytime the tip of one of your branches is updated, a new reflog entry is saved. This makes is extremely useful to look at how code or files existed in the past, on any branch in your repository. Here’s what a reflog might look like:</p><p>For example, you can use <strong>git reflog HEAD@{4}</strong> which will show you where HEAD was 4 steps ago (or 4 commits ago). You can also recover an entire branch from a previous date. If something catastrophic happened and you knew the codebase was good last week, you could find that good commit using <strong>git reflog master@{three.days.ago}</strong>.</p><p>You can also just parse the reflog line by line to see every commit/change to every branch in your repository until you find that commit you’re looking for.</p><pre>$ git reflog<br>d2f35f1 HEAD@{5}: checkout: moving from master to develop<br>d2f35f1 HEAD@{6}: pull: Fast-forward<br>2f01b11 HEAD@{7}: checkout: moving from develop to master<br>8564ea1 HEAD@{8}: rebase finished: returning to refs/heads/develop<br>8564ea1 HEAD@{9}: rebase: develop<br>2f01b11 HEAD@{10}: rebase: checkout master<br>1ff3481 HEAD@{11}: checkout: moving from master to develop<br>2f01b11 HEAD@{12}: commit: this was a commit message</pre><h3>Recover that lost commit</h3><p>In your repository, run <strong>git reflog</strong> (If you know what branch the commit was in, use <strong>git reflog &lt;branch&gt;</strong>)</p><p>Look for the commit message that went missing</p><p>Find the associated SHA, and run <strong>git show &lt;sha&gt;</strong></p><p>View your missing code in all its glory</p><p>Use <strong>git cherry-pick &lt;sha&gt;</strong> to get that lost commit into a new branch</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7n_iJxnzCsMvtVwCgoFymA.png" /></figure><h3><a href="http://gitmonitor.com/">GitMonitor.com</a></h3><h4><a href="http://gitmonitor.com/">Instant email, Slack message &amp; push notification when:</a></h4><ul><li>Any of your protected branches are force pushed to</li><li>Any commits are made directly to protected branches</li><li>A new team member of collaborator is added to the repository</li><li>A pull request is merged with no comments</li><li>A pull request is merged without an “LGTM” comment</li><li>A pull request is merged by an unauthorized user</li><li>A protected file is modified or deleted in any branch</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c06ff06e4447" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gitmonitor/recover-a-lost-or-deleted-git-commit-c06ff06e4447">Recover a lost or deleted git commit</a> was originally published in <a href="https://medium.com/gitmonitor">GitMonitor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Replace Your Production API With Elixir Today]]></title>
            <link>https://medium.com/twins-of-tech/replace-your-production-api-with-elixir-today-4426a8903642?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/4426a8903642</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[phoenix]]></category>
            <category><![CDATA[proxy]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Tue, 14 Jun 2016 13:30:31 GMT</pubDate>
            <atom:updated>2019-05-31T00:32:42.072Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Qq-JjT2Ecr759cggKGTMw.png" /></figure><h4>AKA: How to build a reverse proxy with Elixir and Phoenix</h4><p>So, you have an API that is running in production, serving your wonderful clients and customers with the data they need to have a great experience in your app. But most likely you don’t have the time to re-write your entire production API into a different language.</p><p>We’re going to build a reverse proxy in Elixir and Phoenix that will utilize the Phoenix 404 handlers to reverse-proxy requests to your current production API, allowing you to migrate your API to Elixir route-by-route.</p><h4>Setup</h4><p>Let’s start by generating a new ReverseProxy Elixir app. You may want to name yours “project_api” if you intend to eventually port your current API into this project, it will no longer function just as a reserve proxy.</p><pre>$ mix phoenix.new reverse_proxy</pre><p>Next we need to add <a href="https://github.com/edgurgel/httpoison">HTTPoison</a> to our dependency list and to our application. Open up “mix.exs” and add the bold lines to your files.</p><pre>defmodule ReverseProxy.Mixfile do<br>  use Mix.Project</pre><pre>  def application do<br>    [mod: {ReverseProxy, []},<br>    applications: [:phoenix, :phoenix_html, ... <strong>:httpoison</strong>]]<br>  end</pre><pre>  defp deps do<br>    [{:phoenix, &quot;~&gt; 1.1.4&quot;},<br>    {:postgrex, &quot;&gt;= 0.0.0&quot;},<br>    {:phoenix_ecto, &quot;~&gt; 2.0&quot;},<br>    {:phoenix_html, &quot;~&gt; 2.4&quot;},<br>    {:phoenix_live_reload, &quot;~&gt; 1.0&quot;, only: :dev},<br>    {:gettext, &quot;~&gt; 0.9&quot;},<br>    {:cowboy, &quot;~&gt; 1.0&quot;},<br>    <strong>{:httpoison, &quot;~&gt; 0.8&quot;}</strong>]<br>  end<br>end</pre><p>Then update your dependencies.</p><pre>$ mix deps.get</pre><h4>Implementation</h4><p>Now it’s time to hijack Phoenix’s built-in 404 handling in order to reverse proxy live production requests to your current production API servers. First, let’s open “web/views/error_view.ex”. You’ll see two functions on top to handle 404 and 500 errors. You’ll also see that the error handling is using html templates, but because we are working with an API, we need to change these to JSON.</p><pre>defmodule ReverseProxy.ErrorView do<br>  use ReverseProxy.Web, :view</pre><pre>  def render(&quot;404<strong>.json</strong>&quot;, assigns) do<br>    # This is where our reverse proxy code will go<br>  end</pre><pre>  def render(&quot;500<strong>.json</strong>&quot;, _assigns) do<br>    %{status: 500, message: &quot;Internal Server Error&quot;}<br>  end</pre><pre>end</pre><p>Now <strong>because we don’t have any routes setup, every request that doesn’t raise an exception will run through render/2 with 404.json</strong>. And if our Elixir app hits an exception, it will run through render/2 with 500.json.</p><p>There is one small caveat, and that is that the ErrorView behavior is actually disabled by default in your development environment. In order to correctly test the reverse proxy, we need to change turn off the “debug_errors” option inside of our “config/dev.exs” file.</p><pre>config :reverse_proxy, ReverseProxy.Endpoint,<br> http: [port: 4000],<br> <strong>debug_errors: false</strong>,<br> code_reloader: true,<br> check_origin: false,</pre><p>Now let’s jump back to our ErrorView file and begin building our reverse proxy handling functionality inside 404.json.</p><pre>def render(&quot;404.json&quot;, assigns) do<br>  host = &quot;<a href="https://yourwebsite.com/api">https://yourwebsite.com/api</a>&quot;<br>  conn = assigns[:conn]<br>  params = conn.query_string<br>  headers = conn.req_headers<br>  cookies = [cookie: [&quot;session=a933ec1dd74e691; logged_in=true&quot;]]<br>  url = host &lt;&gt; conn.request_path &lt;&gt; &quot;?&quot; &lt;&gt; params<br>end</pre><p>We are setting up the URL of our production API, grabbing the parameters that were passed in with this request so we can pass them to the production API, and then building out a URL for our reverse proxy to make a request to. Next, we need to have HTTPoison actually make the request.</p><pre>def render(&quot;404.json&quot;, assigns) do<br>  ...</pre><pre>  case HTTPoison.get(url) do<br>    {:ok, %HTTPoison.Response{status_code: 200, body: body}} -&gt;<br>      case Poison.decode(body) do<br>        {:error, _} -&gt;<br>          %{status: 500, message: &quot;Remote Bad Response&quot;}<br>        {:ok, response} -&gt;<br>          response<br>      end<br>    {:ok, %HTTPoison.Response{status_code: 404}} -&gt;<br>      %{status: 404, message: &quot;Remote Not Found&quot;}<br>    {:ok, %HTTPoison.Response{status_code: 500}} -&gt;<br>      %{status: 500, message: &quot;Remote Server Error&quot;}<br>    {:error, %HTTPoison.Error{}} -&gt;<br>      %{status: 500, message: &quot;Remote Server Error&quot;}<br>  end</pre><pre>end</pre><p>First, we are telling HTTPoison to grab the data from our current production API, and then we are pattern matching on the result, mainly the status code. HTTPoison returns the status code of every request in its response, so we can easily match on that status code to determine the result of the request.</p><p>If the first element in our tuple is :ok, and “status_code” is 200, the request was successful and we use Poison to decode the JSON response we received from the API.</p><p>If the first element in our tuple is :ok, and “status_code” is 404, we will return our own 404 response back to the client. And finally, if HTTPoison returns an error, we will render a 500 error. This usually happens when HTTPoison is unable to connect to the remote host.</p><p>Let’s fire up a server and test this!</p><pre>$ iex -S mix phoenix.server</pre><p>Now you need to get a working API URL from your production app, for example: <a href="https://api.yourwebsite.com/v1/users">https://yourwebsite.com/api/v1/users</a> and copy the path, in this case “/api/v1/users”.</p><p>Next, hit your local server with the path from above, for example: <a href="http://localhost:4000/v1/users">http://localhost:4000/api/v1/users</a>, using a tool like <a href="https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en-US">Advanced REST Client</a> to simulate a JSON request</p><p>The response you receive from your local Elixir / Phoenix application should be identical to the response you get from your production API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PM21Ql1G9lhK878PeDPgZg.png" /><figcaption>The request goes through our localhost, and returns a JSON payload from our production API!</figcaption></figure><p><strong>Thats it! You can now begin replacing your production API with Elixir!</strong></p><p>You can find this repo, <em>with </em>the “more options” features listed below, on GitHub at <a href="https://github.com/fourkio/reverse_proxy_elixir_phoenix">fourkio/reverse_proxy_elixir_phoenix</a></p><h4>More Options</h4><p>Most production apps have more advanced and complicated requirements, than just taking one route and asking another server for that data. We have different request types, and sometimes we need to pass along different headers. Here is how you can handle these cases in the reverse proxy.</p><p><strong>Request Types</strong></p><p>Most likely your API has to deal with more than just GET requests, at least POST and PUT. We can just match on the connection method using the conn variable we assigned at the top of our function, and call the correlating HTTPoison function on our URL.</p><pre>def render(&quot;404.json&quot;, assigns) do<br>  ...<br>  conn = assigns[:conn]</pre><pre>  <strong>case conn.method do</strong><br>    <strong>&quot;GET&quot; -&gt;</strong><br>      case HTTPoison.<strong>get</strong>(url) do<br>        ...<br>      end<br>    <strong>&quot;POST&quot; -&gt;</strong><br>      case HTTPoison.<strong>post</strong>(url, [])<br>        ...<br>      end<br>  end</pre><pre>end</pre><p><strong>Headers</strong></p><p>You can also pass along important information about the client and the request via headers, so that your production app does not lose any data in the reverse proxy request. Sending headers through HTTPoison trivial, and simply requires a tuple of key-values as the second argument in the function.</p><pre>case HTTPoison.get(url, [], <strong>%{auth_token: &quot;secret4auth2token0&quot;}</strong>) do<br>  ...<br>end</pre><p>You can also just pull the headers out of the initial reverse proxy request and pass them along to your API.</p><pre>headers = conn.req_headers<br>case HTTPoison.get(url, [], <strong>headers</strong>) do<br>  ...<br>end</pre><p><strong>Cookies</strong></p><p>Cookies are also very trivial to pass along in your reverse proxy request. Simply pass in an array of values as the last parameter in your function to pass along cookie information to your production API. The only thing to note here is that the we have to pass in “hackney” as the main options key, with a cookies sub-key attached. It looks like this.</p><pre>case HTTPoison.post(url, [], headers, <strong>hackney: [cookie: [&quot;session=a933ec1dd923b874e691; logged_in=true&quot;]]</strong>) do<br>  ...<br>end</pre><h4>Find me on Twitter @<a href="https://twitter.com/effektz">Effektz</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4426a8903642" width="1" height="1" alt=""><hr><p><a href="https://medium.com/twins-of-tech/replace-your-production-api-with-elixir-today-4426a8903642">Replace Your Production API With Elixir Today</a> was originally published in <a href="https://medium.com/twins-of-tech">Twins of Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zip Arrays, Ranges & Hashes in Ruby]]></title>
            <link>https://medium.com/twins-of-tech/zip-arrays-ranges-hashes-in-ruby-f63c255dd07c?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/f63c255dd07c</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Tue, 31 May 2016 19:53:00 GMT</pubDate>
            <atom:updated>2019-05-31T00:37:50.455Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*VGI5TQtEQahiux0zQZik4Q.jpeg" /></figure><p>Today, we are going to spend some time digging into <a href="http://apidock.com/ruby/Array/zip">Ruby’s #zip method</a>. Zip is a method in Ruby’s Array class, and it allows us to manipulate arrays in some fun and interesting ways. We are also going to find out how the zip method can be used with Range and Hash as well. Let’s dive in.</p><h3><strong>Arrays</strong></h3><h4><strong>Zip a Single Array</strong></h4><p>If you run zip on a single array, all of the elements in that array will be wrapped in it’s own array, and then each single array is placed back into the “parent array”. Not very useful, but its important to understand as we start digging into multiple arrays.</p><pre>a = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</pre><pre>a.zip<br>=&gt; [[<strong>&quot;</strong>a<strong>&quot;</strong>], [<strong>&quot;</strong>b<strong>&quot;</strong>], [<strong>&quot;</strong>c<strong>&quot;</strong>]]</pre><h4><strong>Zip Two Arrays</strong></h4><p>If you pass an array as an argument to the zip method, it will create an array of arrays, just like we saw previously. However, this time each child-array will contain the index of each zipped array. What? Let’s take a look.</p><pre>a = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]<br>b = [1, 2, 3]</pre><pre><strong>a.zip(b)<br>=&gt; [[&quot;a&quot;, 1], [&quot;b&quot;, 2], [&quot;c&quot;, 3]]</strong></pre><p>The zip method takes an unlimited number of arrays as arguments.</p><pre>a1 = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]<br>a2 = [1, 2, 3]<br>a3 = [&quot;x&quot;, &quot;y&quot;, &quot;z&quot;]<br>a4 = [7, 8, 9]</pre><pre><strong>a1.zip(a2, a3, a4)<br>=&gt; [[&quot;a&quot;, 1, &quot;x&quot;, 7], [&quot;b&quot;, 2, &quot;y&quot;, 8], [&quot;c&quot;, 3, &quot;z&quot;, 9]]</strong></pre><p>You can pass in arrays with different numbers of elements, and depending on which array you zip, one of two things will happen. If the first array has a smaller number of objects than the second array, then everything past last index in the second array will be dropped.</p><pre>a = [1,2]<br>b = [3,4,5]</pre><pre><strong>a.zip(b)<br>=&gt; [[1, 3], [2, 4]]</strong></pre><p>In this case, the 5 is dropped from the second array. However, if we reverse this and the first array has more elements, a nil object will be inserted to fill in the missing values.</p><pre>a = [1,2,3]<br>b = [4,5]</pre><pre><strong>a.zip(b)<br>=&gt; [[1,4], [2,5], [3, nil]]</strong></pre><h3>Ranges</h3><p>Everything we talked about in regards to zipping an array also applies to zipping a range. When you call zip on a range, it is automatically converted to an array using the .to_a method, and then zipped.</p><pre>a = 1..5<br>b = 6..10</pre><pre><strong>a.zip(b)<br>=&gt; [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]</strong></pre><h3><strong>Hashes</strong></h3><p>So what do you do with a zipped array? One use-case is to turn your zipped array into a hash, simply by passing in your zipped array into a new Hash. Here’s a useless example that shows exactly how it works.</p><p>First we will grab a user from our database.</p><pre>user = User.first<br>=&gt; <strong>#&lt;</strong>User id: 1, name: &quot;Alex&quot;, email: &quot;alex@fourk.io&quot;&gt;</pre><p>Next, we will grab an array of keys and an array of values from the user attributes.</p><pre>keys = user.attributes.keys<br>=&gt; [&quot;id&quot;, &quot;name&quot;, &quot;email&quot;]</pre><pre>vals = user.attributes.values<br>=&gt; [1, &quot;Alex&quot;, &quot;alex@fourk.io&quot;]</pre><p>Now let’s #zip them like we have been doing.</p><pre>zip = keys.zip(vals)<br>=&gt; [[&quot;id&quot;, 1], [&quot;name&quot;, &quot;Alex&quot;], [&quot;email&quot;, &quot;alex@fourk.io&quot;]]</pre><p>Finally, lets wrap the returned zip in a Hash object and what we have is a hash of keys and values that mirrors User.first.attributes</p><pre><strong>Hash[zip]</strong><br>=&gt; {&quot;id&quot; =&gt; 1, &quot;name&quot; =&gt; &quot;Alex&quot;, &quot;email&quot; =&gt; &quot;alex@fourk.io&quot;}</pre><pre><strong>User.first.attributes</strong><br>=&gt; {&quot;id&quot; =&gt; 1, &quot;name&quot; =&gt; &quot;Alex&quot;, &quot;email&quot; =&gt; &quot;alex@fourk.io&quot;}</pre><h4>Find me on Twitter @<a href="https://twitter.com/effektz">Effektz</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f63c255dd07c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/twins-of-tech/zip-arrays-ranges-hashes-in-ruby-f63c255dd07c">Zip Arrays, Ranges &amp; Hashes in Ruby</a> was originally published in <a href="https://medium.com/twins-of-tech">Twins of Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails CLI Commands for Elixir & Phoenix]]></title>
            <link>https://medium.com/twins-of-tech/rails-cli-commands-for-elixir-phoenix-88cb7da45fe7?source=rss-3c0248197ccf------2</link>
            <guid isPermaLink="false">https://medium.com/p/88cb7da45fe7</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Alex Weidmann]]></dc:creator>
            <pubDate>Tue, 24 May 2016 13:56:02 GMT</pubDate>
            <atom:updated>2019-05-31T00:40:02.070Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KfiqPT1x-4CPntKNyyrrxw.jpeg" /></figure><p>At first glance, Elixir might look like Ruby, but <a href="https://zeroclarkthirty.com/2015-11-01-elixir-is-not-ruby.html">it’s not Ruby</a>, anyone who has worked in both languages for any amount of time should back that up. But just because it’s not Ruby, doesn’t mean that Rubyists should be turned off by Elixir, and all that it has to offer, including the <a href="http://www.phoenixframework.org/">Phoenix Framework</a>.</p><p>When you install Elixir, you also get a tool called “<a href="http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html">mix</a>” which basically handles and performs the functionality that “rails” and “rake do in a Rails project. Here is a list of standard rails/rake commands, and their mix equivalents.</p><h4>Generate New App</h4><pre># Rails<br>$ <strong>rails new &lt;app_name&gt;</strong></pre><pre># Mix<br>$ <strong>mix phoenix.new &lt;app_name&gt;</strong></pre><p>As with rails, mix will create a new sub-directory with the app name, and will install a new Phoenix application inside that directory.</p><h4><strong>Launch a Server</strong></h4><pre># Rails<br>$ <strong>rails server</strong></pre><pre># Mix<br>$ <strong>iex -S mix phoenix.server</strong></pre><h4><strong>Launch a Console</strong></h4><pre># Rails<br>$ <strong>rails console</strong></pre><pre># Mix<br>$ <strong>iex -S mix</strong></pre><h4>Install Dependencies</h4><pre># Rails<br>$ <strong>bundle install</strong></pre><pre># Mix<br>$ <strong>mix deps.get</strong></pre><h4>Update Dependencies</h4><pre># Rails<br>$ <strong>bundle update &lt;gem_name&gt;</strong><br>$ <strong>bundle update</strong></pre><pre># Mix<br>$ <strong>mix deps.update &lt;pack<br></strong>$ <strong>mix deps.update --all</strong></pre><h4>View Routes</h4><pre># Rails<br>$ <strong>rake routes</strong></pre><pre># Mix<br>$ <strong>mix phoenix.routes</strong></pre><h4>Migrate Database</h4><pre># Rails<br>$ <strong>rake db:migrate</strong></pre><pre># Mix<br>$ <strong>mix ecto.migrate</strong></pre><p><a href="https://github.com/elixir-lang/ecto">Ecto</a> is the Elixir package used to communicate with our database, and you will notice it’s used in place of “db” for most rake/rails commands.</p><h4>Rollback Migration</h4><pre># Rails<br>$ <strong>rake db:rollback</strong></pre><pre># Mix<br>$ <strong>mix ecto.rollback</strong></pre><h4>Create Database</h4><pre># Rails<br>$ <strong>rake db:create</strong></pre><pre># Mix<br>$ <strong>mix ecto.create</strong></pre><h4>Drop Database</h4><pre># Rails<br>$ <strong>rake db:drop</strong></pre><pre># Mix<br>$ <strong>mix ecto.drop</strong></pre><h4>Generate a Migration</h4><pre># Rails<br>$ <strong>rails g migration CreatePictures</strong></pre><pre># Mix<br>$ <strong>mix ecto.gen.migration create_pictures</strong></pre><h4>Run Tests</h4><pre># Rails<br>$ <strong>rake test</strong></pre><pre># Mix<br>$ <strong>mix test</strong></pre><h4>Find me on Twitter @<a href="https://twitter.com/effektz">Effektz</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88cb7da45fe7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/twins-of-tech/rails-cli-commands-for-elixir-phoenix-88cb7da45fe7">Rails CLI Commands for Elixir &amp; Phoenix</a> was originally published in <a href="https://medium.com/twins-of-tech">Twins of Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>