title: How to implement a bespoke recaptcha in rails?
body: <p>Though using existing gems to add a recaptcha to every single form is a pretty simple affair, I can't work out how to create recaptcha as described by the diagram below.</p>
  
  <pre><code>      |
   user submits question form  &lt;-------
        |                             | 
        V                             |
  =============================       |
  | Are the attributes valid? | ----&gt; NO 
  =============================
        |
       YES  
        |
        V
  ===============================================================
  | Has user submitted more than 4 forms in the last 5 minutes? | --NO---| 
  ===============================================================        |
       |                                                                 |
      YES                                                                |
       |                                                                 |
    redirect them to recaptcha form                                      |
       |                                                                 |
       V                                                                 V
  ============================                    ************************************
  |  Captcha form incorrect? | -- NO ----------&gt;  *   save the form as a record      *
  ============================                    *     and redirect user to see it  *
       |              ^                           ************************************
      YES             |
       |              NO
       V              |
  ==================================================
  |  Have they been redirected to the captcha form | 
  |    more than 5 times in the last minute?       |
  ==================================================
       |
      YES
       |
       V
  *******************
  * TEMPORARY BLOCK *
  *******************          
  </code></pre>
  
  <p>I just can't do figure out how to do it. Where is the submitted data saved, for example, while the user fills out the captcha form? In the session? In the cache? In a cookie? Any pointers on how this could be achieved?</p>
  
  <p>I am thinking some sort of function in the application controller, triggered by a <code>before_action</code> all hook. Every time this action is called (i.e whenever a user requests a link of my app) the action is time-stamped in an array of time stamps. When this array is filled with say 10 time stamps, we work out the average, and it's under x amount we block them. This would stop crawlers as well as spammers. </p>
  
  <p><code>However, I still want to let google, yahoo! and bing in!</code> And why not the internet way back machine? <strong><em>I don't want to compromise my app.</em></strong> There's lots of little nuances I need to know before I jump into this.</p>
  
  <p>Here's the behavior I want to implement in a nutshell:</p>
  
  <p>*<em>If the user is sending x number of requests in y number of seconds, redirect them to a recaptcha page. If they pass the recaptcha, let them continue as normal. *</em></p>
  
  <p>I'm open to both visual and logic (question based) recaptchas, just want a solution other than adding a recaptcha to every single form...</p>
  
  <p>Also, I don't like using Gems as they feel like blackboxes to me. Much prefer doing it myself. However, I do like versitile gems like state machine and carrierwave, so if there's an open ended-recaptcha gem I'm open to that.</p>