<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://beforeitwasround.com/atom.xml" rel="self" type="application/atom+xml" /><link href="https://beforeitwasround.com/" rel="alternate" type="text/html" /><updated>2026-04-20T00:08:10+00:00</updated><id>https://beforeitwasround.com/atom.xml</id><title type="html">flat</title><subtitle>before it was round</subtitle><author><name>Steve Agalloco</name></author><entry><title type="html">Knowing what endpoints your Dropwizard application supports</title><link href="https://beforeitwasround.com/2016/09/knowing-what-endpoints-your-dropwizard-application-supports.html" rel="alternate" type="text/html" title="Knowing what endpoints your Dropwizard application supports" /><published>2016-09-02T10:22:00+00:00</published><updated>2016-09-02T10:22:00+00:00</updated><id>https://beforeitwasround.com/2016/09/knowing-what-endpoints-your-dropwizard-application-supports</id><content type="html" xml:base="https://beforeitwasround.com/2016/09/knowing-what-endpoints-your-dropwizard-application-supports.html"><![CDATA[<p>A vast majority of our API technology is built on <a href="http://dropwizard.io">Dropwizard</a> - we love the framework and how easily it allows us to build new APIs. As the number of services we run has increased, it can often be difficult to remember what endpoints an application responds to. When you start a Dropwizard application, the framework logs the endpoints in a really elegant format:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>INFO  [2016-09-01 20:41:23,904] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

POST    /eat/lunch (com.tradier.rest.LunchResource)
GET     /fruit/banana (com.tradier.rest.BananaResource)
GET     /vegetable/avocado (com.tradier.rest.AvocadoResource)
</code></pre></div></div>

<p>But it’s both impractical and slow to startup a server periodically to see it’s endpoints. Of course documentation would be a solution here as well, but <em>ain’t nobody got time for that!</em>. Fortunately, Dropwizard 0.8 added public methods to <code class="language-plaintext highlighter-rouge">DropwizardResourceConfig</code> to access the endpoint data in a way that can be leveraged within an application. Not wanting this to be part of a public API, <a href="http://stdout.tradier.com/development/2014/05/27/how_to_create_dropwizard_tasks.html">Dropwizard tasks</a> are a great place to add this to our application:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">EndpointsTask</span> <span class="kd">extends</span> <span class="nc">Task</span> <span class="o">{</span>

  <span class="kd">public</span> <span class="nf">EndpointsTask</span><span class="o">()</span> <span class="o">{</span>
    <span class="kd">super</span><span class="o">(</span><span class="s">"endpoints"</span><span class="o">);</span>
  <span class="o">}</span>

  <span class="nd">@Override</span>
  <span class="kd">public</span> <span class="kt">void</span> <span class="nf">execute</span><span class="o">(</span><span class="nc">ImmutableMultimap</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">,</span> <span class="nc">String</span><span class="o">&gt;</span> <span class="n">parameters</span><span class="o">,</span> <span class="nc">PrintWriter</span> <span class="n">output</span><span class="o">)</span>
      <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
    <span class="nc">DropwizardResourceConfig</span> <span class="n">config</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">DropwizardResourceConfig</span><span class="o">();</span>
    <span class="n">config</span><span class="o">.</span><span class="na">packages</span><span class="o">(</span><span class="s">"com.tradier.rest"</span><span class="o">);</span>
    <span class="n">output</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">config</span><span class="o">.</span><span class="na">getEndpointsInfo</span><span class="o">());</span>
  <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>

<p>Now, when we want to see the endpoints for a given application, we can just access the task!</p>

<hr />

<p><em>This post has been cross-posted to the <a href="http://stdout.tradier.com">Tradier Developer Blog</a>, for more posts like this, you may want to follow our posts there as well!</em></p>]]></content><author><name>Steve Agalloco</name></author><category term="Development" /><summary type="html"><![CDATA[A useful task for your Dropwizard application.]]></summary></entry><entry><title type="html">Using Lua to implement multi-get on Redis hashes</title><link href="https://beforeitwasround.com/2014/07/using-lua-to-implement-multi-get-on-redis-hashes.html" rel="alternate" type="text/html" title="Using Lua to implement multi-get on Redis hashes" /><published>2014-07-10T09:45:00+00:00</published><updated>2014-07-10T09:45:00+00:00</updated><id>https://beforeitwasround.com/2014/07/using-lua-to-implement-multi-get-on-redis-hashes</id><content type="html" xml:base="https://beforeitwasround.com/2014/07/using-lua-to-implement-multi-get-on-redis-hashes.html"><![CDATA[<p>On a recent project at <a href="http://tradier.com">Tradier</a>, we relied heavily on <a href="http://redis.io">Redis</a> hashes. We really like Redis for it’s versatility - it’s various data types create lots of possibilities for solutions. We were thoroughly impressed by it’s tolerance to a write-heavy data stream that we were pushing into it. But as fast as we were able to write data in, we found it’s performance while doing multi-read’s to be a little more cumbersome than we’d like.</p>

<p>Using the Redis Ruby Gem, we first turned to pipelined requests. Pipelined requests return a future, meaning in order to fully query and load, you essentially have to loop twice:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>

<span class="vg">$redis</span><span class="p">.</span><span class="nf">pipelined</span> <span class="k">do</span>
  <span class="n">keys</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">key</span><span class="o">|</span>
    <span class="n">data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="vg">$redis</span><span class="p">.</span><span class="nf">hgetall</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="n">data</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">key</span><span class="p">,</span><span class="n">value</span><span class="o">|</span>
  <span class="n">data</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span><span class="p">.</span><span class="nf">value</span>
<span class="k">end</span>

</code></pre></div></div>

<p>While this does the job, it’s tedious and with large key-sets not as performant as we’d like it to be. What we’d really like is something closer to Memcached’s multi-get support. As we considered other solutions, we decided to take a look at Redis’ <a href="http://redis.io/commands/eval">scripting support</a> to see if it could help.  Not really knowing much about <a href="http://www.lua.org">Lua</a>, we were pretty surprised by how powerful Lua was. Using Lua, we can make a single request to Redis, passing all of the keys as an argument to the Lua script:</p>

