Git Product home page Git Product logo

gotcha's Introduction

Gotcha

Adding captchas to some action should be really easy. It shouldn't require a migration, like Brain Buster or API calls like reCAPTCHA. Gotcha is an easy way to ask (custom) questions of your users in order for them to perform some action (like submitting a form).


Installation

To install Gotcha, just run:

$ gem install gotcha

Or put it in your Gemfile

gem 'gotcha'

Built-in Gotchas

There are a few captchas implemented and installed by default:

  • SumGotcha - Two random numbers, ask the user for the sum of them
  • BackwardGotcha - Ask the user to retype a random string backwards (case-insensitive)

A random Gotcha type will be generated on each call to gotcha


Using Gotcha

In your forms (HAML used for brevity):

= form_for @thing do |f|
  = gotcha_error
  = gotcha
  = f.submit

In your controller

def new
  @thing = Thing.new
end

def create
  @thing = Thing.new(params[:thing])
  if gotcha_valid? && @thing.save
    redirect_to @thing
  else
    render :new
  end
end

Multiple Gotchas in a page?

= form_for @thing do |f|
  = gotcha_error
  - 10.times do
    .gotcha= gotcha
  = f.submit

and in your controller

gotcha_valid?(10)

Writing your own Gotchas

To write your own Gotcha, all you have to do is extend Gotcha::Base and provide a question and an answer:

class MyGotcha < Gotcha::Base
  def initialize
    @question = 'Who made this?'
    @answer = 'john'
  end
end

When writing your own gotchas, you may want the answers to be able to flex a bit, like allowing users to enter things in different cases. In that case, your Gotcha can just override self.down_transform with whatever logic you want. It takes a single argument, text which is the text of the answer. You are expected to put it in a form that it would match regardless of the cases you want. By default, self.down_transform is implemented as:

def self.down_transform(text)
  text = text.is_a?(String) ? text.dup : text.to_s
  text.downcase!
  text.gsub!(/\s+/, ' ')
  text.strip!
  text
end

Meaning by default, space types don't matter - and case is insensitive.


Installing your gotchas

In an initializer:

Gotcha.unregister_all_types # Remove pre-defined
Gotcha.register_type SumGotcha

Determine if the gotcha was valid

You have a few ways to determine whether or not a Gotcha was valid. You can use gotcha_valid? in views and controllers, or use validate_gotcha! (which throws a Gotcha::ValidationException if the Gotcha was not valid) in your controllers.


License

MIT License. See attached

gotcha's People

Contributors

seejohnrun avatar marcometz avatar ptek avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.