green_eggs_and_spam 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .DS_Store
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in green_eggs_and_spam.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ Green Eggs and Spam
2
+ ===================
3
+
4
+ A simple way to filter spam in your rails forms. Green eggs and spam presents the user with a simple question: **What color is this image?**
5
+
6
+ You'll supply the images and a key of which one's which. The gem will handle the rest.
7
+
8
+
9
+ ** ----- under development ----- **
10
+
11
+
12
+ Usage
13
+ -----
14
+
15
+ Install the gem just like you would any other:
16
+
17
+ (sudo) gem install green_eggs_and_spam
18
+
19
+ # or with bundler
20
+
21
+ gem 'green_eggs_and_spam', '>= 0.1.0'
22
+
23
+
24
+ Create a handful of color coded images and name them `1.png`, `2.png`, etc. Tell Green Eggs and Spam which ones which with an initializer:
25
+
26
+ # config/initializers/green_eggs_and_spam.rb
27
+
28
+ GreenEggsAndSpam::AntiSpam.key_index = { "1" => "blue", "2" => "green", "3" => "red" }
29
+
30
+
31
+ In your controller:
32
+
33
+ class CommentsController < ApplicationController
34
+
35
+ has_anti_spam
36
+
37
+ def create
38
+ ... # your code
39
+
40
+ # add an error unless the anti spam question was answered correctly
41
+ @comment.errors[:base] << "Spam Detected! Please make sure you've properly answered the anti spam question." unless anti_spam_valid?
42
+
43
+ ... # the rest of your action
44
+ end
45
+
46
+ end
47
+
48
+
49
+ In your form:
50
+
51
+ = form_for @comment, :url => comment_path do |f|
52
+ %p
53
+ = f.label :comment
54
+ = f.text_field :comment
55
+
56
+ // Here's what your interested in:
57
+ %p
58
+ = anti_spam_form, "What color is this piece of bacon?" # the optional argument is your custom question
59
+
60
+ = f.submit 'send'
61
+
62
+
63
+ Customization:
64
+
65
+ So you're using `.gif`'s or you don't want the images stored in `/images/antispam`. Here's some available options for the form helper.
66
+
67
+ # the defaults
68
+ {
69
+ :alt => 'AntiSpam Image',
70
+ :class => 'antispam-image',
71
+ :path => '/images/antispam',
72
+ :extension => 'jpg'
73
+ }
74
+
75
+ # usage
76
+ anti_spam_form, "Your custom color question?", { :extension => 'png', :path => '/images' }
77
+
78
+
79
+
80
+ License
81
+ -------
82
+
83
+ Copyright (c) 2011 Spencer Steffen, released under the New BSD License All rights reserved.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "green_eggs_and_spam/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "green_eggs_and_spam"
7
+ s.version = GreenEggsAndSpam::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Spencer Steffen"]
10
+ s.email = ["spencer@citrusme.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A simple way to filter spam in your rails forms.}
13
+ s.description = %q{Green eggs and spam presents the user with a simple question: What color is this image? You'll supply the images and a key of which one's which, and the gem will help with the rest.}
14
+
15
+ s.rubyforge_project = "green_eggs_and_spam"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency("rails", ">= 3.0.0")
23
+ end
@@ -0,0 +1,7 @@
1
+ require 'green_eggs_and_spam/anti_spam'
2
+ require 'green_eggs_and_spam/action_controller'
3
+ require 'green_eggs_and_spam/form_helper'
4
+
5
+ module GreenEggsAndSpam
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,33 @@
1
+ module GreenEggsAndSpam
2
+
3
+ module ActionController
4
+
5
+ def self.included(base)
6
+ base.extend GreenEggsAndSpam::ActionController::ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def has_anti_spam
11
+ self.send(:include, GreenEggsAndSpam::ActionController::InstanceMethods)
12
+ self.class_eval do
13
+ helper GreenEggsAndSpam::AntiSpamFormHelper
14
+ before_filter :setup_anti_spam, :only => [:index,:new, :create]
15
+ private
16
+ def setup_anti_spam
17
+ @antispam_key = GreenEggsAndSpam::AntiSpam.random_key
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ module InstanceMethods
24
+ def anti_spam_valid?
25
+ GreenEggsAndSpam::AntiSpam.valid?(params)
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ ActionController::Base.send(:include, GreenEggsAndSpam::ActionController) if defined?(ActionController)
@@ -0,0 +1,29 @@
1
+ module GreenEggsAndSpam
2
+ module AntiSpam
3
+
4
+ extend self
5
+
6
+ def key_index
7
+ @key_index ||= { "1" => "blue", "2" => "green", "3" => "red" }
8
+ end
9
+
10
+ def key_index=(value)
11
+ @key_index = value
12
+ end
13
+
14
+ def random_key
15
+ rand(key_index.keys.length) + 1
16
+ end
17
+
18
+ def valid?(params={})
19
+ key = params[:antispam_key].to_s
20
+ val = params[:antispam].to_s
21
+ return false if key.empty? || val.empty?
22
+ answer = key_index[key.to_s]
23
+ regex = Regexp.new(answer, 'i')
24
+ return val.match(regex) != nil
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,22 @@
1
+ module GreenEggsAndSpam
2
+
3
+ module AntiSpamFormHelper
4
+
5
+ def anti_spam_form(question="What color is this image?", options={})
6
+ key = @antispam_key || 0
7
+ options = { :alt => 'AntiSpam Image', :class => 'antispam-image', :path => '/images/antispam', :extension => 'jpg' }.merge(options)
8
+ choices = GreenEggsAndSpam::AntiSpam.key_index.values.to_a
9
+ choices[choices.length - 1] = "or #{choices.last}"
10
+ [
11
+ label_tag(:antispam, question, :class => 'antispam-label'),
12
+ tag(:br),
13
+ text_field_tag(:antispam, '', :class => 'antispam-field text required', :title => choices.join(", ")),
14
+ hidden_field_tag(:antispam_key, key),
15
+ tag(:br, :class => 'clear'),
16
+ image_tag(File.join(options[:path], "#{key}.#{options[:extension]}"), :alt => options[:alt], :class => options[:class])
17
+ ].join("\n").html_safe
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,3 @@
1
+ module GreenEggsAndSpam
2
+ VERSION = "0.1.0"
3
+ end
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: green_eggs_and_spam
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Spencer Steffen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-22 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: "Green eggs and spam presents the user with a simple question: What color is this image? You'll supply the images and a key of which one's which, and the gem will help with the rest."
28
+ email:
29
+ - spencer@citrusme.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - README.md
40
+ - Rakefile
41
+ - green_eggs_and_spam.gemspec
42
+ - lib/green_eggs_and_spam.rb
43
+ - lib/green_eggs_and_spam/action_controller.rb
44
+ - lib/green_eggs_and_spam/anti_spam.rb
45
+ - lib/green_eggs_and_spam/form_helper.rb
46
+ - lib/green_eggs_and_spam/version.rb
47
+ - public/images/antispam/1.jpg
48
+ - public/images/antispam/2.jpg
49
+ - public/images/antispam/3.jpg
50
+ has_rdoc: true
51
+ homepage: ""
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project: green_eggs_and_spam
74
+ rubygems_version: 1.5.0
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A simple way to filter spam in your rails forms.
78
+ test_files: []
79
+