<div class="language-lua highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="kd">local</span> <span class="n">collate</span> <span class="o">=</span> <span class="k">function</span> <span class="p">(</span><span class="n">key</span><span class="p">)</span>
  <span class="kd">local</span> <span class="n">raw_data</span> <span class="o">=</span> <span class="n">redis</span><span class="p">.</span><span class="n">call</span><span class="p">(</span><span class="s1">'HGETALL'</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>
  <span class="kd">local</span> <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>

  <span class="k">for</span> <span class="n">idx</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="o">#</span><span class="n">raw_data</span><span class="p">,</span> <span class="mi">2</span> <span class="k">do</span>
    <span class="n">data</span><span class="p">[</span><span class="n">raw_data</span><span class="p">[</span><span class="n">idx</span><span class="p">]]</span> <span class="o">=</span> <span class="n">raw_data</span><span class="p">[</span><span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
  <span class="k">end</span>

  <span class="k">return</span> <span class="n">data</span><span class="p">;</span>
<span class="k">end</span>

<span class="kd">local</span> <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>

<span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">key</span> <span class="k">in</span> <span class="nb">ipairs</span><span class="p">(</span><span class="n">KEYS</span><span class="p">)</span> <span class="k">do</span>
  <span class="n">data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">collate</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="k">end</span>

</code></pre></div></div>

<p>The code was fairly simple. We can loop through the <code class="language-plaintext highlighter-rouge">KEYS</code> and invoke the <code class="language-plaintext highlighter-rouge">collate</code> method we defined to load the hash data. The challenge then became passing this data back to our ruby code. We found that while Lua objects will not easily serialize back to a Ruby object, Redis’ Lua implementation offers up some options: namely cjson and cmsgpack. We need just return from the script and we’re now returning data back:</p>

<div class="language-lua highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="c1">-- return json</span>
<span class="k">return</span> <span class="n">cjson</span><span class="p">.</span><span class="n">encode</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>

<span class="c1">-- return messagepack</span>
<span class="k">return</span> <span class="n">cmsgpack</span><span class="p">.</span><span class="n">pack</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

</code></pre></div></div>

<p>What we found is that between pipelined requests, lua + json, and lua + messagepack, messagepack was the best performer of the three solutions. Our final solution ended up something like this:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="nb">require</span> <span class="s1">'redis'</span>
<span class="nb">require</span> <span class="s1">'msgpack'</span>

<span class="n">keys</span> <span class="o">=</span> <span class="sx">%w(FOO BAR BAZ)</span>

<span class="n">lua_msgpack_loader</span> <span class="o">=</span> <span class="o">&lt;&lt;</span><span class="no">LUA</span><span class="sh">
local collate = function (key)
  local raw_data = redis.call('HGETALL', key)
  local hash_data = {}

  for idx = 1, #raw_data, 2 do
    hash_data[raw_data[idx]] = raw_data[idx + 1]
  end

  return hash_data;
end

local data = {}

for _, key in ipairs(KEYS) do
  data[key] = collate(key)
end

return cmsgpack.pack(data)
</span><span class="no">LUA</span>

<span class="n">redis</span> <span class="o">=</span> <span class="o">::</span><span class="no">Redis</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:driver</span> <span class="o">=&gt;</span> <span class="ss">:hiredis</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="no">MessagePack</span><span class="p">.</span><span class="nf">unpack</span><span class="p">(</span><span class="n">redis</span><span class="p">.</span><span class="nf">eval</span><span class="p">(</span><span class="n">lua_msgpack_loader</span><span class="p">,</span> <span class="ss">:keys</span> <span class="o">=&gt;</span> <span class="n">keys</span><span class="p">))</span>

</code></pre></div></div>

<p>Of course, no post like this would be complete without a benchmark (using 10K keys):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>                      user     system      total        real
lua + json        0.350000   0.010000   0.360000 (  1.242315)
lua + msgpack     0.260000   0.020000   0.280000 (  1.146377)
redis pipelined   1.070000   0.020000   1.090000 (  1.759858)

</code></pre></div></div>

<p>Overall, we were pleasantly surprised by Redis and Lua and it’s definitely a solution we’ll turn to in the future as well.</p>

<hr />

<p><em>This post has been cross-posted to the <a href="http://stdout.tradier.com">Tradier Developer Blog</a>, for more posts like this, you may want to follow our posts there as well!</em></p>]]></content><author><name>Steve Agalloco</name></author><category term="Development" /><summary type="html"><![CDATA[Experimenting with Redis and Lua.]]></summary></entry><entry><title type="html">How To: Create Dropwizard Tasks</title><link href="https://beforeitwasround.com/2014/05/how_to_create_dropwizard_tasks.html" rel="alternate" type="text/html" title="How To: Create Dropwizard Tasks" /><published>2014-05-27T11:45:00+00:00</published><updated>2014-05-27T11:45:00+00:00</updated><id>https://beforeitwasround.com/2014/05/how_to_create_dropwizard_tasks</id><content type="html" xml:base="https://beforeitwasround.com/2014/05/how_to_create_dropwizard_tasks.html"><![CDATA[<p>We’re big fans of the <a href="https://dropwizard.github.io/dropwizard/">Dropwizard</a> framework. It’s straightforward approach to services allows us to create fast and highly available systems very easily. While we’ve used a number of different Java frameworks when building out our platform, Dropwizard is quickly becoming our de-facto standard for all new services.</p>

<p>As we built out our new market data service, we found a need to add a task. Dropwizard’s <a href="https://dropwizard.github.io/dropwizard/manual/index.html">documentation</a> is fantastic, encompassing everything from application structure to internals and semantics, but tasks aren’t covered particularly well. We’ve <a href="https://github.com/dropwizard/dropwizard/pull/596">submitted a pull request</a> to improve the docs, but opted to document our learnings here as well.</p>

<p>The <code class="language-plaintext highlighter-rouge">Task</code> is <a href="https://github.com/dropwizard/dropwizard/blob/master/dropwizard-servlets/src/main/java/io/dropwizard/servlets/tasks/Task.java">defined as an abstract class</a> that you must subclass.  The main thing to point out is the <code class="language-plaintext highlighter-rouge">execute</code> method which is called when the task is invoked. Let’s define a sample task to see how it works.  For this example, we’ll create a task called <code class="language-plaintext highlighter-rouge">TruncateDatabaseTask</code>. First, we’ll define the class:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">package</span> <span class="nn">com.myapp.tasks</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">io.dropwizard.servlets.tasks.Task</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">TruncateDatabaseTask</span> <span class="kd">extends</span> <span class="nc">Task</span> <span class="o">{</span>
<span class="o">}</span>
</code></pre></div></div>

