Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
  lib
  .gitignore
  LICENSE
  README.md
  Rakefile
  rack-iamnotarobot.gemspec
Size: Mime:
  README.md

I am not a robot.

A Rack middleware that provides CAPTCHA verification via Google's reCAPTCHA API. Works with Google's new reCAPTCHA implementation.

How to Use

Configuration

First, install the library with:

[sudo] gem install rack-iamnotarobot

You have to require 'rack-iamnotarobot' in your gemfile.

## Gemfile
gem 'rack-iamnotarobot', :require => 'rack/iamnotarobot'

Available options for Rack::IAmNotARobot middleware are:

  • :site_key -- your ReCaptcha API site key (required)
  • :secret_key -- your ReCaptcha API secret key (required)

Now configure your app to use the middleware. This might be different across each web framework.

Sinatra

## app.rb
use Rack::IAmNotARobot, :site_key => 'KEY', :secret_key => 'SECRET'
helpers Rack::IAmNotARobot::Helpers

Padrino

## app/app.rb
use Rack::IAmNotARobot, :site_key => 'KEY', :secret_key => 'SECRET'
helpers Rack::IAmNotARobot::Helpers

Rails

## application.rb:
module YourRailsAppName
  class Application < Rails::Application
    ...
    config.gem 'rack-iamnotarobot', :lib => 'rack/recaptcha'
    config.middleware.use Rack::IAmNotARobot, :site_key => 'KEY', :secret_key => 'SECRET'
  end
end

## application_helper.rb or whatever helper you want it in.
module ApplicationHelper
  include Rack::IAmNotARobot::Helpers
end

## application_controller.rb or whatever controller you want it in.
class ApplicationController < ActionController::Base
  ...
  include Rack::IAmNotARobot::Helpers
  ...
end

Helpers

The Rack::IAmNotARobot::Helpers module (for Sinatra, Rails, Padrino) adds the following method to your app:

To test whether or not the verification passed, you can use:

human?

The human? helper can also be overloaded during tests. You can set its response to either true or false by doing the following:

 # Have human? return true
 Rack::IAmNotARobot.test_mode!

 # Or have it return false
 Rack::IAmNotARobot.test_mode! :return => false

For additional options and resources checkout the customization page

Example

In Rails, here's how you would use the library.

In your view:

<form>
  <input name="email">
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  <div class="g-recaptcha" data-sitekey="YOUR_RECAPTCHA_SITE_KEY"></div>
  <button type="submit">Submit</button>
</form>

In your controller:

if human?
  "passed!"
else
  "failed!"
end

Credit

Based on @achiu's rack-recaptcha.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2015 Crush & Lovely, PJ Kelly. See LICENSE for details.