rack-captchator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ (The MIT License)
3
+
4
+ Copyright © 2011 nyash
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ ### Configuration
2
+
3
+ [sudo] gem install rack-captchator
4
+
5
+ ````ruby
6
+ ## Gemfile
7
+ gem 'rack-captchator', :require => 'rack/captchator'
8
+ ````
9
+ Add the middleware in your app file
10
+
11
+ #### Sinatra-like frameworks
12
+
13
+ ````ruby
14
+ ## app.rb
15
+ use Rack::Captchator
16
+ helpers Rack::Captchator::Helpers
17
+ ````
18
+
19
+ #### Example
20
+
21
+ ````haml
22
+ ## form.haml
23
+ %form{:action => '/someroute', :method => 'post'}
24
+ = captcha_image_tag
25
+ = captha_answer_tag
26
+ %input{:type => "submit", :value => "submit"}
27
+
28
+ ## app.rb
29
+ post '/someroute' do
30
+ if captcha_valid?
31
+ # your logic when it passed
32
+ else
33
+ # your logic when it failed
34
+ end
35
+ end
36
+ ````
37
+
38
+ #### Copyright
39
+
40
+ Copyright (c) 2011 nyash. See LICENSE for details.
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../captchator/helpers', __FILE__)
2
+ require 'open-uri'
3
+
4
+ module Rack
5
+
6
+ class Captchator
7
+
8
+ ANSWER_URL = 'http://captchator.com/captcha/check_answer'
9
+ IMAGE_URL = 'http://captchator.com/captcha/image'
10
+ ANSWER_FIELD = 'captchator_answer'
11
+ SESSION_FIELD = 'captchator_session'
12
+
13
+ def initialize(app)
14
+ @app = app
15
+ end
16
+
17
+ def call(env)
18
+ request = Rack::Request.new(env)
19
+ if request.params[SESSION_FIELD]
20
+ result = validate(request.params[SESSION_FIELD],
21
+ request.params[ANSWER_FIELD])
22
+ env['captchator.valid'] = result
23
+ end
24
+ @app.call(env)
25
+ end
26
+
27
+ def validate(session, answer)
28
+ !open("#{ANSWER_URL}/#{session}/#{answer}").read.to_i.zero? rescue false
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,47 @@
1
+ require 'securerandom'
2
+
3
+ module Rack
4
+
5
+ class Captchator
6
+
7
+ module Helpers
8
+
9
+ # A helper method that outputs an image tag containing captcha
10
+ # @param [Hash] options
11
+ # @example You can provide key-value pairs to add additional attributes
12
+ # captcha_image_tag(:class => "foo", :id => "bar", :alt => "baz")
13
+ # # 'alt' attribute defaults to 'captcha'
14
+ def captcha_image_tag(options={})
15
+ options = {:alt => "captcha"}.merge!(options)
16
+ %Q{<input type='hidden' name='#{Rack::Captchator::SESSION_FIELD}' value='#{captcha_session}' />
17
+ <img src='#{Rack::Captchator::IMAGE_URL}/#{captcha_session}' #{attrify(options)}/>}
18
+ end
19
+
20
+ # A helper method that outputs a text field tag for answering the captcha
21
+ # @param [Hash] options
22
+ # @example You can provide key-value pairs to add additional attributes
23
+ # captcha_answer_tag(:class => "foo", :id => "bar")
24
+ def captcha_answer_tag(options={})
25
+ %Q{<input type='text' name='#{Rack::Captchator::ANSWER_FIELD}' #{attrify(options)}/>}
26
+ end
27
+
28
+ # A helper method that checks whether the captcha was answered correctly
29
+ def captcha_valid?
30
+ request.env['captchator.valid']
31
+ end
32
+
33
+ def captcha_session
34
+ @captcha_session ||= SecureRandom.hex
35
+ end
36
+
37
+ private
38
+
39
+ def attrify(options)
40
+ options.map { |kv| kv.map { |e| e.to_s } }.reduce("") { |r, n| r << "#{n[0]}='#{n[1]}' " }
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-captchator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nyash
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-17 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: A very simple rack captcha middleware via captchator.com API
16
+ email:
17
+ - nyashh@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/rack/captchator/helpers.rb
23
+ - lib/rack/captchator.rb
24
+ - LICENSE
25
+ - README.md
26
+ has_rdoc: true
27
+ homepage: http://github.com/nyash/rack_captchator
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: 1.3.6
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.5.2
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Rack Captcha middleware
51
+ test_files: []