<p>Dropwizard requires that each task have a name. The name has a special purpose as it’s also used to compose the path that the task is accessible from within Dropwizard.  The <code class="language-plaintext highlighter-rouge">Task</code> defines a basic constructor that we can use, but let’s define it explicity via a constructor of our own (more on this later). We’ll also implement the <code class="language-plaintext highlighter-rouge">execute</code> method even though we’re not doing anything yet.</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">package</span> <span class="nn">com.myapp.tasks</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">io.dropwizard.servlets.tasks.Task</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">TruncateDatabaseTask</span> <span class="kd">extends</span> <span class="nc">Task</span> <span class="o">{</span>
  <span class="kd">public</span> <span class="nf">TruncateDatabaseTask</span><span class="o">()</span> <span class="o">{</span>
    <span class="kd">super</span><span class="o">(</span><span class="s">"truncate"</span><span class="o">)</span>
  <span class="o">}</span>

  <span class="nd">@Override</span>
  <span class="kd">public</span> <span class="kt">void</span> <span class="nf">execute</span><span class="o">(</span><span class="nc">ImmutableMultimap</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">,</span> <span class="nc">String</span><span class="o">&gt;</span> <span class="n">parameters</span><span class="o">,</span> <span class="nc">PrintWriter</span> <span class="n">output</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
  <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>

<p>At this point, we have a fully composed Dropwizard task. Now we just need to add it to our application:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">(</span><span class="nc">MyAppConfiguration</span> <span class="n">config</span><span class="o">,</span> <span class="nc">Environment</span> <span class="n">env</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>

  <span class="c1">// tasks</span>
  <span class="n">env</span><span class="o">.</span><span class="na">admin</span><span class="o">().</span><span class="na">addTask</span><span class="o">(</span><span class="k">new</span> <span class="nc">TruncateDatabaseTask</span><span class="o">());</span>
<span class="o">}</span>
</code></pre></div></div>

<p>Start your application, and you’ll see that the task is now registered alongside the garbage collection task that Dropwizard adds automatically</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>INFO  [2014-05-16 19:15:35,564] io.dropwizard.setup.AdminEnvironment: tasks =

    POST    /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)
    POST    /tasks/truncate (com.myapp.tasks.TruncateDatabaseTask)
</code></pre></div></div>

<p>Let’s make this a more practical example. The value of defining a constructor is that we can dependency inject anything we need into the task from our Dropwizard application:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">package</span> <span class="nn">com.myapp.tasks</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">io.dropwizard.servlets.tasks.Task</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">TruncateDatabaseTask</span> <span class="kd">extends</span> <span class="nc">Task</span> <span class="o">{</span>
  <span class="kd">private</span> <span class="nc">Database</span> <span class="n">database</span><span class="o">;</span>

  <span class="kd">public</span> <span class="nf">TruncateDatabaseTask</span><span class="o">(</span><span class="nc">Database</span> <span class="n">database</span><span class="o">)</span> <span class="o">{</span>
    <span class="kd">super</span><span class="o">(</span><span class="s">"truncate"</span><span class="o">)</span>
    <span class="k">this</span><span class="o">.</span><span class="na">database</span> <span class="o">=</span> <span class="n">database</span><span class="o">;</span>
  <span class="o">}</span>

  <span class="nd">@Override</span>
  <span class="kd">public</span> <span class="kt">void</span> <span class="nf">execute</span><span class="o">(</span><span class="nc">ImmutableMultimap</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">,</span> <span class="nc">String</span><span class="o">&gt;</span> <span class="n">parameters</span><span class="o">,</span> <span class="nc">PrintWriter</span> <span class="n">output</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
    <span class="k">this</span><span class="o">.</span><span class="na">database</span><span class="o">.</span><span class="na">truncate</span><span class="o">();</span>
  <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>

<p>Most of our Dropwizard tasks look a lot like the one above. We found these tasks valuable as schedulable components of our application without building custom scripts to tie back to the current runtime.  If you use Sinatra or Express to build service APIs definitely check out <a href="https://dropwizard.github.io/dropwizard/">Dropwizard</a>.</p>

<hr />

<p><em>This post has been cross-posted to the <a href="http://stdout.tradier.com">Tradier Developer Blog</a>, for more posts like this, you may want to follow our posts there as well!</em></p>]]></content><author><name>Steve Agalloco</name></author><category term="Development" /><summary type="html"><![CDATA[Learn how to create tasks inside the Java based Dropwizard framework.]]></summary></entry><entry><title type="html">Sending Rails Deprecation Warnings to Sentry</title><link href="https://beforeitwasround.com/2014/02/sending-rails-deprecation-warnings-to-sentry.html" rel="alternate" type="text/html" title="Sending Rails Deprecation Warnings to Sentry" /><published>2014-02-23T00:00:00+00:00</published><updated>2014-02-23T00:00:00+00:00</updated><id>https://beforeitwasround.com/2014/02/sending-rails-deprecation-warnings-to-sentry</id><content type="html" xml:base="https://beforeitwasround.com/2014/02/sending-rails-deprecation-warnings-to-sentry.html"><![CDATA[<p>If you’ve upgraded a Rails app recently, you are no stranger to deprecation warnings. As I’ve been going through some of our apps (most of which were running 3.2) and upgrading them, I’ve wanted a way to get notified of these warnings without having to scan the logs.</p>

<p>We use <a href="https://getsentry.com">Sentry</a> to manage our exception handling, so I figured there must be a way to handle these warnings just like exceptions.  Since I had to read a little bit of Rails code to figure this out, I figured I’d share what I learned.  ActiveSupport allows you to define one or more deprecation behaviors by using  <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/deprecation/behaviors.rb#L71-L73"><code class="language-plaintext highlighter-rouge">ActiveSupport::Deprecation.behavior=</code></a>. Several options are included by default:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">:raise</code> - raise an exception instead of warning</li>
  <li><code class="language-plaintext highlighter-rouge">:stderr</code> - log them to <code class="language-plaintext highlighter-rouge">$stderr</code></li>
  <li><code class="language-plaintext highlighter-rouge">:log</code> - log them out using <code class="language-plaintext highlighter-rouge">Rails.logger</code></li>
  <li><code class="language-plaintext highlighter-rouge">:notify</code> - publish an alert using <a href="http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html">ActiveSupport Notifications</a></li>
  <li><code class="language-plaintext highlighter-rouge">:silence</code> - sweep them under a rug and pretend they never existed, until they are truly deprecated and bite you.</li>
</ul>

<p>Based on the <a href="http://api.rubyonrails.org/classes/ActiveSupport/Deprecation/Behavior.html">documentation</a>, you can also pass a custom handler class or a proc, basically, anything that responds to <code class="language-plaintext highlighter-rouge">call</code> can be used. When a deprecation warning is issued, the list of behaviors is looped through and passed the deprecation message and callstack:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">warn</span><span class="p">(</span><span class="n">message</span> <span class="o">=</span> <span class="kp">nil</span><span class="p">,</span> <span class="n">callstack</span> <span class="o">=</span> <span class="kp">nil</span><span class="p">)</span>
  <span class="k">return</span> <span class="k">if</span> <span class="n">silenced</span>

  <span class="n">callstack</span> <span class="o">||=</span> <span class="nb">caller</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
  <span class="n">deprecation_message</span><span class="p">(</span><span class="n">callstack</span><span class="p">,</span> <span class="n">message</span><span class="p">).</span><span class="nf">tap</span> <span class="k">do</span> <span class="o">|</span><span class="n">m</span><span class="o">|</span>
    <span class="n">behavior</span><span class="p">.</span><span class="nf">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">b</span><span class="o">|</span> <span class="n">b</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">callstack</span><span class="p">)</span> <span class="p">}</span>
  <span class="k">end</span>
