jpoz-sinatra-recaptcha 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.markdown +36 -0
  2. data/lib/sinatra/recaptcha.rb +47 -0
  3. metadata +55 -0
@@ -0,0 +1,36 @@
1
+ # Quick, simple, easy way to implement REcaptcha for Sinatra apps
2
+
3
+ ## Installation
4
+
5
+ gem sources -a http://gems.github.com
6
+ sudo gem install jpoz-sinatra-captcha
7
+
8
+ <pre><code>
9
+ require 'rubygems'
10
+ require 'sinatra'
11
+ require 'sinatra/recaptcha'
12
+
13
+ configure do
14
+ # https://admin.recaptcha.net/accounts/signup/
15
+ Sinatra::ReCaptcha.public_key = 'your_public_key'
16
+ Sinatra::ReCaptcha.private_key = 'your_private_key'
17
+ end
18
+
19
+
20
+ get '/' do
21
+ haml :recaptcha
22
+ end
23
+
24
+ post '/' do
25
+ halt(401, "invalid captcha") unless captcha_correct?
26
+ "passed!"
27
+ end
28
+
29
+ __END__
30
+
31
+ @@ captcha
32
+
33
+ %h1 Try ReCaptch
34
+ %form{:method=>"post", :action=>"/post"}
35
+ = recaptcha
36
+ %input{:type=>'submit', :value => 'Send'}
@@ -0,0 +1,47 @@
1
+ require 'net/http'
2
+
3
+ module Sinatra
4
+ module ReCaptcha
5
+ VERSION = "0.0.1"
6
+
7
+ @server = 'http://api.recaptcha.net'
8
+ @verify = 'http://api-verify.recaptcha.net'
9
+
10
+ class << self
11
+ attr_accessor :public_key, :private_key, :server
12
+ attr_reader :verify
13
+ end
14
+
15
+ def captcha_pass?
16
+ session = params[:captcha_session].to_i
17
+ answer = params[:captcha_answer].gsub(/\W/, '')
18
+ open("http://captchator.com/captcha/check_answer/#{session}/#{answer}").read.to_i.nonzero? rescue false
19
+ end
20
+
21
+ def recaptcha
22
+ "<div id='dynamic_recaptcha'>
23
+ <script type='text/javascript' src='#{Sinatra::ReCaptcha.server}/js/recaptcha_ajax.js'></script>
24
+ <script type='text/javascript' >
25
+ Recaptcha.create('#{Sinatra::ReCaptcha.public_key}', document.getElementById('dynamic_recaptcha') );
26
+ </script>
27
+ </div>"
28
+ end
29
+
30
+ def recaptcha_correct?
31
+ recaptcha = Net::HTTP.post_form URI.parse("#{Sinatra::ReCaptcha.verify}/verify"), {
32
+ :privatekey => Sinatra::ReCaptcha.private_key,
33
+ :remoteip => request.ip,
34
+ :challenge => params[:recaptcha_challenge_field],
35
+ :response => params[:recaptcha_response_field]
36
+ }
37
+ answer, error = recaptcha.body.split.map { |s| s.chomp }
38
+ unless answer == 'true'
39
+ return false
40
+ else
41
+ return true
42
+ end
43
+ end
44
+ end
45
+
46
+ helpers ReCaptcha
47
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jpoz-sinatra-recaptcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Blake Mizerany
8
+ - James Pozdena
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-07-09 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Simple, easy way to implement recaptcha for Sinatra
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - README.markdown
27
+ - lib/sinatra/recaptcha.rb
28
+ has_rdoc: false
29
+ homepage:
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Simple, easy way to implement recaptcha for Sinatra
54
+ test_files: []
55
+