Captcher
Easy to use classical captcha for Rails apps
Installation
Add this line to your application's Gemfile:
gem 'captcher'
And then execute:
$ bundle
Or install it yourself as:
$ gem install captcher
Mount the engine inside your application by adding this line to your application's routes file at config/routes.rb
:
mount Captcher::Engine => "/captcher"
Usage
Render on page
- Include the concern with helper methods to your ApplicationController:
class ApplicationController < ActionController::Base
include Captcher::CaptchaAware
end
- Use helper methods in your controller:
class MyController < ApplicationController
def index
reload_captcha(session) # Reload the captcha
# render response with success code ...
end
def create
@comment = Comment.new(comment_params)
captcha_check = confirm_captcha?(session, params[:captcha])
if @comment.valid? && captcha_check && @comment.save
# render response with success code ...
else
@comment.errors[:captcha] << "Captcha verification failed" unless captcha_check
# render response with error code ...
end
end
# ... some other code
end
- An example bootstrap-based html/erb code:
<%= simple_form_for(some_form) do |f| %>
<!-- Some html/erb code for all form fields -->
<!-- ... -->
<!-- /Some html/erb code for all form fields -->
<div class="input-group">
<%= text_field_tag :captcha, "",
type: :text,
label: false,
class: "form-control",
placeholder: "Enter the captcha" %>
<div class="input-group-append">
<div class="input-group-text" style="padding: 0">
<%= image_tag(captcher.captcha_path(format: :png), style: "height: 35px;",
id: "captcha-image") %>
</div>
<button class="btn btn-outline-secondary" type="button" id="captcha-reload">
<i class="fa fa-refresh"></i>
</button>
</div>
</div>
<% end %>
- Javascript code to refresh the capture:
function reloadCaptcha() {
$.ajax({
type: 'GET',
url: '/captcher/captcha/reload.png',
success: function() {
var timestamp = (new Date()).getTime();
$('#captcha-image').attr("src", "/captcher/captcha.png?" + timestamp);
},
});
}
$('#captcha-reload').click(function() {
reloadCaptcha();
});
API endpoints
These endpoints are available by default (as soon as you've mounted the Captcher
engine to your routes.rb
file) and can be used for some async requests:
http://your-application.com/captcher/captcha
- Load the captcha imagehttp://your-application.com/captcher/captcha/reload
- Reload the captchahttp://your-application.com/captcher/captcha/confirm?confirmation=code
- Confirm captcha code
Configuration
# config/initialiers/captcher.rb
Captcher.configure do |c|
c.mode = :code_captcha
c.code_captcha do |cc|
cc.fonts Dir[Captcher::Engine.root.join("lib/fonts/**")]
cc.font_size 50
cc.font_color "black"
cc.count 5
cc.background "#999999"
cc.format "png"
end
end
TODO
Implement some other types of captcha
Integrate with Travis to test the gem against different versions of Ruby/ROR
Improve code style
Improve documentation
Autogenerated API documentation
Contributing
Contribution directions go here.
Fonts
The fonts wich are shiped by default with this repo are taken from https://github.com/google/fonts and use the SIL Open Font License, v1.1
There's a list of the origin paths of the fonts:
License
The gem is available as open source under the terms of the MIT License.