<span class="k">end</span>
</code></pre></div></div>

<p>By default, Rails uses <code class="language-plaintext highlighter-rouge">:stderr</code>. I wanted to keep that behavior, but also include  Sentry. ActiveSupport Notifications are a perfect way to do that. Using the raven gem to hook into Sentry, here’s where I landed:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Deprecation</span><span class="p">.</span><span class="nf">behavior</span> <span class="o">=</span> <span class="p">[</span><span class="ss">:stderr</span><span class="p">,</span> <span class="ss">:notify</span><span class="p">]</span>
<span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Notifications</span><span class="p">.</span><span class="nf">subscribe</span><span class="p">(</span><span class="s1">'deprecation.rails'</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="nb">name</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">finish</span><span class="p">,</span> <span class="nb">id</span><span class="p">,</span> <span class="n">payload</span><span class="o">|</span>
  <span class="no">Raven</span><span class="p">.</span><span class="nf">capture_message</span><span class="p">(</span><span class="s2">"DEPRECATION WARNING"</span><span class="p">,</span> <span class="p">{</span>
    <span class="ss">:logger</span> <span class="o">=&gt;</span> <span class="s1">'logger'</span><span class="p">,</span>
    <span class="ss">:extra</span>  <span class="o">=&gt;</span> <span class="p">{</span>
      <span class="s1">'message'</span>   <span class="o">=&gt;</span> <span class="n">payload</span><span class="p">[</span><span class="ss">:message</span><span class="p">],</span>
      <span class="s1">'backtrace'</span> <span class="o">=&gt;</span> <span class="n">payload</span><span class="p">[</span><span class="ss">:callstack</span><span class="p">].</span><span class="nf">join</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">  "</span><span class="p">)</span>
    <span class="p">},</span>
    <span class="ss">:tags</span>   <span class="o">=&gt;</span> <span class="p">{</span>
      <span class="s1">'environment'</span> <span class="o">=&gt;</span> <span class="no">Rails</span><span class="p">.</span><span class="nf">env</span>
    <span class="p">}</span>
  <span class="p">})</span>
<span class="k">end</span>
</code></pre></div></div>]]></content><author><name>Steve Agalloco</name></author><category term="rails" /><category term="sentry" /><summary type="html"><![CDATA[If you’ve upgraded a Rails app recently, you are no stranger to deprecation warnings. As I’ve been going through some of our apps (most of which were running 3.2) and upgrading them, I’ve wanted a way to get notified of these warnings without having to scan the logs.]]></summary></entry><entry><title type="html">Auto-bcc’ing emails sent from Devise</title><link href="https://beforeitwasround.com/2014/02/auto-bcc-ing-emails-sent-from-devise.html" rel="alternate" type="text/html" title="Auto-bcc’ing emails sent from Devise" /><published>2014-02-10T00:00:00+00:00</published><updated>2014-02-10T00:00:00+00:00</updated><id>https://beforeitwasround.com/2014/02/auto-bcc-ing-emails-sent-from-devise</id><content type="html" xml:base="https://beforeitwasround.com/2014/02/auto-bcc-ing-emails-sent-from-devise.html"><![CDATA[<p>While digging through the <a href="https://github.com/plataformatec/devise">Devise</a> source some time back, I found a little known and undocumented feature that offers some great flexibility: you can define the parent class for the <code class="language-plaintext highlighter-rouge">DeviseController</code> and <code class="language-plaintext highlighter-rouge">DeviseMailer</code>. Devise uses sane defaults and so these classes inherit from <code class="language-plaintext highlighter-rouge">ApplicationController</code> and <code class="language-plaintext highlighter-rouge">ActionMailer::Base</code> respectively. These are defined as module accessors, on the <code class="language-plaintext highlighter-rouge">Devise</code> module. Devise uses this configuration when defining the <code class="language-plaintext highlighter-rouge">DeviseMailer</code>:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Devise::Mailer</span> <span class="o">&lt;</span> <span class="no">Devise</span><span class="p">.</span><span class="nf">parent_mailer</span><span class="p">.</span><span class="nf">constantize</span>
  <span class="kp">include</span> <span class="no">Devise</span><span class="o">::</span><span class="no">Mailers</span><span class="o">::</span><span class="no">Helpers</span>

  <span class="c1"># there's more stuff in here, you should read the source!</span>
<span class="k">end</span>
</code></pre></div></div>

<p>Why is this useful? There are times when it’s convenient to be able to change this class. For instance, let’s suppose we want to automatically bcc an email address on new emails delivered from Devise. I needed to do this recently and found it to be quite easy using the <code class="language-plaintext highlighter-rouge">parent_mailer</code> configuration.</p>

<p>First, create a new mailer class that inherits from <code class="language-plaintext highlighter-rouge">ActionMailer::Base</code> and put it in <code class="language-plaintext highlighter-rouge">app/mailers</code>, let’s call it <code class="language-plaintext highlighter-rouge">BaseMailer</code>. The simplest example would be an empty class:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">BaseMailer</span> <span class="o">&lt;</span> <span class="no">ActionMailer</span><span class="o">::</span><span class="no">Base</span>
<span class="k">end</span>
</code></pre></div></div>

<p>Then, in the initializer (typically <code class="language-plaintext highlighter-rouge">config/initializers/devise.rb</code>), instruct Devise to use this class as the <code class="language-plaintext highlighter-rouge">parent_mailer</code>:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">Devise</span><span class="p">.</span><span class="nf">setup</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
  <span class="n">config</span><span class="p">.</span><span class="nf">parent_mailer</span> <span class="o">=</span> <span class="s1">'BaseMailer'</span>
<span class="k">end</span>
</code></pre></div></div>

