Yandex Captcha
Ruby wrapper for Yandex Cleanweb spam detector.
Unfortunatelly, this gem is not capable with MRI 1.8.7 because of MRI 1.8.7 doesn't have URI.encode_www_form method.
Installation
Add this line to your application's Gemfile:
gem 'yandex_captcha', '~> 0.4.3.1'
Or:
gem 'yandex_captcha', github: 'merqlove/yandex-captcha'
And then execute:
$ bundle
Or install it yourself as:
$ gem install yandex_captcha
This gem can be used as Rails Engine or Sinatra Extension.
Tested:
- Rails 3.2+ (Rails 4 also checked)
- Sinatra 1.4+
Usage
Get the api key: http://api.yandex.ru/cleanweb/getkey.xml
Code
# Rails routes
mount YandexCaptcha::Engine, at: '/yandex_captcha/'
# Sinatra
register YandexCaptcha::Sinatra
# Configuration
YandexCaptcha.configure do |config|
config.api_key = "your_key"
end
In Views
View Helper Options
ajax=true/falsenoscript=true/falseOnly for non ajax version.
Code
<%= captcha_tags %>
<%= captcha_tags ajax:true %>
<%= captcha_tags noscript:true %>
In Controllers
if YandexCaptcha::Verify.valid_captcha?(params[:captcha_response_id], params[:captcha_response_field])
# some
end
Other examples
# Methods
YandexCaptcha::Verify.spam?("just phrase")
=> false
YandexCaptcha::Verify.spam?(body_plain: "my text", ip: "80.80.40.3")
=> false
YandexCaptcha::Verify.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
=> { id: "request id", links: [ ['http://spam.com', true] ] }
More complex example
user_input = "download free porn <a>...</a>"
if spam_check = YandexCaptcha::Verify.spam?(user_input, ip: current_user.ip)
captcha = YandexCaptcha::Verify.get_captcha(spam_check[:id])
# now you can show captcha[:url] to user
# but remember to write captcha[:captcha] to session
# to check is captcha enterred by user is valid:
captcha_valid = YandexCaptcha::Verify.valid_captcha?(result[:id], captcha[:captcha], user_captcha)
end
If you use Yandex Captcha in Rails app, we recommend to set up the api key in config/initializers/yandex_captcha.rb
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Special respect to Evrone which gem yandex-cleanweb include most of code in lib/yandex_captcha/verify.rb and some examples on this page.
