textcaptcha 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in textcaptcha.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Drew Tempelmeyer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,62 @@
1
+ = textcaptcha
2
+
3
+ textcaptcha utilizies the textcaptcha.com API to help prevent SPAM. The idea behind the library is to be framework agnostic.
4
+
5
+ == Configuring
6
+ If you've not yet obtained an API key for textcaptcha.com, do so at http://textcaptcha.com/register.
7
+ Textcaptcha.configure do |config|
8
+ config.api_key = 'yourapikeyhere'
9
+ end
10
+
11
+ == Retrieving a textCAPTCHA
12
+ captcha = Textcaptcha.obtain
13
+ # => { :question => "What's eight - 7?", :answers => [ "c4ca4238a0b923820dcc509a6f75849b", "f97c5d29941bfb1b2fdab0874906ab82" ] }
14
+
15
+ == Validating an answer
16
+ Validate the user's answer compared to the answers retrieved from Textcaptcha.obtain
17
+ captcha = Textcaptcha.obtain
18
+ # => { :question => "What's eight - 7?", :answers => [ "c4ca4238a0b923820dcc509a6f75849b", "f97c5d29941bfb1b2fdab0874906ab82" ] }
19
+ Textcaptcha.valid? 'sixteen', captcha[:answers]
20
+ # => false
21
+
22
+ == Rails 3 Implementation
23
+ Since textcaptcha remains framework agnostic, there's a bit more work to implement it, yet minimal.
24
+
25
+ On the sign up form, retrieve the question and answer(s).
26
+ # app/controllers/users_controller.rb
27
+ # GET /users/new
28
+ def new
29
+ Textcaptcha.configure do |config|
30
+ config.api_key = 'yourapikeyhere'
31
+ end
32
+
33
+ captcha = Textcaptcha.obtain
34
+ session[:captcha_answers] = captcha[:answers]
35
+ @captcha_question = captcha[:question]
36
+ @user = User.new
37
+ end
38
+
39
+ # POST /users
40
+ def create
41
+ user = User.new(params[:user])
42
+ if Textcaptcha.valid?(params[:captcha_answer], session[:captcha_answers]) && user.save
43
+ # ...
44
+ end
45
+ end
46
+
47
+ In your view.
48
+ # app/views/users/new.html.erb
49
+ <%= label_tag 'captcha_answer', @captcha_question %>
50
+ <%= text_field_tag 'captcha_answer' %>
51
+
52
+ == Contributing
53
+ Be a hero in helping with the battle against spam.
54
+ * Fork
55
+ * Write test(s)
56
+ * Implement
57
+ * Submit pull request
58
+
59
+ Or
60
+
61
+ * Send me money
62
+
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+ require "rdoc/task"
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/test_*.rb'
9
+ test.verbose = false
10
+ end
11
+
12
+ RDoc::Task.new do |rd|
13
+ README = 'README.rdoc'
14
+ rd.main = README
15
+ rd.rdoc_dir = 'doc'
16
+ rd.title = 'textcaptcha'
17
+ rd.rdoc_files.include(README, 'lib/**/*.rb')
18
+ end
19
+
@@ -0,0 +1,39 @@
1
+ require "textcaptcha/configuration"
2
+ require "textcaptcha/version"
3
+ require "digest/md5"
4
+ require "httparty"
5
+
6
+ module Textcaptcha
7
+ # The API URL for textcaptcha
8
+ URL = 'http://api.textcaptcha.com/'
9
+
10
+ class << self
11
+ attr_accessor :configuration
12
+
13
+ # Configure variables. See Textcaptcha::Configuration
14
+ def configure
15
+ self.configuration ||= Configuration.new
16
+ yield configuration
17
+ end
18
+
19
+ # Obtain the question and answer(s). Returns a hash with the question and answers. The answers are MD5 hashed values to be checked after submission.
20
+ #
21
+ # Textcaptcha.obtain # => { :question => "What's eight - 7?", :answers => [ "c4ca4238a0b923820dcc509a6f75849b", "f97c5d29941bfb1b2fdab0874906ab82" ] }
22
+ def obtain
23
+ api_response = HTTParty.get(URL << self.configuration.api_key)
24
+ { :question => api_response['captcha']['question'], :answers => Array.new(api_response['captcha']['answer']) }
25
+ end
26
+ alias :get :obtain
27
+
28
+ # Returns true if the answer is valid.
29
+ #
30
+ # == Parameters
31
+ # * <tt>answer</tt> - The answer provided by the user.
32
+ # * <tt>answers</tt> - The possible correct answers returned from <tt>obtain</tt>.
33
+ def valid?(answer, answers)
34
+ answers.include?(Digest::MD5.hexdigest(answer.to_s.lstrip.rstrip))
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,7 @@
1
+ module Textcaptcha #:nodoc:
2
+ # Store the configuration values for Textcaptcha
3
+ class Configuration
4
+ # * <tt>api_key</tt> - The API key that you received from textcaptcha.com
5
+ attr_accessor :api_key
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Textcaptcha
2
+ VERSION = "0.0.1"
3
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "test/unit"
2
+ require "textcaptcha"
3
+
4
+ class Test::Unit::TestCase
5
+ def setup
6
+ Textcaptcha.configure do |config|
7
+ config.api_key = '7k7mhd0r3ack4gkwwsg8o0g0k9hk1abi'
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,24 @@
1
+ require "helper"
2
+ require "digest/md5"
3
+
4
+ class TestTextcaptcha < Test::Unit::TestCase
5
+
6
+ def test_obtain
7
+ response = Textcaptcha.obtain
8
+ assert_equal false, response[:question].nil?
9
+ assert_equal true, response[:question].length > 0
10
+ assert_equal 'Array', response[:answers].class.to_s
11
+ assert_equal true, response[:answers].length > 0
12
+ end
13
+
14
+ def test_valid?
15
+ answers = [ Digest::MD5.hexdigest('three'), Digest::MD5.hexdigest('3') ]
16
+ assert_equal false, Textcaptcha.valid?('four', answers)
17
+ assert_equal false, Textcaptcha.valid?(4, answers)
18
+ assert_equal true, Textcaptcha.valid?('three', answers)
19
+ assert_equal true, Textcaptcha.valid?('3', answers)
20
+ assert_equal true, Textcaptcha.valid?(3, answers)
21
+ end
22
+
23
+ end
24
+
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/textcaptcha/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Drew Tempelmeyer"]
6
+ gem.email = ["drewtemp@gmail.com"]
7
+ gem.description = %q{Ruby gem to use the textcaptcha.com service}
8
+ gem.summary = %q{Ruby gem to use the textcaptcha.com service}
9
+ gem.homepage = "https://github.com/drewtempelmeyer/textcaptcha"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "textcaptcha"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Textcaptcha::VERSION
17
+ gem.date = "2012-01-19"
18
+
19
+ gem.add_dependency('httparty', '>= 0.8.1')
20
+
21
+ # Development dependencies
22
+ gem.add_development_dependency 'rake', '>= 0.9.2'
23
+ gem.add_development_dependency 'rdoc', '>= 2.5.0'
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: textcaptcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Drew Tempelmeyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70292534697440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70292534697440
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70292534696940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70292534696940
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &70292534696480 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 2.5.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70292534696480
47
+ description: Ruby gem to use the textcaptcha.com service
48
+ email:
49
+ - drewtemp@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - lib/textcaptcha.rb
60
+ - lib/textcaptcha/configuration.rb
61
+ - lib/textcaptcha/version.rb
62
+ - test/helper.rb
63
+ - test/test_textcaptcha.rb
64
+ - textcaptcha.gemspec
65
+ homepage: https://github.com/drewtempelmeyer/textcaptcha
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: 1438809734930026122
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 1438809734930026122
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.15
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Ruby gem to use the textcaptcha.com service
95
+ test_files:
96
+ - test/helper.rb
97
+ - test/test_textcaptcha.rb