<p>For the sake of brevity, I’ve omitted the rest of the Devise configuration, but you’ll want to add the <code class="language-plaintext highlighter-rouge">parent_mailer</code> alongside any other configuration items. Now <code class="language-plaintext highlighter-rouge">DeviseMailer</code> will inherit from our mailer class.  Now, it’s just a matter of hooking in our auto-bcc logic:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">BaseMailer</span> <span class="o">&lt;</span> <span class="no">ActionMailer</span><span class="o">::</span><span class="no">Base</span>

  <span class="kp">protected</span>

  <span class="k">def</span> <span class="nf">mail_with_bcc</span><span class="p">(</span><span class="n">headers</span><span class="o">=</span><span class="p">{},</span> <span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
    <span class="n">headers</span><span class="p">.</span><span class="nf">merge!</span><span class="p">(</span><span class="ss">:bcc</span> <span class="o">=&gt;</span> <span class="s1">'me@example.com'</span><span class="p">)</span>
    <span class="n">mail_without_bcc</span><span class="p">(</span><span class="n">headers</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
  <span class="k">end</span>
  <span class="n">alias_method_chain</span> <span class="ss">:mail</span><span class="p">,</span> <span class="ss">:bcc</span>
<span class="k">end</span>
</code></pre></div></div>

<p>As you can tell, I really enjoy reading other people’s code. Especially when you are pleasantly surprised by a feature that solves a problem you are trying to solve!</p>]]></content><author><name>Steve Agalloco</name></author><category term="devise" /><category term="actionmailer" /><summary type="html"><![CDATA[While digging through the Devise source some time back, I found a little known and undocumented feature that offers some great flexibility: you can define the parent class for the DeviseController and DeviseMailer. Devise uses sane defaults and so these classes inherit from ApplicationController and ActionMailer::Base respectively. These are defined as module accessors, on the Devise module. Devise uses this configuration when defining the DeviseMailer:]]></summary></entry><entry><title type="html">Konacha and ActionController callbacks</title><link href="https://beforeitwasround.com/2013/12/konacha-and-actioncontroller-callbacks.html" rel="alternate" type="text/html" title="Konacha and ActionController callbacks" /><published>2013-12-04T00:00:00+00:00</published><updated>2013-12-04T00:00:00+00:00</updated><id>https://beforeitwasround.com/2013/12/konacha-and-actioncontroller-callbacks</id><content type="html" xml:base="https://beforeitwasround.com/2013/12/konacha-and-actioncontroller-callbacks.html"><![CDATA[<p>At <a href="https://tradier.com">Tradier</a>, we are big fans of TDD. We’re quite comfortable testing application and server-based code but front-end testing was relatively new to us. After looking at several options we decided on <a href="http://visionmedia.github.io/mocha/">Mocha</a> as our test framework of choice and we’ve looked to <a href="https://github.com/jfirebaugh/konacha">Konacha</a> to conveniently test from our Rails apps. Our suite was running fine, until we introduced the <a href="https://github.com/airblade/paper_trail">PaperTrail</a> gem. We started seeing the following error:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>NoMethodError: undefined method 'authenticate' for nil:NilClass
  ~/Projects/myapp/.bundle/gems/devise-3.0.1/lib/devise/controllers/helpers.rb:56:in 'current_user'
  ~/Projects/myapp/.bundle/gems/paper_trail-2.7.2/lib/paper_trail/controller.rb:17:in 'user_for_paper_trail'
  ~/Projects/myapp/.bundle/gems/paper_trail-2.7.2/lib/paper_trail/controller.rb:59:in 'set_paper_trail_whodunnit'
</code></pre></div></div>

<p>What’s going on here? Konacha’s <code class="language-plaintext highlighter-rouge">SpecsController</code> inherits from <code class="language-plaintext highlighter-rouge">ActionController::Base</code> so that it can render ERB templates and layouts which setup a browser environment to run the test suite. Pretty standard stuff for a Rails Engine. That brings us to PaperTrail and it’s controller callbacks. By default, PaperTrail enables itself for all controllers, <code class="language-plaintext highlighter-rouge">SpecsController</code> included.  If we look at <code class="language-plaintext highlighter-rouge">user_for_paper_trail</code>, we see the following:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">user_for_paper_trail</span>
  <span class="n">current_user</span> <span class="k">if</span> <span class="k">defined?</span><span class="p">(</span><span class="n">current_user</span><span class="p">)</span>
<span class="k">end</span>
</code></pre></div></div>

<p>It’s triggering <code class="language-plaintext highlighter-rouge">current_user</code> which invoked Devise, causing the exception above. Clearly, PaperTrail’s user fingerprinting are interfering with Konacha. To fix this, we simply need to disable PaperTrail in the <code class="language-plaintext highlighter-rouge">SpecsController</code>. In addition to your Konacha configuration, you’ll want to have something like this in <code class="language-plaintext highlighter-rouge">config/initializers/konacha.rb</code>:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="k">defined?</span><span class="p">(</span><span class="no">Konacha</span><span class="p">)</span>
  <span class="nb">require</span> <span class="s1">'capybara/poltergeist'</span>
  <span class="no">Konacha</span><span class="p">.</span><span class="nf">configure</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
    <span class="n">config</span><span class="p">.</span><span class="nf">spec_dir</span>    <span class="o">=</span> <span class="s2">"spec/javascripts"</span>
    <span class="n">config</span><span class="p">.</span><span class="nf">driver</span>      <span class="o">=</span> <span class="ss">:poltergeist</span>
    <span class="n">config</span><span class="p">.</span><span class="nf">stylesheets</span> <span class="o">=</span> <span class="sx">%w(application)</span>
  <span class="k">end</span>

  <span class="no">Konacha</span><span class="o">::</span><span class="no">SpecsController</span><span class="p">.</span><span class="nf">class_eval</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">paper_trail_enabled_for_controller</span>
      <span class="kp">false</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>
</code></pre></div></div>

<p>With that in place, our tests started to work again. Konacha and Mocha are a nice combination for testing JavaScript, be sure to check them out. Happy testing!</p>]]></content><author><name>Steve Agalloco</name></author><category term="ruby" /><category term="konacha" /><summary type="html"><![CDATA[At Tradier, we are big fans of TDD. We’re quite comfortable testing application and server-based code but front-end testing was relatively new to us. After looking at several options we decided on Mocha as our test framework of choice and we’ve looked to Konacha to conveniently test from our Rails apps. Our suite was running fine, until we introduced the PaperTrail gem. We started seeing the following error:]]></summary></entry><entry><title type="html">Serving Multiple Rails Apps Under One Virtual Host with Phusion Passenger</title><link href="https://beforeitwasround.com/2013/03/serving-multiple-rails-apps-under-one-virtual-host-with-phusion-passenger.html" rel="alternate" type="text/html" title="Serving Multiple Rails Apps Under One Virtual Host with Phusion Passenger" /><published>2013-03-31T00:00:00+00:00</published><updated>2013-03-31T00:00:00+00:00</updated><id>https://beforeitwasround.com/2013/03/serving-multiple-rails-apps-under-one-virtual-host-with-phusion-passenger</id><content type="html" xml:base="https://beforeitwasround.com/2013/03/serving-multiple-rails-apps-under-one-virtual-host-with-phusion-passenger.html"><![CDATA[<p>A while back I was tasked with a unique problem I hadn’t encountered before. I was running two Rails apps, both of which needed to be hosted under a single domain - one served from root, and the other from a directory.  Here was my ideal setup:</p>

<ul>
  <li>app1 deployed to <code class="language-plaintext highlighter-rouge">/apps/app1/current</code> at mydomain.com</li>
  <li>app2 deployed to <code class="language-plaintext highlighter-rouge">/apps/app2/current</code> at mydomain.com/app2</li>
