Ruby CAPTCHA Integration
The following tutorial will guide you on how to integrate the TrustCaptcha CAPTCHA solution into your Ruby backend to retrieve and evaluate the CAPTCHA verification result.
Preparation
Section titled “Preparation”You should have already completed the following steps before you start with CAPTCHA validation in Ruby backend.
Read the basic Information: For a basic overview, please read the get started guide. We also recommend that you familiarize yourself with the technical concept of TrustCaptcha.
Existing CAPTCHA: If you don’t have a CAPTCHA yet, sign in or create a new user account. Then create a new CAPTCHA.
A frontend with TrustCaptcha: Integrate the TrustCaptcha widget into your frontend. Go to the CAPTCHA widget guide.
Existing Ruby project: You need a Ruby project in which you want to integrate TrustCaptcha.
Verification token: You need the verification token from your frontend, which you receive every time you successfully solve the CAPTCHA.
How the CAPTCHA Ruby integration works
Section titled “How the CAPTCHA Ruby integration works”Follow the three steps below to retrieve and evaluate the CAPTCHA verification result in your Ruby backend.
You can find the source code of our Ruby CAPTCHA integration on Github.
1. Add dependency
Section titled “1. Add dependency”To use the TrustCaptcha Ruby library, you first need to add the corresponding dependencies to your project.
gem install trustcaptchaYou can find our TrustCaptcha Ruby package on Rubygems.
2. Fetch result
Section titled “2. Fetch result”In the next step, retrieve the CAPTCHA result from our servers.
If the CAPTCHA widget has been successfully solved in the frontend, you will receive a so-called verification token. Send this to your backend. You will also need the secret-key from your CAPTCHA. You can find your secret key in the dashboard of your CAPTCHA.
Now use the CaptchaManager class of our Ruby integration to retrieve the verification result from our servers.
# Retrieving the verification resultbegin verification_result = CaptchaManager.get_verification_result('<your_secret_key>', verification_token)rescue StandardError => e # Fetch verification result failed - handle error puts "Failed to fetch verification result: #{e.class} - #{e.message}" status 500 return { error: 'Captcha verification failed', message: e.message }.to_jsonend3. Evaluate the result
Section titled “3. Evaluate the result”Once you have successfully fetched the verification result, you can plan your next steps based on it. A concrete overview of all the information contained in the verification result and their respective meanings can be found in the result validation overview.
# Act on the verification resultif !verification_result.verification_passed || verification_result.score > 0.5 puts 'Verification failed or bot score > 0.5 – possible automated request.'endExample implementation
Section titled “Example implementation”The following example shows a possible implementation of TrustCaptcha in a Ruby backend.
In this example: When a POST request is sent to /api/example, the CAPTCHA verification token is sent to the Ruby backend in the request body. In the backend, our library is used to retrieve the CAPTCHA verification result from our servers and evaluate it. If the verification fails or the bot score exceeds 0.5, a warning is displayed. In addition, the entire verification result is returned to the client.
Hint: The steps and thresholds shown are examples and should be adapted to your individual requirements in your specific use case.
The complete example including source code can be found on Github.
require 'sinatra'require 'json'require 'sinatra/cross_origin'require 'trustcaptcha/captcha_manager'
set :port, 8080set :bind, '0.0.0.0'
configure do enable :cross_originend
options '*' do response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, PATCH, DELETE, OPTIONS' response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization, X-Requested-With, Origin, Accept' 200end
post '/api/example' do content_type :json response.headers['Access-Control-Allow-Origin'] = '*' verification_token = JSON.parse(request.body.read)['verificationToken']
# Retrieving the verification result begin verification_result = CaptchaManager.get_verification_result('<your_secret_key>', verification_token) rescue StandardError => e # Fetch verification result failed - handle error puts "Failed to fetch verification result: #{e.class} - #{e.message}" status 500 return { error: 'Captcha verification failed', message: e.message }.to_json end
# Act on the verification result if !verification_result.verification_passed || verification_result.score > 0.5 puts 'Verification failed or bot score > 0.5 – possible automated request.' end
verification_result.to_jsonendNext steps
Section titled “Next steps”Once you have integrated the TrustCaptcha widget into your frontend and the CAPTCHA result validation into your backend, you can use TrustCaptcha to its full extent. However, we still recommend the following additional technical and organizational measures:
Security rules: You can find many security settings for your CAPTCHA in the CAPTCHA settings. These include, for example, authorized websites, CAPTCHA bypass for specific IP addresses, bypass keys, IP based blocking, geoblocking, individual difficulty and duration of the CAPTCHA, and much more. Learn more about the security rules.
Privacy & GDPR compliance: Include a passage in your privacy policy that refers to the use of TrustCaptcha. We also recommend that you enter into a data processing agreement with us to stay GDPR-compliant. Learn more about data protection.
Accessibility & UX: Customize TrustCaptcha to your website so that your website is as accessible as possible and offers the best possible user experience. More about accessibility.
Testing: If you use automated testing, make sure that the CAPTCHA does not block it. Learn more about testing.