gotcha 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/gotcha.rb +34 -0
- data/lib/gotcha/base.rb +23 -0
- data/lib/gotcha/controller_helpers.rb +36 -0
- data/lib/gotcha/form_helpers.rb +22 -0
- data/lib/gotcha/validation_error.rb +2 -0
- data/lib/gotcha/version.rb +5 -0
- data/spec/spec_helper.rb +4 -0
- metadata +83 -0
data/lib/gotcha.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Gotcha
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/gotcha/base'
|
4
|
+
autoload :FormHelpers, File.dirname(__FILE__) + '/gotcha/form_helpers'
|
5
|
+
autoload :ControllerHelpers, File.dirname(__FILE__) + '/gotcha/controller_helpers'
|
6
|
+
|
7
|
+
# Remove all gotcha types
|
8
|
+
def self.unregister_all_types
|
9
|
+
@gotcha_types = []
|
10
|
+
end
|
11
|
+
|
12
|
+
# Register a Gotcha type
|
13
|
+
def self.register_type(type)
|
14
|
+
@gotcha_types ||= []
|
15
|
+
@gotcha_types << type
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get a random Gotcha from the registered types
|
19
|
+
def self.random
|
20
|
+
if !@gotcha_types.nil? && type = @gotcha_types.sample
|
21
|
+
type.new
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.gotcha_types
|
26
|
+
@gotcha_types ||= []
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
ActionView::Helpers::FormTagHelper.send(:include, Gotcha::FormHelpers)
|
32
|
+
ActionController::Base.send(:include, Gotcha::ControllerHelpers)
|
33
|
+
|
34
|
+
Dir.glob(File.dirname(__FILE__) + '/../gotchas/*_gotcha.rb').each { |f| require f }
|
data/lib/gotcha/base.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Gotcha
|
2
|
+
|
3
|
+
class Base
|
4
|
+
|
5
|
+
attr_reader :question, :answer
|
6
|
+
|
7
|
+
# Determine whether or not an answer is correct
|
8
|
+
def correct?(str)
|
9
|
+
str = str.is_a?(String) ? str : str.to_s
|
10
|
+
str == (@answer.is_a?(String) ? @answer : @answer.to_s) # don't change @answer type
|
11
|
+
end
|
12
|
+
|
13
|
+
# A default implementation of down_transform - adds the ability to make transforms fuzzy
|
14
|
+
def self.down_transform(text)
|
15
|
+
text = text.is_a?(String) ? text.dup : text.to_s
|
16
|
+
text.downcase!
|
17
|
+
text.gsub! /\s+/, ' '
|
18
|
+
text
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Gotcha
|
2
|
+
|
3
|
+
module ControllerHelpers
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:helper_method, :gotcha_valid?)
|
7
|
+
end
|
8
|
+
|
9
|
+
# return a true / false as to whether or not *all* of the gotchas on the page validated
|
10
|
+
def gotcha_valid?
|
11
|
+
@_gotcha_validated ||= determine_gotcha_validity
|
12
|
+
end
|
13
|
+
|
14
|
+
# Validate the gotcha, throw an exception if the gotcha does not validate (any on the page)
|
15
|
+
def validate_gotcha!
|
16
|
+
raise Gotcha::ValidationError.new unless validate_gotcha
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# Go through each response, using the down_transform
|
22
|
+
# of the original class (as long as it is a subclass of Gotcha::Base)
|
23
|
+
# and compare the hash to the hash of the value
|
24
|
+
def determine_gotcha_validity
|
25
|
+
return false unless params[:gotcha_response].kind_of?(Enumerable)
|
26
|
+
params[:gotcha_response].all? do |ident, value|
|
27
|
+
type, hash = ident.split '-'
|
28
|
+
return false unless Object.const_defined?(type)
|
29
|
+
return false unless (klass = Object.const_get(type)) < Gotcha::Base
|
30
|
+
Digest::MD5.hexdigest(klass.down_transform(value)) == hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Gotcha
|
2
|
+
|
3
|
+
module FormHelpers
|
4
|
+
|
5
|
+
# Propose a gotcha to the user - question and answer hash
|
6
|
+
def gotcha
|
7
|
+
if gotcha = Gotcha.random
|
8
|
+
field = "gotcha_response[#{gotcha.class.name.to_s}-#{Digest::MD5.hexdigest(gotcha.class.down_transform(gotcha.answer))}]"
|
9
|
+
(label_tag field, gotcha.question) + "\n" + (text_field_tag field)
|
10
|
+
else
|
11
|
+
raise "No Gotchas Installed"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Return the gotcha error if its needed
|
16
|
+
def gotcha_error
|
17
|
+
t(:gotcha_validation_failed, :default => 'Failed to validate the Gotcha.') if @_gotcha_validated === false
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gotcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Crepezzi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-08 00:00:00 -05:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
description: A smart captcha library
|
39
|
+
email: john.crepezzi@patch.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- lib/gotcha/base.rb
|
48
|
+
- lib/gotcha/controller_helpers.rb
|
49
|
+
- lib/gotcha/form_helpers.rb
|
50
|
+
- lib/gotcha/validation_error.rb
|
51
|
+
- lib/gotcha/version.rb
|
52
|
+
- lib/gotcha.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://seejohnrun.github.com/gotcha/
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: gotcha
|
78
|
+
rubygems_version: 1.5.0
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: A captcha library for auto-generating questions
|
82
|
+
test_files:
|
83
|
+
- spec/spec_helper.rb
|