</ul>

<p>This is easily accomplished with Passenger, but took some tinkering to figure out. Here’s what I ended up with:</p>

<pre><code class="language-apacheconf">&lt;VirtualHost *:80&gt;
  ServerName mydomain.com
  DocumentRoot "/apps/app1/current/public"
  RailsEnv production

  &lt;Directory "/apps/app1/current/public"&gt;
    Options Indexes FollowSymLinks -MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  &lt;/Directory&gt;

  &lt;Directory /apps/app1/current/public/app2&gt;
    Options Indexes FollowSymLinks -MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  &lt;/Directory&gt;

  RackBaseURI /app2
&lt;/VirtualHost&gt;
</code></pre>

<p>Apache is now properly configured. All that remains is a little setup in the apps themselves. Since the Apache config expects a Rack/Rails app at <code class="language-plaintext highlighter-rouge">/apps/app1/current/public/app2</code>, you just need to create a symlink in <code class="language-plaintext highlighter-rouge">/apps/app1/current/public</code> to app2:</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">ln</span> <span class="nt">-s</span> /apps/app2/current/public app2
</code></pre></div></div>

<p>Depending upon your deployment strategy, you may need to create the symlink each time you deploy. I setup a symlink as part of the capistrano deploy and everything just worked™.</p>

<p><strong>Bonus</strong>: If you use a common datastore and session_store key within your Rails applications, you can share authentication between the two apps to create a cohesive “logged-in” interface.</p>]]></content><author><name>Steve Agalloco</name></author><summary type="html"><![CDATA[A while back I was tasked with a unique problem I hadn’t encountered before. I was running two Rails apps, both of which needed to be hosted under a single domain - one served from root, and the other from a directory. Here was my ideal setup:]]></summary></entry><entry><title type="html">Using CarrierWave and Fog With Private Rackspace Cloud Files</title><link href="https://beforeitwasround.com/2013/01/using-carrierwave-and-fog-with-private-rackspace-cloud-files.html" rel="alternate" type="text/html" title="Using CarrierWave and Fog With Private Rackspace Cloud Files" /><published>2013-01-08T00:00:00+00:00</published><updated>2013-01-08T00:00:00+00:00</updated><id>https://beforeitwasround.com/2013/01/using-carrierwave-and-fog-with-private-rackspace-cloud-files</id><content type="html" xml:base="https://beforeitwasround.com/2013/01/using-carrierwave-and-fog-with-private-rackspace-cloud-files.html"><![CDATA[<p>During a recent project, I used <a href="http://www.rackspace.com/cloud/files/">Rackspace Cloud Files</a> (an S3 equivalent run by Rackspace) to store mp3 files. Since the files I was working with were attached to ActiveRecord models, I immediately turned to <a href="https://github.com/jnicklas/carrierwave">CarrierWave</a>.  CarrierWave makes this really easy, simply configure appropriately:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">CarrierWave</span><span class="p">.</span><span class="nf">configure</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
  <span class="n">config</span><span class="p">.</span><span class="nf">fog_credentials</span> <span class="o">=</span> <span class="p">{</span>
    <span class="ss">:provider</span>               <span class="o">=&gt;</span> <span class="s1">'Rackspace'</span><span class="p">,</span>
    <span class="ss">:rackspace_username</span>     <span class="o">=&gt;</span> <span class="no">ENV</span><span class="p">[</span><span class="s1">'RACKSPACE_USERNAME'</span><span class="p">],</span>
    <span class="ss">:rackspace_api_key</span>      <span class="o">=&gt;</span> <span class="no">ENV</span><span class="p">[</span><span class="s1">'RACKSPACE_API_KEY'</span><span class="p">],</span>
    <span class="ss">:rackspace_servicenet</span>   <span class="o">=&gt;</span> <span class="no">Rails</span><span class="p">.</span><span class="nf">env</span><span class="p">.</span><span class="nf">production?</span><span class="p">,</span>
    <span class="ss">:rackspace_temp_url_key</span> <span class="o">=&gt;</span> <span class="no">ENV</span><span class="p">[</span><span class="s1">'RACKSPACE_URL_KEY'</span><span class="p">]</span>
  <span class="p">}</span>
  <span class="n">config</span><span class="p">.</span><span class="nf">fog_directory</span> <span class="o">=</span> <span class="s1">'mp3s'</span>
  <span class="n">config</span><span class="p">.</span><span class="nf">fog_public</span> <span class="o">=</span> <span class="kp">false</span>
<span class="k">end</span>
</code></pre></div></div>

<p>Add an Uploader class to your model and you are nearly all the way there:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">RecordingUploader</span> <span class="o">&lt;</span> <span class="no">CarrierWave</span><span class="o">::</span><span class="no">Uploader</span><span class="o">::</span><span class="no">Base</span>
  <span class="n">storage</span> <span class="ss">:fog</span>
<span class="k">end</span>

<span class="k">class</span> <span class="nc">Recording</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="n">mount_uploader</span> <span class="ss">:file</span><span class="p">,</span> <span class="no">RecordingUploader</span>
<span class="k">end</span>
</code></pre></div></div>

<p>But, I hit a snag. Not wanting the files I was uploading to be publicly available, I had configured <code class="language-plaintext highlighter-rouge">fog_public = false</code> in the CarrierWave configuration. But when I tried accessing the files from my AR model, I kept getting errors.  At time of writing, the latest version of CarrierWave (0.7.1) did not support private Cloud Files. Fortunately for me, CarrierWave master did.  Assuming that was all I needed, I was hopeful that my problem was fully solved but I was still having issues.</p>

<p>What I had missed was the <code class="language-plaintext highlighter-rouge">rackspace_temp_url_key</code>.  This is something you need to established before use.  I couldn’t find a way to do this on Rackspace’s website, but fortunately, <a href="https://github.com/fog/fog">Fog</a> exposes an easy way to set this via API. I stuck a little bin script in my <code class="language-plaintext highlighter-rouge">scripts</code> directory and ran it to set a key:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">#!/usr/bin/env ruby</span>

<span class="nb">require</span> <span class="s1">'rubygems'</span>
<span class="nb">require</span> <span class="s1">'bundler/setup'</span>
<span class="nb">require</span> <span class="s1">'fog'</span>

<span class="n">storage</span> <span class="o">=</span> <span class="no">Fog</span><span class="o">::</span><span class="no">Storage</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:rackspace_username</span> <span class="o">=&gt;</span> <span class="no">ENV</span><span class="p">[</span><span class="s1">'RACKSPACE_USERNAME'</span><span class="p">],</span>
                           <span class="ss">:rackspace_api_key</span>  <span class="o">=&gt;</span> <span class="no">ENV</span><span class="p">[</span><span class="s1">'RACKSPACE_API_KEY'</span><span class="p">],</span>
                           <span class="ss">:provider</span>           <span class="o">=&gt;</span> <span class="s1">'Rackspace'</span><span class="p">)</span>
