sinatra-recaptcha 0.0.2a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.markdown +39 -0
  2. data/Rakefile +52 -0
  3. data/lib/sinatra/recaptcha.rb +62 -0
  4. metadata +69 -0
@@ -0,0 +1,39 @@
1
+ # Quick, simple, easy way to implement REcaptcha for Sinatra apps
2
+
3
+ ## Installation
4
+ gem sources -a http://gems.github.com
5
+
6
+ sudo gem install djbender-sinatra-recaptcha
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
+ # to use ssl set Sinatra::ReCaptcha.server = 'https://api-secure.recaptcha.net'
18
+ end
19
+
20
+
21
+ get '/' do
22
+ haml :recaptcha
23
+ end
24
+
25
+ post '/' do
26
+ halt(401, "invalid captcha") unless recaptcha_correct?
27
+ "passed!"
28
+ end
29
+
30
+ __END__
31
+
32
+ @@ captcha
33
+
34
+ %h1 Try ReCaptcha
35
+ %form{:method=>"post", :action=>"/post"}
36
+ = recaptcha
37
+ # you can also use recaptcha(:ajax) to use a pure ajax version--
38
+ %input{:type=>'submit', :value => 'Send'}
39
+ </code></pre>
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = 'sinatra-recaptcha'
8
+ GEM_NAME = 'sinatra-recaptcha'
9
+ GEM_VERSION = '0.0.2'
10
+ AUTHORS = ['James Pozdena']
11
+ EMAIL = "jpoz@jpoz.net"
12
+ HOMEPAGE = "http://github.com/jpoz/sinatra-recaptcha"
13
+ SUMMARY = "Simple, easy way to implement recaptcha for Sinatra"
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.name = GEM
17
+ s.version = GEM_VERSION
18
+ s.platform = Gem::Platform::RUBY
19
+ s.has_rdoc = true
20
+ s.summary = SUMMARY
21
+ s.description = s.summary
22
+ s.authors = AUTHORS
23
+ s.email = EMAIL
24
+ s.homepage = HOMEPAGE
25
+ s.require_path = 'lib'
26
+ s.autorequire = GEM
27
+ s.files = %w(README.markdown Rakefile) + Dir.glob("{lib}/**/*")
28
+ end
29
+
30
+ task :default => :spec
31
+
32
+ desc "Run specs"
33
+ Spec::Rake::SpecTask.new do |t|
34
+ t.spec_files = FileList['spec/**/*_spec.rb']
35
+ t.spec_opts = %w(-fs --color)
36
+ end
37
+
38
+ Rake::GemPackageTask.new(spec) do |pkg|
39
+ pkg.gem_spec = spec
40
+ end
41
+
42
+ desc "install the gem locally"
43
+ task :install => [:package] do
44
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
45
+ end
46
+
47
+ desc "create a gemspec file"
48
+ task :make_spec do
49
+ File.open("#{GEM}.gemspec", "w") do |file|
50
+ file.puts spec.to_ruby
51
+ end
52
+ end
@@ -0,0 +1,62 @@
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 recaptcha(type = :iframe)
16
+ raise "Recaptcha type: #{type} is not known, please use (:iframe or :ajax)" unless [:iframe, :ajax].include?(type.to_sym)
17
+ self.send("recaptcha_#{type}")
18
+ end
19
+
20
+ def recaptcha_correct?
21
+ recaptcha = Net::HTTP.post_form URI.parse("#{Sinatra::ReCaptcha.verify}/verify"), {
22
+ :privatekey => Sinatra::ReCaptcha.private_key,
23
+ :remoteip => request.ip,
24
+ :challenge => params[:recaptcha_challenge_field],
25
+ :response => params[:recaptcha_response_field]
26
+ }
27
+ answer, error = recaptcha.body.split.map { |s| s.chomp }
28
+ unless answer == 'true'
29
+ return false
30
+ else
31
+ return true
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+ def recaptcha_iframe
38
+ "<script type='text/javascript'
39
+ src='#{Sinatra::ReCaptcha.server}/challenge?k=#{Sinatra::ReCaptcha.public_key}'>
40
+ </script>
41
+ <noscript>
42
+ <iframe src='#{Sinatra::ReCaptcha.server}/noscript?k=#{Sinatra::ReCaptcha.public_key}'
43
+ height='300' width='500' frameborder='0'></iframe><br>
44
+ <textarea name='recaptcha_challenge_field' rows='3' cols='40'>
45
+ </textarea>
46
+ <input type='hidden' name='recaptcha_response_field'
47
+ value='manual_challenge'>
48
+ </noscript>"
49
+ end
50
+
51
+ def recaptcha_ajax
52
+ "<div id='recaptcha_div'> </div>
53
+ <script type='text/javascript' src='#{Sinatra::ReCaptcha.server}/js/recaptcha_ajax.js'></script>
54
+ <script type='text/javascript' >
55
+ Recaptcha.create('#{Sinatra::ReCaptcha.public_key}', 'recaptcha_div' );
56
+ </script>"
57
+ end
58
+
59
+ end
60
+
61
+ helpers ReCaptcha
62
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-recaptcha
3
+ version: !ruby/object:Gem::Version
4
+ hash: 34
5
+ prerelease: 5
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ - a
11
+ version: 0.0.2a
12
+ platform: ruby
13
+ authors:
14
+ - James Pozdena
15
+ - Derek Bender
16
+ autorequire: sinatra-recaptcha
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-10-02 00:00:00 Z
21
+ dependencies: []
22
+
23
+ description: Simple, easy way to implement recaptcha for Sinatra
24
+ email: ""
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - README.markdown
33
+ - Rakefile
34
+ - lib/sinatra/recaptcha.rb
35
+ homepage: http://github.com/djbender/sinatra-recaptcha
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.6
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Simple, easy way to implement recaptcha for Sinatra
68
+ test_files: []
69
+