recaptchaed 1.0.0

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Dave Paroulek
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,121 @@
1
+ Recaptchaed
2
+ ==========
3
+
4
+ Rails Plugin for adding "recaptcha(recaptcha)":http://www.google.com/recaptcha to a form.
5
+
6
+ Install
7
+ ==========
8
+
9
+ # First, go to "recaptcha(recaptcha)":http://www.google.com/recaptcha and sign up for a api key.
10
+
11
+ # Next, install the recaptchaed gem:
12
+
13
+ @gem install recaptchaed@
14
+
15
+ # Tell Rails to use the gem
16
+
17
+ Open config/environment.rb and enter the following:
18
+
19
+ @config.gem "recaptchaed"@
20
+
21
+ # Run the generator to create config files and to add customizable error messages
22
+
23
+ @script/generate recaptchaed@
24
+
25
+ If it runs successfully, you should see the following:
26
+
27
+ @
28
+ create config/recaptchaed.yml
29
+ create config/initializers/recaptchaed.rb
30
+ added recaptcha i18n error message to config/locales/en.yml
31
+ @
32
+
33
+ # Edit the config/recaptcaed.yml file and add your personal recaptcha public and private api keys
34
+
35
+ Uninstall
36
+ ==========
37
+
38
+ # Run script/destroy recaptchaed
39
+
40
+ # remove any related custom logic from your views and controllers
41
+
42
+ Example Usage
43
+ =============
44
+
45
+ This example shows how to protect a comment model with a recaptcha
46
+
47
+ # Include the recaptcha javascript library in app/views/layouts/comments.html.erb.
48
+
49
+ @<%=javascript_include_tag 'http://www.google.com/recaptcha/api/js/recaptcha_ajax.js' %>@
50
+
51
+ # Add the recaptcha markup to the app/views/comments/new.html.erb.
52
+
53
+ @<h1>New comment</h1>
54
+
55
+ <% form_for(@comment) do |f| %>
56
+ <%= f.error_messages %>
57
+
58
+ <p>
59
+ <%= f.label :title %><br />
60
+ <%= f.text_field :title %>
61
+ </p>
62
+ <p>
63
+ <%= f.label :body %><br />
64
+ <%= f.text_area :body %>
65
+ </p>
66
+ <%= recaptcha_tag (RECAPTCHAED["RECAPTCHA_PUBLIC_KEY"], "recaptcha_div", "blackglass") %>
67
+ <p>
68
+ <%= f.submit 'Create' %>
69
+ </p>
70
+ <% end %>
71
+
72
+ <%= link_to 'Back', comments_path %>
73
+ @
74
+
75
+ The "recaptcha site(recaptcha)":http://code.google.com/apis/recaptcha/docs/customization.html has several options for different themes/styles. For example, you can change the theme of the recaptcha to 'white' by passing a different theme string.
76
+
77
+ @ <%= recaptcha_tag (RECAPTCHAED["RECAPTCHA_PUBLIC_KEY"], "recaptcha_div", "white") %> @
78
+
79
+ # Add logic to create action inside the comments controller to validate the captcha.
80
+
81
+ First, include the library in the comments controller:
82
+
83
+ @
84
+ class CommentsController < ApplicationController
85
+ include Recaptchaed
86
+ ...rest of controller...
87
+ end
88
+ @
89
+
90
+ Then, protect the create method using the 'validate_captcha(..)' method
91
+
92
+ @
93
+ # POST /comments
94
+ # POST /comments.xml
95
+ def create
96
+ @comment = Comment.new(params[:comment])
97
+
98
+ @recaptcha = validate_captcha(RECAPTCHAED["RECAPTCHA_PRIVATE_KEY"], request.remote_ip, params['recaptcha_challenge_field'], params['recaptcha_response_field'])
99
+
100
+ respond_to do |format|
101
+ if @recaptcha && @recaptcha['success'] && @comment.save
102
+ flash[:notice] = 'Comment was successfully created.'
103
+ format.html { redirect_to(@comment) }
104
+ format.xml { render :xml => @comment, :status => :created, :location => @comment }
105
+ else
106
+ if(!@recaptcha['success'])
107
+ @comment.errors.add('recaptcha_response_field', :"recaptcha.invalid")
108
+ end
109
+ format.html { render :action => "new" }
110
+ format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
111
+ end
112
+ end
113
+ end
114
+ @
115
+
116
+ When a user fails to enter the correct captcha, the error message that is displayed is defined inside 'config/locales/en.yml'. Feel free to update that to whatever you like. And, it can also be internationalized, if needed.
117
+
118
+ Enjoy!
119
+
120
+ http://upgradingdave.com
121
+ Copyright (c) 2010 Dave Paroulek, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+ require 'rake/gempackagetask'
4
+
5
+ desc 'Default: gem'
6
+ task :default => :gem
7
+
8
+ PKG_FILES = FileList[
9
+ '[a-zA-Z]*',
10
+ 'generators/**/*',
11
+ 'lib/**/*',
12
+ 'rails/**/*',
13
+ 'tasks/**/*',
14
+ 'spec/**/*'
15
+ ]
16
+
17
+ spec = Gem::Specification.new do |s|
18
+ s.name = "recaptchaed"
19
+ s.version = "1.0.0"
20
+ s.author = "Dave Paroulek"
21
+ s.email = "dave@daveparoulek.com"
22
+ s.homepage = "http://upgradingdave.com"
23
+ s.platform = Gem::Platform::RUBY
24
+ s.summary = "Rails Plugin that makes it super easy to add recaptcha to any model form"
25
+ s.files = PKG_FILES.to_a
26
+ s.require_path = "lib"
27
+ s.has_rdoc = false
28
+ s.extra_rdoc_files = ["README"]
29
+ end
30
+
31
+ desc 'Turn this plugin into a gem.'
32
+ Rake::GemPackageTask.new(spec) do |pkg|
33
+ pkg.gem_spec = spec
34
+ end
35
+
36
+ desc 'Generate documentation for the recaptchaed plugin.'
37
+ Rake::RDocTask.new(:rdoc) do |rdoc|
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = 'Recaptchaed'
40
+ rdoc.options << '--line-numbers' << '--inline-source'
41
+ rdoc.rdoc_files.include('README')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
@@ -0,0 +1,14 @@
1
+ Description:
2
+ Configures the recaptchaed gem rails plugin.
3
+
4
+ Usage:
5
+ ./script/generate recaptchaed
6
+
7
+ This will create:
8
+ config/recaptchaed.yml
9
+ config/initializers/recaptchaed.rb
10
+
11
+ And this also adds an i18n error message to the config/locales/en.yml file
12
+
13
+
14
+ ./script/destroy recaptchaed will undo everything that generate created and/or added
@@ -0,0 +1,69 @@
1
+ require 'rails_generator'
2
+ require 'rails_generator/commands'
3
+
4
+ module Recaptchaed #:nodoc:
5
+ module Generator #:nodoc:
6
+ module Commands #:nodoc:
7
+ # This is the text we want to apped to the end of config/locales/en.yml:
8
+ CONTENT = <<END
9
+
10
+ en:
11
+ activerecord:
12
+ errors:
13
+ full_messages:
14
+ recaptcha:
15
+ invalid: "{{message}}"
16
+ models:
17
+ comment:
18
+ attributes:
19
+ recaptcha_response_field:
20
+ recaptcha:
21
+ invalid: "The characters you entered didn't quite match up to the image. Please make sure to match the letters in the image exactly."
22
+ END
23
+
24
+ I18N_PATH = 'config/locales/en.yml'
25
+
26
+ module Create
27
+ def recaptcha_i18n
28
+ logger.added "recaptcha i18n error message to #{I18N_PATH}"
29
+ look_for = '$'
30
+ unless options[:pretend]
31
+ gsub_file(I18N_PATH, /\z/mi){|match| "#{match}\n #{CONTENT}\n"}
32
+ end
33
+ end
34
+ end
35
+
36
+ module Destroy
37
+ def recaptcha_i18n
38
+ logger.removed "recaptcha i18n error message from #{I18N_PATH}"
39
+ gsub_file I18N_PATH, /#{Regexp.escape(CONTENT)}/mi, ''
40
+ end
41
+ end
42
+
43
+ module List
44
+ def recaptcha_i18n
45
+ end
46
+ end
47
+
48
+ module Update
49
+ def recaptcha_i18n
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ Rails::Generator::Commands::Create.send :include, Recaptchaed::Generator::Commands::Create
57
+ Rails::Generator::Commands::Destroy.send :include, Recaptchaed::Generator::Commands::Destroy
58
+ Rails::Generator::Commands::List.send :include, Recaptchaed::Generator::Commands::List
59
+ Rails::Generator::Commands::Update.send :include, Recaptchaed::Generator::Commands::Update
60
+
61
+ class RecaptchaedGenerator < Rails::Generator::Base
62
+ def manifest
63
+ record do |m|
64
+ m.file "recaptchaed.yml", "config/recaptchaed.yml"
65
+ m.file "recaptchaed.rb", "config/initializers/recaptchaed.rb"
66
+ m.recaptcha_i18n
67
+ end
68
+ end
69
+ end
@@ -0,0 +1 @@
1
+ RECAPTCHAED = YAML.load_file("#{RAILS_ROOT}/config/recaptchaed.yml")[RAILS_ENV]
@@ -0,0 +1,14 @@
1
+ development:
2
+ RECAPTCHA_ENABLED: true
3
+ RECAPTCHA_PUBLIC_KEY: your recaptcha private key here
4
+ RECAPTCHA_PRIVATE_KEY: your recaptcha private key here
5
+
6
+ test:
7
+ RECAPTCHA_ENABLED: false
8
+ RECAPTCHA_PUBLIC_KEY: your recaptcha private key here
9
+ RECAPTCHA_PRIVATE_KEY: your recaptcha private key here
10
+
11
+ production:
12
+ RECAPTCHA_ENABLED: true
13
+ RECAPTCHA_PUBLIC_KEY: your recaptcha private key here
14
+ RECAPTCHA_PRIVATE_KEY: your recaptcha private key here
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1 @@
1
+ require 'recaptchaed/recaptchaed'
File without changes
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'action_view'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module Recaptchaed
7
+ def validate_captcha(private_key, remote_ip, recaptcha_challenge_field, recaptcha_response_field)
8
+ site = "http://www.google.com/recaptcha/api/verify"
9
+ url = URI.parse(site)
10
+ req = Net::HTTP::Post.new(url.path)
11
+ req.set_form_data({'privatekey' => private_key,
12
+ 'remoteip' => remote_ip,
13
+ 'challenge' => recaptcha_challenge_field,
14
+ 'response' => recaptcha_response_field})
15
+ h = Net::HTTP.new(url.host, url.port)
16
+ # h.set_debug_output $stderr
17
+ res = h.start { |http| http.request(req) }
18
+
19
+ case res
20
+ when Net::HTTPSuccess, Net::HTTPRedirection
21
+ success, message = res.body.split.map { |s| s.chomp }
22
+ success = success =~ /^true$/i ? true : false
23
+ message = message =~ /^incorrect-captcha-sol$/ ? "Oops, the text you entered doesn't match the image exactly, please try again" : message
24
+ result = { 'success' => success, 'message' => message }
25
+ else
26
+ result = { 'success' => false, 'message' => res.error! }
27
+ end
28
+ end
29
+
30
+ ActionView::Helpers::FormTagHelper.class_eval do
31
+ def recaptcha_tag (public_key, div_id = "recaptcha_div", theme="red")
32
+ @result ||= "<div id=\"#{div_id}\"></div>
33
+ <script type=\"text/javascript\">
34
+ Recaptcha.create('#{public_key}', '#{div_id}', { theme: '#{theme}' })
35
+ </script>"
36
+ end
37
+ end
38
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'recaptchaed'
@@ -0,0 +1,14 @@
1
+ require 'test_helper.rb'
2
+ require 'rubygems'
3
+ require 'action_view'
4
+ include ActionView::Helpers::FormTagHelper
5
+ include ActionView::Helpers::TagHelper
6
+
7
+ describe Recaptchaed, 'form_tag' do
8
+ it "should add a form helper" do
9
+ recaptcha_tag("12345", "test", "blackglass").should == "<div id=\"test\"></div>
10
+ <script type=\"text/javascript\">
11
+ Recaptcha.create('12345', 'test', { theme: 'blackglass' })
12
+ </script>"
13
+ end
14
+ end
@@ -0,0 +1,83 @@
1
+ require 'test_helper.rb'
2
+ # require File.join(File.expand_path(File.dirname(__FILE__)), "../lib", "recaptchaed")
3
+ # include Recaptchaed
4
+ require 'rails_generator'
5
+ require 'rails_generator/scripts/generate'
6
+
7
+ describe Recaptchaed, 'generator' do
8
+
9
+ before(:all) do
10
+ FileUtils.mkdir_p(fake_initializers_dir)
11
+ FileUtils.mkdir_p(fake_locales_dir)
12
+ @original_config_files = config_file_list
13
+ @original_initializers_files = initializers_file_list
14
+
15
+ content = <<-END
16
+ # Sample localization file for English. Add more files in this directory for other locales.
17
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
18
+
19
+ en:
20
+ hello: "Hello world"
21
+ END
22
+ File.open(fake_i18n_path, 'wb') {|f| f.write(content) }
23
+ end
24
+
25
+ after(:all) do
26
+ FileUtils.rm_r(fake_config_dir)
27
+ end
28
+
29
+ it "should generate a config file, an initializer, and add i18n error message" do
30
+ Rails::Generator::Scripts::Generate.new.run(["recaptchaed"], :destination => fake_rails_root)
31
+
32
+ new_file = (config_file_list - @original_config_files).first
33
+ File.basename(new_file).should == "recaptchaed.yml"
34
+
35
+ new_file = (initializers_file_list - @original_initializers_files).first
36
+ File.basename(new_file).should == "recaptchaed.rb"
37
+
38
+ File.read(fake_i18n_path).should match(/recaptcha_response_field/)
39
+ end
40
+
41
+ # it "should remove config file, initializer, and should remove i18n error message" do
42
+ # Rails::Generator::Scripts::Destroy.new.run(["recaptchaed"], :destination => fake_rails_root)
43
+
44
+ # new_file = (config_file_list - @original_config_files)
45
+ # new_file.should be nil
46
+
47
+ # new_file = (initializers_file_list - @original_initializers_files)
48
+ # new_file.should be nil
49
+
50
+ # File.read(fake_i18n_path).should_not match(/recaptcha_response_field/)
51
+ # end
52
+
53
+ private
54
+
55
+ def fake_rails_root
56
+ File.join(File.dirname(__FILE__), 'rails_root')
57
+ end
58
+
59
+ def fake_initializers_dir
60
+ @fake_initializers_dir ||= "#{fake_rails_root}/config/initializers"
61
+ end
62
+
63
+ def fake_config_dir
64
+ @fake_config_dir ||= "#{fake_rails_root}/config"
65
+ end
66
+
67
+ def fake_locales_dir
68
+ @fake_locales_dir ||= "#{fake_rails_root}/config/locales"
69
+ end
70
+
71
+ def fake_i18n_path
72
+ @fake_i18n_path ||= "#{fake_rails_root}/config/locales/en.yml"
73
+ end
74
+
75
+ def config_file_list
76
+ Dir.glob(File.join(fake_config_dir, "*"))
77
+ end
78
+
79
+ def initializers_file_list
80
+ Dir.glob(File.join(fake_initializers_dir, "*"))
81
+ end
82
+
83
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper.rb'
2
+ require 'rubygems'
3
+ require 'action_view'
4
+ include ActionView::Helpers::FormTagHelper
5
+ include ActionView::Helpers::TagHelper
6
+
7
+ describe Recaptchaed, 'form_tag' do
8
+ it "should add a form helper" do
9
+ recaptcha_tag("12345", "test", "blackglass").should == "<div id=\"test\"></div>
10
+ <script type=\"text/javascript\">
11
+ Recaptcha.create('12345', 'test', { theme: 'blackglass' })
12
+ </script>"
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper.rb'
2
+ include Recaptchaed
3
+
4
+ ##
5
+ # its kind of a pain to test this because, by nature, a captcha
6
+ # shouldn't be able to be programmatically solved. So, to test, go to
7
+ # a page with a form protected by recaptcha. View the source and update the
8
+ # corresponding values below from the recaptcha form that is generated
9
+
10
+ REMOTE_IP="changeme"
11
+ CHALLENGE_FIELD="changeme"
12
+ RESPONSE_FIELD="changeme"
13
+
14
+ describe Recaptchaed, "#validate_captcha" do
15
+ it "should return true when valid captcha is submitted" do
16
+ result = validate_captcha(RECAPTCHAED["RECAPTCHA_PRIVATE_KEY"], REMOTE_IP, CHALLENGE_FIELD, RESPONSE_FIELD)
17
+ puts "Success: #{result['success']}, Message: #{result['message']}"
18
+ result['success'].should be true
19
+ end
20
+ end
21
+
@@ -0,0 +1,5 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
3
+
4
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
5
+ # require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/initializers/recaptchaed.rb'))
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :recaptchaed do
3
+ # # Task goes here
4
+ # end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recaptchaed
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Dave Paroulek
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-05 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: dave@daveparoulek.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ files:
31
+ - install.rb
32
+ - MIT-LICENSE
33
+ - Rakefile
34
+ - README
35
+ - uninstall.rb
36
+ - generators/recaptchaed/recaptchaed_generator.rb
37
+ - generators/recaptchaed/templates/recaptchaed.rb
38
+ - generators/recaptchaed/templates/recaptchaed.yml
39
+ - generators/recaptchaed/USAGE
40
+ - lib/recaptchaed/commands.rb
41
+ - lib/recaptchaed/recaptchaed.rb
42
+ - lib/recaptchaed.rb
43
+ - rails/init.rb
44
+ - tasks/recaptchaed_tasks.rake
45
+ - spec/#recaptcha_form_tag_spec.rb#
46
+ - spec/generator_spec.rb
47
+ - spec/recaptcha_form_tag_spec.rb
48
+ - spec/recaptchaed_spec.rb
49
+ - spec/test_helper.rb
50
+ has_rdoc: true
51
+ homepage: http://upgradingdave.com
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
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.7
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Rails Plugin that makes it super easy to add recaptcha to any model form
84
+ test_files: []
85
+