<span class="n">storage</span><span class="p">.</span><span class="nf">post_set_meta_temp_url_key</span><span class="p">(</span><span class="no">ENV</span><span class="p">[</span><span class="s1">'URL_KEY'</span><span class="p">])</span>
</code></pre></div></div>

<p>Once the <code class="language-plaintext highlighter-rouge">rackspace_temp_url_key</code> was properly setup, I was all set. Private Cloud Files accessible using CarrierWave.</p>]]></content><author><name>Steve Agalloco</name></author><summary type="html"><![CDATA[During a recent project, I used Rackspace Cloud Files (an S3 equivalent run by Rackspace) to store mp3 files. Since the files I was working with were attached to ActiveRecord models, I immediately turned to CarrierWave. CarrierWave makes this really easy, simply configure appropriately:]]></summary></entry><entry><title type="html">Using Ruby to post to a Facebook Page’s wall</title><link href="https://beforeitwasround.com/2010/12/using-ruby-to-post-to-a-facebook-pages-wall.html" rel="alternate" type="text/html" title="Using Ruby to post to a Facebook Page’s wall" /><published>2010-12-22T00:00:00+00:00</published><updated>2010-12-22T00:00:00+00:00</updated><id>https://beforeitwasround.com/2010/12/using-ruby-to-post-to-a-facebook-pages-wall</id><content type="html" xml:base="https://beforeitwasround.com/2010/12/using-ruby-to-post-to-a-facebook-pages-wall.html"><![CDATA[<p>I was recently tasked with what seemed like a fairly simple request: Post to a Facebook Page via the Facebook API.  This proved to be no small feat.  I’ve decided to document my steps here to serve as a help to others, and a reminder to myself should I ever need to revisit this in the future.</p>

<p>Why was this so hard, you ask?  For starters, Facebook’s documentation is particularly lacking.  Most of the documentation leads one to believe it’s really only good for reading from the API.  It’s difficult to understand how you can create anything.  Further muddying the waters is the amount of irrelevant information out there since the release of the Graph API.  They’ve changed their product so much is such a short period of time that it’s hard to tell what’s relevant and what isn’t.  Many tutorials you’ll find won’t work because they speak to the old REST API that is now deprecated.  In other words, separating old from new is a bit of a needle/haystack problem.</p>

<p>So where to start, first read up on Facebook API <a href="http://developers.facebook.com/docs/authentication/">authentication</a>.  So we’re going to authenticate via OAuth and once authenticated, make calls against the API.  But how do we do this as a page instead of as ourself?  Facebook devotes 2 paragraphs to it in their <a href="http://developers.facebook.com/docs/api">documentation</a> under a section titled “Page impersonation”.  I highly recommend you read it, especially this: <em>“Once a user has granted your application the “manage_pages” permission, the “accounts” connection will yield an additional access_token property for every page administrated by the current user. These access_tokens can be used to make calls on behalf of a page. The permissions granted by a user to your application will now also be applicable to their pages.”</em></p>

<p>They’ve laid it out for us:</p>

<ul>
  <li>grant your application the ‘manage_pages’ permission</li>
  <li>use ‘accounts’ to yield an access_token for the page</li>
  <li>use the access_token to make calls on behalf of the page</li>
</ul>

<p>I’m going to show you how to do it.  Let’s assume you’ve done the following:</p>

<ul>
  <li>created a Facebook Page</li>
  <li>created a Facebook app</li>
  <li>added the app to your profile</li>
</ul>

<p>If you haven’t done that yet, go right ahead, we’ll wait for you to come back.  Make note of the ids of your app and your page, we’ll need those shortly.  Now, we’ll need to retrieve an access token for our app so that we can use it to make Graph API calls.  The easiest way to do this is to go to the browser and enter a URL that looks like the following:</p>

<p><code class="language-plaintext highlighter-rouge">https://graph.facebook.com/oauth/authorize?type=user_agent&amp;client_id=&lt;APPID&gt;&amp;redirect_uri=&lt;CALLBACK_URL&gt;&amp;scope=manage_pages,offline_access,publish_stream</code></p>

<p>The comma delimited options we are passing to the scope argument specify what <a href="http://developers.facebook.com/docs/authentication/permissions">permissions</a> we are granting to the app.  offline_access allows us to use the access_token outside of a user session.  Without this, the token will only be valid during your user session, when you logout, the token expires.  We also need to allow the app to manage_pages that we administrate.  Lastly, publish_stream is what allows us to create content via the API.</p>

<p>Visiting that link will bring you to an authorization page where Facebook will prompt you to verify that you want to allow your app access to the privileges you specified above.  When you click approve, you’ll be redirected to your callback URL and Facebook will pass your access token in the URL:</p>

<p><code class="language-plaintext highlighter-rouge">your_callback_url?access_token=&lt;YOUR_TOKEN_HERE&gt;&amp;expires_in=0</code></p>

<p>Make sure to record the access token.  Armed with that, you can now make authenticated requests against the Graph API.  At this point, we should have all we need to start working with Ruby.  There are a number of Rubygems out there in varying stages of development that support the Graph API.  I was previously familiar with <a href="http://www.elevatedcode.com/mike-mangino">Mike Mangino’s</a> <a href="http://facebooker.rubyforge.org/">facebooker</a> rubygem, but it’s meant to work against the deprecated REST API.  I was then turned on to <a href="https://github.com/mmangino/facebooker2">Facebooker2</a> which is a revamp of the original Facebooker gem meant to work with Facebook connect.  That’s not really what we’re looking for, but I tried working with Facebooker2 for a while before realizing it wasn’t the perfect fit for what I was trying to do.  Inevitably, with this sort of thing, I prefer to dig down into to the code and the tests to see how the gem works.  As it turns out, the bulk of Facebooker2 is really a Rails-friendly wrapper around another gem by Mike Mangino called <a href="https://github.com/mmangino/mogli">mogli</a>.</p>

<p>With Mogli, we can very easily obtain a client that we’ll use to engage with the API:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">access_token</span> <span class="o">=</span> <span class="s1">'my_facebook_access_token'</span>
<span class="n">page_id</span> <span class="o">=</span> <span class="s1">'my_facebook_page_id'</span>

<span class="n">client</span> <span class="o">=</span> <span class="no">Mogli</span><span class="o">::</span><span class="no">Client</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">access_token</span><span class="p">)</span>
<span class="n">user</span> <span class="o">=</span> <span class="no">Mogli</span><span class="o">::</span><span class="no">User</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="s2">"me"</span><span class="p">,</span><span class="n">client</span><span class="p">)</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">user</span><span class="p">.</span><span class="nf">accounts</span><span class="p">.</span><span class="nf">select</span> <span class="k">do</span> <span class="o">|</span><span class="n">account</span><span class="o">|</span>
  <span class="n">account</span><span class="p">.</span><span class="nf">id</span><span class="p">.</span><span class="nf">to_s</span> <span class="o">==</span> <span class="n">page_id</span>
