Permalink
Browse files

Merge pull request #24 from matthutchinson/appraisal

Use appraisal to test multiple Rails versions
  • Loading branch information...
matthutchinson committed Sep 8, 2016
2 parents 0931c08 + e2d8f78 commit 8a494a9fbae5a13412182c1f41eeb58b14425cbe
Showing with 127 additions and 84 deletions.
  1. +2 βˆ’0 .gitignore
  2. +9 βˆ’2 .travis.yml
  3. +11 βˆ’0 Appraisals
  4. +61 βˆ’62 README.md
  5. +4 βˆ’2 Rakefile
  6. +2 βˆ’1 acts_as_textcaptcha.gemspec
  7. +7 βˆ’0 gemfiles/rails_3.gemfile
  8. +7 βˆ’0 gemfiles/rails_4.gemfile
  9. +7 βˆ’0 gemfiles/rails_5.gemfile
  10. +0 βˆ’2 init.rb
  11. +3 βˆ’6 lib/acts_as_textcaptcha/textcaptcha.rb
  12. +10 βˆ’5 test/test_helper.rb
  13. +4 βˆ’4 test/textcaptcha_helper_test.rb
View
@@ -1,4 +1,5 @@
/.bundle/
/gemfiles/vendor/bundle
/.yardoc
/Gemfile.lock
/_yardoc/
@@ -7,3 +8,4 @@
/pkg/
/spec/reports/
/tmp/
*.gemfile.lock
View
@@ -1,10 +1,17 @@
sudo: false
language: ruby
before_install:
- mkdir -p ./tmp/test_db
gemfile:
- gemfiles/rails_3.gemfile
- gemfiles/rails_4.gemfile
- gemfiles/rails_5.gemfile
rvm:
- 2.1.10
- 2.2.5
- 2.3.1
matrix:
exclude:
- rvm: 2.1.10
gemfile: gemfiles/rails_5.gemfile
branches:
except:
- gh-pages
View
@@ -0,0 +1,11 @@
appraise "rails-3" do
gem "rails", "3.2.22.4"
end
appraise "rails-4" do
gem "rails", "4.2.7.1"
end
appraise "rails-5" do
gem "rails", "5.0.0.1"
end
View
123 README.md
@@ -8,26 +8,27 @@
ActsAsTextcaptcha provides spam protection for your Rails models using logic
questions from the excellent [TextCaptcha](http://textcaptcha.com/) web service
(by [Rob Tuley](http://openknot.com/me/) of [Openknot](http://openknot.com/)).
It is also possible to configure your own text captcha questions (instead, or as
a fallback in the event of any API issues).
(by [Rob Tuley](https://twitter.com/robtuley). It is also possible to configure your
own captcha questions (instead, or as a fallback in the event of any API
issues).
This gem is actively maintained, has good test coverage and is compatible with
Rails >= 3.0.0 (including Rails 4 & 5). If you have issues please report them
Rails >= 3 (including Rails 4 & 5) and Ruby >= 2.1. If you have issues
please report them
[here](https://github.com/matthutchinson/acts_as_textcaptcha/issues/new).
Logic questions from the web service are aimed at a child's age of 7, so they
can be solved easily by even the most cognitively impaired users. As they
involve human logic, questions cannot be solved by a robot. There are both
advantages and disadvantages for using logic questions over image based
advantages and disadvantages in using logic questions over image based
captchas, find out more at [TextCaptcha](http://textcaptcha.com/).
## Demo
Try a [working demo here](https://acts-as-textcaptcha-demo.herokuapp.com)!
**Or** click below to deploy your own example Rails app (configured with
acts_as_textcaptcha) to Heroku. See
**Or** click below to deploy your own example Rails app to Heroku (already
configured with acts_as_textcaptcha). See
[here](https://github.com/matthutchinson/acts_as_textcaptcha_demo) for more
details.
@@ -39,28 +40,28 @@ First add the following to your Gemfile, then `bundle install`;
gem 'acts_as_textcaptcha'
Next add the following code to models you would like to protect;
Add the following code to models you would like to protect;
class Comment < ApplicationRecord
# (this is the simplest way to configure the gem)
acts_as_textcaptcha :api_key => 'TEXTCAPTCHA_API_IDENTITY'
end
`TEXTCAPTCHA_API_IDENTITY` should be some reference to yourself (e.g. an email
address, domain or similar where if there are problems with your usage you can
be contacted).
Your `TEXTCAPTCHA_API_IDENTITY` should be some reference to yourself (e.g. an
email address, domain or similar where if there are problems with your usage you
can be contacted).
Next in your controller's *new* action you'll want to generate and assign the
logic question for the new record, like so;
Next, in your controller's *new* action generate and assign the logic question
for the record, like so;
def new
@comment = Comment.new
@comment.textcaptcha
end
Finally, in your form view add the textcaptcha question and answer fields using
the textcaptcha_fields helper. Feel free to arrange the HTML within this block
as you like;
Finally, in the view add the textcaptcha question and answer fields to your form
using the `textcaptcha_fields` helper. Feel free to arrange the HTML within this
block as you like;
<%= textcaptcha_fields(f) do %>
<div class="field">
@@ -69,14 +70,14 @@ as you like;
</div>
<% end %>
*NOTE:* If you'd rather NOT use this helper and would prefer to write your own
form view code, see the html produced from the textcaptcha_fields method in the
[source
code](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/lib/acts_as_textcaptcha/textcaptcha_helper.rb).
*NOTE:* If you'd rather NOT use this helper and prefer to write your own form
elements, take a look at the HTML produced from this helper method
[here](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/lib/acts_as_textcaptcha/textcaptcha_helper.rb).
*NOTE:* The defaults for [cache
configuration](http://guides.rubyonrails.org/caching_with_rails.html#cache-stores)
changed with Rails 5. This gem requires a working Rails.cache store to exist.
changed with Rails 5 and this gem **requires** a working Rails.cache store to
exist.
*NOTE:* These installation steps changed with v4.0.0 of this gem. If you are
having problems please refer to the 3.0 [upgrade
@@ -86,11 +87,11 @@ guide](https://github.com/matthutchinson/acts_as_textcaptcha/wiki/Upgrading-from
You can toggle textcaptcha on/off for your models by overriding the
`perform_textcaptcha?` method. If it returns false, no questions will be fetched
from the web service and textcaptcha validation is disabled.
from the web service and captcha validation is disabled.
This is useful for writing your own custom logic for toggling spam protection
on/off e.g. for logged in users. By default the `perform_textcaptcha?` method
[checks if the form object is a new (unsaved)
[checks if the object is a new (unsaved)
record](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/lib/acts_as_textcaptcha/textcaptcha.rb#L54).
So out of the box, spam protection is only enabled for creating new records (not
@@ -109,8 +110,8 @@ updating). Here is a typical example showing how to overwrite the
You can configure captchas with the following options;
* *api_key* (_required_) - some reference to yourself (to identity you to the textcaptcha.com API)
* *questions* (_optional_) - array of question and answer hashes (see below) A random question from this array will be asked if the web service fails OR if no `api_key` has been set. Multiple answers to the same question are comma separated (e.g. 2,two). So do not use commas in your answers!
* *api_key* (_required_) - reference to yourself (e.g. your email - to identify calls to the textcaptcha.com API)
* *questions* (_optional_) - array of question and answer hashes (see below) A random question from this array will be asked if the web service fails OR if no `api_key` has been set. Multiple answers to the same question are comma separated (e.g. 2,two). Don't use commas in your answers!
* *cache_expiry_minutes* (_optional_) - minutes for answers to persist in the cache (default 10 minutes), see [below for details](https://github.com/matthutchinson/acts_as_textcaptcha#what-does-the-code-do)
* *http_read_timeout* (_optional_) - Net::HTTP option, seconds to wait for one block to be read from the remote API
* *http_open_timeout* (_optional_) - Net::HTTP option, seconds to wait for the connection to open to the remote API
@@ -129,19 +130,20 @@ For example;
#### YAML config
The gem can be configured for models individually (as shown above) or with a
config/textcaptcha.yml file. The config file must have an `api_key` defined and
optional array of questions. Options definied inline in model classes take
preference over the configuration in textcaptcha.yml. The gem comes with a
handy rake task to copy over a
config/textcaptcha.yml file. The config file must have an `api_key` defined
and/or an array of questions and answers. Any options defined inline in model
classes take preference over the global configuration in textcaptcha.yml.
The gem comes with a handy rake task to copy over a
[textcaptcha.yml](http://github.com/matthutchinson/acts_as_textcaptcha/raw/master/config/textcaptcha.yml)
template to your config directory;
rake textcaptcha:config
#### Configuring _without_ the TextCaptcha web service
To use only your own logic questions, simply ommit the `api_key` from the
configuration and define at least 1 logic question and answer (see above).
To use only your own logic questions, simply omit the `api_key` from the
configuration and define at least one logic question and answer (see above).
## Translations
@@ -163,29 +165,26 @@ logic questions in English.
comment:
textcaptcha_answer: "TextCaptcha answer"
## Without Rails or ActiveRecord
## Without ActiveRecord
Although this gem has been built with Rails in mind, is entirely possible to use
it without ActiveRecord, or Rails. As an example, take a look at the
It is possible to use this gem without ActiveRecord. As an example, take a look at the
[Contact](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/test/test_models.rb#L44)
model used in the test suite
[here](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/test/test_models.rb#L44).
Please note that the built-in
[TextcaptchaCache](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/lib/acts_as_textcaptcha/textcaptcha_cache.rb)
class directly wraps the
[Rails.cache](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html).
An alternative TextcaptchaCache implementation will be necessary if
`Rails.cache` is not available.
## Testing and docs
In development you can run the tests and rdoc tasks like so;
* `rake test` (all tests)
* `rake test:coverage` (all tests with code coverage)
* `rake test:coverage` (all tests with code coverage reporting)
* `appraisal rake test` (all tests with all gemfile variations)
* `appraisal rails-3 rake test` (all tests using a specific gemfile)
* `rake rdoc` (generate docs)
This gem uses [appraisal](https://github.com/thoughtbot/appraisal) to run the
test suite with multiple versions of Rails.
## What does the code do?
The gem contains two parts, a module for your ActiveRecord models, and a single
@@ -195,11 +194,11 @@ and
[TextcaptchaCache](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/lib/acts_as_textcaptcha/textcaptcha_cache.rb).
A call to `@model.textcaptcha` in your controller will query the TextCaptcha web
service. A GET request is made with Net::HTTP and parsed using the default
Rails `ActiveSupport::XMLMini` backend. A textcaptcha_question and a random cache
key is assigned to the record. An array of possible answers is stored in the
TextcaptchaCache with this random key. The cached answers have (by default) a
10 minute TTL in your cache. If your forms take more than 10 minutes to be
service. A GET request is made with Net::HTTP and parsed using the default Rails
`ActiveSupport::XMLMini` backend. A textcaptcha_question and a random cache key
are assigned to the record. An array of possible answers is also stored in the
TextcaptchaCache with this random key. The cached answers have (by default) a 10
minute TTL in your cache. If your forms take more than 10 minutes to be
completed you can adjust this value setting the `cache_expiry_minutes` option.
Internally TextcaptchaCache wraps
[Rails.cache](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html)
@@ -208,7 +207,7 @@ and all cache keys are name spaced.
On saving, `validate_textcaptcha` is called on @model.validate checking that the
`@model.textcaptcha_answer` matches one of the possible answers (retrieved from
the cache). By default, this validation is _only_ carried out on new records,
i.e. never on edit, only on create. All attempted answers are case-insensitive
i.e. never on update, only on create. All attempted answers are case-insensitive
and have trailing/leading white-space removed.
Regardless of a correct, or incorrect answer the possible answers are cleared
@@ -232,23 +231,23 @@ requests and bug reports are welcome.
What do you need?
* [Rails](http://github.com/rails/rails) >= 3.0.0 (including Rails 4 & 5)
* [Rails](http://github.com/rails/rails) >= 3 (including Rails 4 & 5)
* [Rails.cache](http://guides.rubyonrails.org/caching_with_rails.html#cache-stores) - a basic cache configuration is necessary
* [Ruby](http://ruby-lang.org/) >= 2.0.0 (tested with stable versions of 2.2, 2.3) - 1.8/1.9 support ended at version 4.1.1
* [MiniTest](https://rubygems.org/gems/minitest) (_optional_ if you want to run the tests)
* [SimpleCov](https://rubygems.org/gems/simplecov) (_optional_ if you want to run the tests with code coverage reporting)
* [Ruby](http://ruby-lang.org/) >= 2.1
*Note*: The built-in
*NOTE:* The built-in
[TextcaptchaCache](https://github.com/matthutchinson/acts_as_textcaptcha/blob/master/lib/acts_as_textcaptcha/textcaptcha_cache.rb)
class directly wraps the
[Rails.cache](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html)
object.
[Rails.cache](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html).
An alternative TextcaptchaCache implementation will be necessary if
`Rails.cache` is not available.
## Rails 2 Support
Support for Rails 2 was dropped with the release of `v4.1.0`. If you would like to
continue to use this gem with an older version of Rails (>= 2.3.8), please lock
the version to `4.0.0`. Like so;
Support for Rails 2 was dropped with the release of `v4.1.0`. If you would like
to continue to use this gem with an older version of Rails (>= 2.3.8), please
lock your Gemfile with version `4.0.0`. Like so;
# in your Gemfile
gem 'acts_as_textcaptcha', '=4.0.0'
@@ -258,7 +257,7 @@ the version to `4.0.0`. Like so;
Check out the
[README](https://github.com/matthutchinson/acts_as_textcaptcha/tree/v4.0.0) for
this release for more information.
this release for further instructions.
## Links
@@ -276,14 +275,14 @@ this release for more information.
## Who's who?
* [ActsAsTextcaptcha](http://github.com/matthutchinson/acts_as_textcaptcha) and [little robot drawing](http://www.flickr.com/photos/hiddenloop/4541195635/) by [Matthew Hutchinson](http://matthewhutchinson.net)
* [TextCaptcha](http://textcaptcha.com) API and service by [Rob Tuley](http://openknot.com/me/) at [Openknot](http://openknot.com)
* [TextCaptcha](http://textcaptcha.com) API and service by [Rob Tuley](https://twitter.com/robtuley)
## Usage
This gem is used in a number of production websites and apps. It was originally
extracted from code developed for [Bugle](http://bugleblogs.com). If you're
happily using acts_as_textcaptcha in production, let me know and I'll add you to
this list!
happily using acts_as_textcaptcha in production, please let me know and I'll add
you to this list!
* [matthewhutchinson.net](http://matthewhutchinson.net)
* [pmFAQtory.com](http://pmfaqtory.com)
View
@@ -1,7 +1,9 @@
gem 'rdoc'
require 'rubygems'
require 'bundler/gem_tasks'
require 'rake/testtask'
gem 'rdoc'
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
# always test against latest rails version
s.add_development_dependency('rails', '~> 5.0.0')
s.add_development_dependency('rails', '~> 5.0.0.1')
s.add_development_dependency('mime-types')
s.add_development_dependency('bundler')
@@ -41,4 +41,5 @@ Gem::Specification.new do |s|
s.add_development_dependency('sqlite3')
s.add_development_dependency('webmock')
s.add_development_dependency('coveralls')
s.add_development_dependency('appraisal')
end
View
@@ -0,0 +1,7 @@
# This file was generated by Appraisal
source "https://rubygems.org"
gem "rails", "3.2.22.4"
gemspec :path => "../"
View
@@ -0,0 +1,7 @@
# This file was generated by Appraisal
source "https://rubygems.org"
gem "rails", "4.2.7.1"
gemspec :path => "../"
View
@@ -0,0 +1,7 @@
# This file was generated by Appraisal
source "https://rubygems.org"
gem "rails", "5.0.0.1"
gemspec :path => "../"
View
@@ -1,2 +0,0 @@
# this file is required to allow the gem to work as a rails plugin
require './vendor/plugins/acts_as_textcaptcha/lib/acts_as_textcaptcha'
@@ -12,12 +12,9 @@ def acts_as_textcaptcha(options = nil)
cattr_accessor :textcaptcha_config
attr_accessor :textcaptcha_question, :textcaptcha_answer, :textcaptcha_key
if respond_to?(:accessible_attributes)
if accessible_attributes.nil? && respond_to?(:attr_protected)
attr_protected :textcaptcha_question
elsif respond_to?(:attr_accessible)
attr_accessible :textcaptcha_answer, :textcaptcha_key
end
# Rails 3, ensure these attrs are accessible
if respond_to?(:accessible_attributes) && respond_to?(:attr_accessible)
attr_accessible :textcaptcha_answer, :textcaptcha_key
end
validate :validate_textcaptcha, :if => :perform_textcaptcha?
Oops, something went wrong.

0 comments on commit 8a494a9

Please sign in to comment.