Home

Awesome

Build Status Codacy Code Climate

visualCaptcha-rubyGem

RubyGem package for visualCaptcha's backend service

Installation with Gem

You need Ruby 1.9.3+ installed.

gem install visual_captcha

Run tests

You need Bundler and Rake installed and then you can run

bundle install && rake

Usage

Initialization

You have to initialize a session for visualCaptcha to inject the data it needs. You'll need this variable to start and verify visualCaptcha as well.

@session = VisualCaptcha::Session.new session, @namespace

Where:

Setting Routes for the front-end

You also need to set routes for /start/:howmany, /image/:index, and /audio/:type. These will usually look like:

get '/start/:how_many' do
    captcha = VisualCaptcha::Captcha.new @session
    captcha.generate params[:how_many]

    json captcha.frontend_data
  end

  get '/audio/?:type?' do
    type = params[:type]
    type = 'mp3' if type != 'ogg'

    captcha = VisualCaptcha::Captcha.new @session

    if (@body = captcha.stream_audio @headers, type)
      body @body
    else
      not_found
    end
  end

  get '/image/:index' do
    captcha = VisualCaptcha::Captcha.new @session

    if (@body = captcha.stream_image @headers, params[:index], params[:retina])
      body @body
    else
      not_found
    end
  end

Validating the image/audio

Here's how it'll usually look:

@session = VisualCaptcha::Session.new session
captcha = VisualCaptcha::Captcha.new @session
frontend_data = captcha.frontend_data()

# If an image field name was submitted, try to validate it
if ( image_answer = params[ frontend_data[ 'imageFieldName' ] ] )
  if captcha.validate_image image_answer
    # Image was valid.
  else
    # Image was submitted, but wrong.
  end
elsif ( audio_answer = params[ frontend_data[ 'audioFieldName' ] ] )
  if captcha.validate_audio audio_answer.downcase
    # Audio answer was valid.
  else
    # Audio was submitted, but wrong.
  end
else
  # Apparently no fields were submitted, so the captcha wasn't filled.
end

VisualCaptcha::Session properties

VisualCaptcha::Session methods

VisualCaptcha::Captcha properties

VisualCaptcha::Captcha methods

You'll find more documentation on the code itself, but here's the simple list for reference.

License

MIT. Check the LICENSE file.