<?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 Jeffrey Warren on Medium]]></title>
        <description><![CDATA[Stories by Jeffrey Warren on Medium]]></description>
        <link>https://medium.com/@jtwarren?source=rss-740616b025ea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*brasyhF0UdekuUgXQrWbfA.jpeg</url>
            <title>Stories by Jeffrey Warren on Medium</title>
            <link>https://medium.com/@jtwarren?source=rss-740616b025ea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 16:39:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jtwarren/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[Demystifying the Magic of Rails]]></title>
            <link>https://medium.com/ruby-on-rails/demystifying-the-magic-of-rails-416d2195f098?source=rss-740616b025ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/416d2195f098</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Jeffrey Warren]]></dc:creator>
            <pubDate>Mon, 22 Jun 2015 02:16:37 GMT</pubDate>
            <atom:updated>2015-06-28T04:40:07.123Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*J7NbF5fSymDCIQLG4IJ_rQ.jpeg" /></figure><p>For me, like many others, Rails has a very “magical” feel. You don’t really even need to know Ruby to get started with Rails and in a few hours you’ll have a fully functional application, complete with users, sessions, and dynamic content. I probably made about 5 non-trivial Rails applications before I really knew what a block was (other than something I just passed to each). Never mind the inheritance structure, modules, mixins, and metaprogramming in Ruby. For me, it wasn’t until I started thinking about how various calls (attr_accessor, before_filter, etc.) work and what they’re doing that I really felt I had a good grasp on things.</p><p>Simple commands, before filters and validations for example, add a ton of functionality. A lot of Rails is clever uses of metaprogramming, but spend some time looking under the hood and you’ll probably stop feeling like things are just “magic”. That’s what I hope to help you accomplish in this post.</p><h4>Ruby::attr_accessor</h4><p>I want to start by looking at Ruby’s attr_accessor method. It’s pretty simple and has been used by almost everyone who’s ever written a line of Ruby; however, most people probably haven’t stop to understand how it works. It’s not Rails-specific but provides some insight into how we might think about certain functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f716303b9193ca9f2cc30f6c2e3dd54/href">https://medium.com/media/4f716303b9193ca9f2cc30f6c2e3dd54/href</a></iframe><p>Here we see that by calling attr_accessor on `start_at` and `end_at` we have given ourselves a lot of functionality.</p><ul><li>Public methods: [:start_at, :start_at=, :end_at, and :end_at=]</li><li>Instance variables: [:@start_at, :@end_at]</li></ul><p>Try this out for yourself. Copy the above gist into an irb and and check event.public_methods and event.instance_variables.</p><p>So when we want to implement attr_accessor we need to do two things, set instance variables and define public methods. Luckily Ruby comes with <a href="http://apidock.com/ruby/Object/instance_variable_set">instance_variable_set</a>, <a href="http://apidock.com/ruby/Object/instance_variable_get">instance_variable_get</a>, and <a href="http://apidock.com/ruby/Module/define_method">define_method</a> — providing just what we need.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0759c903bcd43891ee3442f641f34ad9/href">https://medium.com/media/0759c903bcd43891ee3442f641f34ad9/href</a></iframe><p>Now we see how easy it is to define functions such as attr_accessor, or the similar attr_reader &amp; attr_writer. The important piece to understand here is that attr_accessor isn’t a special or magic function — it’s just one that happens to take advantage of Ruby’s dynamic nature.</p><h4>Rails::before_action</h4><p>Rails provides filters to be run before, after, or around controller actions. A common use case is to require that users are logged in. Putting a `before_action` (formerly and aliased, `before_filter`) in the application controller, as seen below, means that any class inheriting from ApplicationController will receive the filter (note there are ways to skip filters), and it will be run before any action.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd075b10e6a82e755943164c32e0972c/href">https://medium.com/media/bd075b10e6a82e755943164c32e0972c/href</a></iframe><p>Action filters are slightly more complicated than attr_accessor since we need to deal with inheritance and run arbitrary methods before or after a controller action. But let’s get started. We’ll make some simplifying assumptions to begin, that each action will be in a separate call, and there will be no additional parameters. I’ll leave it up to you to play with ways of relaxing these simplifications.</p><p>We first note that each filter must somehow be saved for each class, and that the inheritance structure must be obeyed. With this in mind we can write a simple controller class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2100bf43097e08af0bd5c4c70c7d4d0b/href">https://medium.com/media/2100bf43097e08af0bd5c4c70c7d4d0b/href</a></iframe><p>Here we have a simple controller class with basic functionality for supporting before and after filters. Most of the code here is pretty simple and not too exciting. Storing &amp; calling the methods (callbacks) is relatively straight forward. The only piece here that might be a little unfamiliar is the use of `self.inherited`. This method is invoked whenever a subclass is created and gives us the subclass itself. We can use this, and give the subclass our (already defined) before/after filters, conveniently passed through inheritance to our parent class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7bed2f2db95d2c144331794b8088e37/href">https://medium.com/media/f7bed2f2db95d2c144331794b8088e37/href</a></iframe><p>Now when we inherit from the Controller class, we have a new class (ApplicationController) with it’s own filters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c49fa5d3f80a97c5b7a2f4cb69b185f3/href">https://medium.com/media/c49fa5d3f80a97c5b7a2f4cb69b185f3/href</a></iframe><p>Finally the EventsController class defines a few more actions. We can instantiate this and invoke the execute method to see our work in action.</p><pre>$ EventsController.new.execute(:create)<br>Checking authentication…<br>Loading event…<br>Cleaning up…</pre><p>We see the simple chain of actions run as expected. The parents classes will run first, which is exactly what happens in Rails.</p><p>Rails provides a lot of methods that seemingly augment the Ruby language with new functionality — in reality a lot of this is just Rails taking advantage of the extensibility of the Ruby programming language. I hope that seeing naive implementations of some of these methods will help you understand the dynamic nature of Ruby and embrace the “magic” within Rails. Some interesting things to think about are how <a href="https://github.com/rails/rails/tree/master/activerecord/lib/active_record/validations">ActiveRecord validations</a> work.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=416d2195f098" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ruby-on-rails/demystifying-the-magic-of-rails-416d2195f098">Demystifying the Magic of Rails</a> was originally published in <a href="https://medium.com/ruby-on-rails">Ruby on Rails</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Safer Capistrano Deployments]]></title>
            <link>https://medium.com/@jtwarren/safer-capistrano-deployments-76e16aef990f?source=rss-740616b025ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/76e16aef990f</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Jeffrey Warren]]></dc:creator>
            <pubDate>Fri, 19 Jun 2015 07:04:23 GMT</pubDate>
            <atom:updated>2015-06-20T21:00:18.674Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://capistranorb.com/">Capistrano</a> is a great tool for automated deployments and we’ve been using it @ <a href="http://www.wellframe.com/">Wellframe</a> for the past few months. We initially only used Capistrano for production deployments (we didn’t have a staging or qa environment..), and I was the only one running deployments. I was always a little nervous that I might deploy the wrong branch, but there wasn’t too much else that could go wrong.</p><pre>BRANCH=master bundle exec cap production deploy</pre><p>I was relatively sure things would work as long as I got this command right.</p><p>Over the past few months we’ve added more servers, more environments, and more employees. I have become increasingly nervous whenever a deployment is going out that something might go wrong. What if we deploy the wrong branch to the wrong environment, or what if we prematurely deploy to production?</p><p>I made a few changes to our deployment scripts to ease my worries. The changes are small and simple, but make me feel content that fewer things will go wrong as we grow. The two changes are:</p><ol><li>Confirm any deployment to production.</li><li>Configure allowed branches per environment.</li></ol><p>First, deploying to production shouldn’t be taken lightly, but a simple confirmation is (hopefully) enough to prevent careless mistakes. This is a task that can be called from any of our environments, production.rb for example. Now each environment is responsible for deciding if it’s confirm-worthy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8121287222734b9da3f0ed5f271790f1/href">https://medium.com/media/8121287222734b9da3f0ed5f271790f1/href</a></iframe><p>Second, we follow the <a href="https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow/">Gitflow Workflow</a>; as such, we maintain the invariant that “master” is always deployable, and it is the only branch that should ever be deployed to production. Similarly we want to restrict staging to branches that either match “master” or “release.*”. This should be simple enough with another Capistrano deployment task. We simply have to define allowed branches on our production and staging environment as necessary. An array of regular expressions does just fine, e.g. [‘master’, ‘release.*’] for staging.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c12c33a5941c9b95f0cfa34f5cfa2930/href">https://medium.com/media/c12c33a5941c9b95f0cfa34f5cfa2930/href</a></iframe><p>While simple, these changes prevent a lot of things from going wrong. No one will accidentally deploy to production when they’re scrolling through their command history. We will only ever deploy “master” to production, and “master” or “release.*” to staging. I feel more comfortable now that things are a bit more explicit and these simple rules will be enforced.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76e16aef990f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event processing in Rails]]></title>
            <link>https://medium.com/@jtwarren/event-processing-in-rails-83b76f33b973?source=rss-740616b025ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/83b76f33b973</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Jeffrey Warren]]></dc:creator>
            <pubDate>Sat, 23 May 2015 03:08:08 GMT</pubDate>
            <atom:updated>2015-05-23T03:08:08.850Z</atom:updated>
            <content:encoded><![CDATA[<p>Event processing is important because as applications grow, more actions will be taken when something happens, i.e. an event is triggered. Starting out, you might allow users to create content, let’s say a post. Then as you start to think about engagement, you decide to send an email to a user’s followers when they publish a post. Next, as you notice a lot of spammy or inappropriate posts, you want to check the post’s content and possibly flag it for manual inspection. Quickly it becomes the case that you want a single event, a PostCreationEvent here, to trigger a lot of different functionality. In many cases, the functionality is different enough from the core action to move it out of the critical path, but you still want a clean abstraction model for this.</p><p>Many companies have sophisticated methods for doing event processing. One common approach is to use Kafka for event logging and to write consumer services that process the data as it becomes available to them. This is nice because you can log a single event somewhere in your code, and consumers will pick up that event when they’re ready to process it. I talked about this a little bit in my last post, <a href="https://medium.com/@jtwarren/event-logging-in-rails-79c9f436255a">event logging in Rails</a>. I think what I missed out on was the fact that setting up something like Kafka takes time and often what developers want, and really need, is a quick but robust solution that offers the same functionality, without sacrificing the abstraction model.</p><p>When I think about implementing something like this in Rails, I want it to be clean and out of the way. In the example above, I could imagine a naive approach calling two separate service objects after creating a post — one call for sending an email, and one call for checking content and flagging posts. This to me seems like poor design. What I’d really like to do is just trigger an Event on a post creation and have configuration around event listeners out of my API. This allows anyone to listen (or consume) an event without continuously adding code to the API. All that is really needed here is to add an event consumer and have it listen to the appropriate events.</p><p>Working backwards with this problem, we have a few different consumers that need to somehow be called when an event is triggered. Looking to Rails for help we see that <a href="http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html">ActiveSupport::Notifications</a> is used by Rails itself for a similar problem, logging and instrumenting various events. The next step is to figure out how to tie each consumer to a given event. This is super easy with Notifications and writing the initializer is trivial.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67b1b135b0973e29ce143d844b1f3b74/href">https://medium.com/media/67b1b135b0973e29ce143d844b1f3b74/href</a></iframe><p>Here we have a POST_CREATION event being mapped to several consumers — one for emails, one for spam, and another for logging to Kafka. The functionality of the consumers is understood, and this nice thing is that they’re logically different and do not need to interact with each other. I do want to note that the above functionality could be abstracted into the consumers themselves, but I prefer explicitness of defining the mapping in this scenario.</p><p>Because we want ultimate flexibility with our consumers, we define them to take in a Ruby Hash called a payload. We’ll define a method named `call` on consumers, one that will be called when an event is triggered.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70b6627a4157aa6d7d93af9a0a93b1b3/href">https://medium.com/media/70b6627a4157aa6d7d93af9a0a93b1b3/href</a></iframe><p>Now, any consumer can do with the payload whatever they want or need to. Writing a new consumer for an event becomes really easy and doesn’t need interaction or coordination with the API controllers at all.</p><p>Let’s say that we wanted to add a new consumer to do something when a post is created. Perhaps we want to do a bit of text analysis (a little tf-idf trickery) and determine what the post is about so that we can either add it to a category or surface it in recommendations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37c10fffa6962b9ca0f5833ae1fb0152/href">https://medium.com/media/37c10fffa6962b9ca0f5833ae1fb0152/href</a></iframe><p>Here we have written a simple consumer that enqueues the analysis for asynchronous execution. While there’s obviously post analysis code missing from here, the only changes we really needed were to write the consumer and update our EVENT_CONSUMER_MAPPING.</p><p>The final step is to define what an event is and create an easy way to trigger them. Here, I have defined a base class for such an event, and defined a few methods to help keep things clean.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fad42f21714939704d0e0b93e7b7e57a/href">https://medium.com/media/fad42f21714939704d0e0b93e7b7e57a/href</a></iframe><p>Now we can finally define our post creation event.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68befed61ae3659e1c6fbd3482939403/href">https://medium.com/media/68befed61ae3659e1c6fbd3482939403/href</a></iframe><p>With all of this in place, the last part is to trigger the event, and that can be done with a single line of code.</p><pre>PostCreationEvent.new(@post).trigger</pre><p>Removed from the above code snippets, for simplicity and clarity, is default asynchronous behavior, and the ability to opt-in to synchronous processing. Extensive use of this abstraction shouldn’t block or slow down requests, so asynchronous behavior is desirable. But, it might make sense for some consumers to specifically opt-in to synchronous behavior though, so that could be an option, though I might advise against it.</p><p>While there are other ways of achieving some of the functionality outlined above (active record callbacks, etc), the point of this is to offer an abstraction — and a damn useful one. ActiveSupport::Notifications takes care of a lot of the heavy lifting and allows us to focus on the important details and the business logic. The ability to trigger arbitrary events and further allow for arbitrary consumers enables application developers to iterate very quickly on new ideas, keep great modularity, and work within a scalable model that allows for future improvements or optimizations when necessary.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83b76f33b973" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Event logging in Rails]]></title>
            <link>https://medium.com/@jtwarren/event-logging-in-rails-79c9f436255a?source=rss-740616b025ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/79c9f436255a</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jeffrey Warren]]></dc:creator>
            <pubDate>Sun, 10 May 2015 18:21:21 GMT</pubDate>
            <atom:updated>2015-05-11T01:35:09.388Z</atom:updated>
            <content:encoded><![CDATA[<p>Event logging provides tremendous business value and has many downstream applications. For example, if we <em>just</em> look at login attempts, both successful and failed, we can do several things: automatically send a password reset email after 3 consecutive failed log in attempts to avoid losing a user, detect and block brute force attacks, analyze differences in platform specific behaviors, etc. These user experience, security, and data analysis applications extend beyond login attempts to almost every high level event our API is capturing. There is probably <em>somebody</em> (a service) <em>somewhere</em> (in our stack) that cares about each API event either individually or in the context of others.</p><p>Event logging has the benefit of separating the “producers” of data from the “consumers” of data, and allowing each to perform individually. For example, you most certainly don’t want a real-time anomaly detection service running synchronously in your API. A first step might be to throw a <a href="https://github.com/resque/resque">resque</a> background job at the problem. But then you’ll soon realize that you’re doing the same thing over and over for each new type of service you need, and that each service <em>might</em> need the same data as other services.</p><p>Enter Kafka. I’ve been a huge fan of Kafka ever since reading Jay Kreps’ <a href="https://engineering.linkedin.com/distributed-systems/log-what-every-software-engineer-should-know-about-real-time-datas-unifying">The Log</a>. The short of Kafka is that it allows you to have different and disparate producers and consumers of data/events, and separate the logic into services to meet your needs. The best part is that producers don’t need to know about each other, inform each other of their existence, or coordinate beyond agreeing on data transport. Producers just produce data (throw it at Kafka) and consumers just consume data (read it from Kafka).</p><p>I wanted to add a naive Kafka event-logging service to my Rails app. I had a couple of self-imposed requirements to make the service easy to use and avoid cluttering my code.</p><ul><li>Logging must be encapsulated and easy to change what’s under the hood.</li><li>Events should have some standardized format, but also encapsulated enough to change them around when necessary.</li><li>It should be very easy to add new event types.</li><li>It takes a single line of code to log an event — this wasn’t a strict requirement but I really didn’t want to litter my API with a ton of lines (more than 1) of logging code.</li></ul><p>After a few hours and a couple iterations I came up with something that I was generally pretty happy with, knew fit my needs right now, and pushed it to production. Below is a simplified version, removing clutter such as experiment groups, etc.</p><p>I created a base Event class that contained all of the common code/setup for each event, an event class for each event type — LoginEvent for example, and an EventLogger class for actually logging events.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd1939cc627ca90f6399bfd7e07800eb/href">https://medium.com/media/bd1939cc627ca90f6399bfd7e07800eb/href</a></iframe><p>The above shows an example event, this one being an event for login attempts. The Event class uses the event_type method in order to nicely structure the payload, simply adding an event_type field. The topic method is used by the EventLogger to decide which topic to write to (admittedly I’m least happy about this mixing of concerns). Finally the payload method provides the data that will ultimately be logged to Kafka.</p><p>Adding this to our SessionController, we might do something like,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa5eee8d524b65960cd69639f7b75f68/href">https://medium.com/media/aa5eee8d524b65960cd69639f7b75f68/href</a></iframe><p>The final piece to this setup is the actual EventLogger. The nice thing is that the API doesn’t really care what the event logger does, as long as it’s not blocking. Without Kafka, you might just do some simple statsd logging, or even write to a local file for later manual inspection and analysis.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b0dd5d5f870aec50069e16bd3497529/href">https://medium.com/media/3b0dd5d5f870aec50069e16bd3497529/href</a></iframe><p>Here we have a single method that calls out Kafka producer with the appropriate topic and payload.</p><p>It is really easy to begin logging with this setup in place. We only have two steps: First create a new event class for the new event to be logged, then log that event from the API. The nice part is that each event fully own’s its payload function and has the ability to log whatever information it wants. Moreover the logger is fully encapsulated and we, as in the API, don’t need to worry what’s actually happening under the hood.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79c9f436255a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>