<span class="k">end</span><span class="p">.</span><span class="nf">first</span>

<span class="n">page</span> <span class="o">=</span> <span class="no">Mogli</span><span class="o">::</span><span class="no">Page</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:access_token</span> <span class="o">=&gt;</span> <span class="n">page</span><span class="p">.</span><span class="nf">access_token</span><span class="p">)</span>
<span class="n">page_client</span> <span class="o">=</span> <span class="n">page</span><span class="p">.</span><span class="nf">client_for_page</span>
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">user.accounts</code> will return an array of accounts which will list any pages and apps you own.  I added a select because we want to manage a specific page so we need to retrieve the page based on the page_id.  Then we use that page’s access token to create a new facebook client that we can use to work on behalf of the page.  Now, we just need to use the client to create our wall post:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">post_data</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">post_data</span><span class="p">[</span><span class="ss">:name</span><span class="p">]</span>    <span class="o">=</span> <span class="s1">'Title for my link'</span>
<span class="n">post_data</span><span class="p">[</span><span class="ss">:link</span><span class="p">]</span>    <span class="o">=</span> <span class="s1">'http://path.to/my/link'</span>
<span class="n">post_data</span><span class="p">[</span><span class="ss">:caption</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'A caption'</span>
<span class="n">post_data</span><span class="p">[</span><span class="ss">:description</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'A description'</span>
<span class="n">post_data</span><span class="p">[</span><span class="ss">:picture</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'http://path.to/myimage.jpg'</span>
<span class="n">post_data</span><span class="p">[</span><span class="ss">:actions</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
  <span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">'My site name'</span><span class="p">,</span>
  <span class="ss">:link</span> <span class="o">=&gt;</span> <span class="s1">'http://link.to/my/site'</span>
<span class="p">}.</span><span class="nf">to_json</span>

<span class="n">client</span><span class="p">.</span><span class="nf">post</span><span class="p">(</span><span class="s2">"feed"</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="n">post_data</span><span class="p">)</span>
</code></pre></div></div>

<p>What I’ve demonstrated above is a use-case where you’d post a link with a thumbnail image.  Facebook specifies quite a few other things that you can include with your <a href="http://developers.facebook.com/docs/reference/api/post">post</a>.  A few things are not relevant to page publishing, for instance you cannot set privacy on a page’s wall post as it’s visible to all people that like that page.</p>

<p>Mogli does include a <code class="language-plaintext highlighter-rouge">Mogli::Post</code> class but I wasn’t able to get it to work without getting an error response from the API.  Fortunately it also supports passing a simple hash of attributes.  Notice that I’m serializing the actions hash to JSON before passing it to the hash.  That threw me for a real loop, but Facebook very literally wants JSON objects passed as strings.</p>

<p>Executing the above code should result in your post appearing on your pages wall.  Congratulations - you made it.  Happy wall posting!</p>]]></content><author><name>Steve Agalloco</name></author><summary type="html"><![CDATA[Posting to a Facebook wall on behalf of a Page is easy, except when it isn't.]]></summary></entry><entry><title type="html">Woot, RSS, Streams and Twitter</title><link href="https://beforeitwasround.com/2010/09/woot-rss-streams-and-twitter.html" rel="alternate" type="text/html" title="Woot, RSS, Streams and Twitter" /><published>2010-09-27T00:00:00+00:00</published><updated>2010-09-27T00:00:00+00:00</updated><id>https://beforeitwasround.com/2010/09/woot-rss-streams-and-twitter</id><content type="html" xml:base="https://beforeitwasround.com/2010/09/woot-rss-streams-and-twitter.html"><![CDATA[<p>Last week there was a <a href="http://woot.com">woot-off</a>.  While I’ve only purchased something from Woot once or times before, the woot-offs are really cool because you never know what they might put up there.  I decided it’d be a fun side project to right a little script to post the latest woot to Campfire and the script was <a href="http://gist.github.com/590089">trivially simple</a> to write.  <a href="http://tinder.rubyforge.org/">Tinder</a> makes it really easy to interact with Campfire so I just had to figure out a way to get the current Woot.  I used woot’s RSS feed to give me this data.  It was pretty easy: fetch the feed, parse it, loop through the entries and find the first item in the ‘woot’ category, a little more parsing and voila.  I had it.</p>

<p>(I realized after the fact that woot already has a twitter account strictly for wootoffs, but it was far from complete enough to replace what I’d built.  For starters, I would have had to parse the text of the tweet and even then, it didn’t include a direct link to the product or a link to an image.)</p>

<p>What dawned on me was what a poor job RSS does for this sort of thing.  I had to continually poll (every 30 seconds) to see if anything had been updated, at some points nothing changed for nearly an hour.  I see why technologies like pubsubhubbub and rssCloud are a potential replacements.  But why haven’t they taken off?  Well, for one, the name pubsubhubbub is a little obtuse, but moreso, it just seems a little complicated.  When you have to create a <a href="http://www.youtube.com/watch?v=B5kHx0rGkec">youtube video</a> to explain your technology in overly simplified terms like the pubsubhubbub team did, you aren’t off to all that great of a start.  Wordpress adopted rssCloud but has it made any difference?  We’re over a year in and neither technology seems to have taken hold in any significant way.</p>

<p>Not foreign to continual polling, Twitter took a different approach, introducing a <a href="http://dev.twitter.com/pages/streaming_api">streaming api</a>.  I can’t help but think this is a much easier and more dynamic means of delivery.  But who wants to consume a bunch of different streams?  Well, pretty soon, we may not have to.  Twitter has also been working on <a href="http://dev.twitter.com/pages/annotations_overview">annotations</a>.  Woot could simply annotate their tweets and anyone writing a service around it just subscribes to the stream.  Awesome.  Twitter annotations is an amazingly powerful idea wrapped up in the simplicity of their <strong>service</strong> and I can’t wait to see what people do with it.</p>]]></content><author><name>Steve Agalloco</name></author><summary type="html"><![CDATA[Last week there was a woot-off. While I’ve only purchased something from Woot once or times before, the woot-offs are really cool because you never know what they might put up there. I decided it’d be a fun side project to right a little script to post the latest woot to Campfire and the script was trivially simple to write. Tinder makes it really easy to interact with Campfire so I just had to figure out a way to get the current Woot. I used woot’s RSS feed to give me this data. It was pretty easy: fetch the feed, parse it, loop through the entries and find the first item in the ‘woot’ category, a little more parsing and voila. I had it.]]></summary></entry></feed>