rain_captcha 0.0.1

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.
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2013 Jay Strybis
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.
21
+
@@ -0,0 +1,44 @@
1
+ # RainCaptcha
2
+
3
+ A simple Ruby wrapper for the [RainCaptcha](http://raincaptcha.driversworld.us/) HTTP API.
4
+
5
+ ## Installation
6
+ gem install rain_captcha
7
+
8
+ ## Configuration
9
+ ### Use HTTPS
10
+ ```ruby
11
+ RainCaptcha.configure do |config|
12
+ config.use_ssl = true
13
+ end
14
+
15
+ RainCaptcha.url("unique_identifier")
16
+ => "https://raincaptcha.driversworld.us/captcha/?key=unique_identifier"
17
+ ```
18
+
19
+ ## Examples
20
+ ### Get a Captcha image url
21
+ ```ruby
22
+ RainCaptcha.url("unique_identifier")
23
+ => "http://raincaptcha.driversworld.us/captcha/?key=unique_identifier"
24
+ ```
25
+
26
+ ### Verify a user response
27
+ ```ruby
28
+ RainCaptcha.verify("unique_identifier", "correct_response")
29
+ => true
30
+
31
+ RainCaptcha.verify("unique_identifier", "incorrect_response")
32
+ => false
33
+ ```
34
+
35
+ ### RainCaptcha service not available
36
+ ```ruby
37
+ RainCaptcha.verify("unique_identifier", "correct_response")
38
+ => Net::HTTPFatalError
39
+ ```
40
+ ## Copyright
41
+ Copyright (c) 2013 Jay Strybis.
42
+ See [LICENSE][] for details.
43
+
44
+ [license]: LICENSE.md
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task default: :test
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'test'
9
+ test.pattern = 'test/**/*_test.rb'
10
+ test.verbose = true
11
+ end
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+ require 'rain_captcha/configuration'
3
+
4
+ module RainCaptcha
5
+ extend Configuration
6
+
7
+ class << self
8
+ def url(identifier)
9
+ uri = URI(endpoint)
10
+ params = { key: identifier }
11
+ uri.query = URI.encode_www_form(params)
12
+ uri.to_s
13
+ end
14
+
15
+ def verify_url(identifier, answer)
16
+ verify_uri(identifier, answer).to_s
17
+ end
18
+
19
+ def verify_uri(identifier, answer)
20
+ uri = URI("#{endpoint}test/")
21
+ params = { key: identifier, code: answer }
22
+ uri.query = URI.encode_www_form(params)
23
+ uri
24
+ end
25
+
26
+ def verify(identifier, answer)
27
+ response = Net::HTTP.get_response(verify_uri(identifier, answer))
28
+ if response.is_a? Net::HTTPSuccess
29
+ response.body == "true"
30
+ else
31
+ response.value
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ module RainCaptcha
2
+ module Configuration
3
+ attr_accessor :use_ssl
4
+
5
+ RAINCAPTCHA_URL = 'raincaptcha.driversworld.us/captcha/'
6
+
7
+ def self.extended(base)
8
+ base.reset
9
+ end
10
+
11
+ def configure
12
+ yield self
13
+ end
14
+
15
+ def protocol
16
+ self.use_ssl ? "https" : "http"
17
+ end
18
+
19
+ def endpoint
20
+ "#{self.protocol}://#{RAINCAPTCHA_URL}"
21
+ end
22
+
23
+ def reset
24
+ self.use_ssl = false
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module RainCaptcha
2
+ VERSION = "0.0.1" unless defined?(RainCaptcha::VERSION)
3
+ end
4
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rain_captcha/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rain_captcha'
8
+ spec.version = RainCaptcha::VERSION
9
+ spec.licenses = ['MIT']
10
+ spec.authors = ['Jay Strybis']
11
+ spec.email = ['jay.strybis@gmail.com']
12
+ spec.homepage = 'https://github.com/unreal/rain_captcha'
13
+ spec.summary = %q{Rails plugin for RainCaptcha service}
14
+ spec.description = %q{Anti-spam protection for your website}
15
+
16
+ spec.files = %w(LICENSE.md rain_captcha.gemspec Rakefile README.md)
17
+ spec.files += Dir.glob("lib/**/*.rb")
18
+ spec.test_files = Dir.glob("test/**/*")
19
+
20
+ spec.require_paths = ['lib']
21
+ spec.required_rubygems_version = '>= 1.3.6'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.0'
24
+ spec.add_development_dependency 'test-unit', '~> 2.5'
25
+ spec.add_development_dependency 'webmock', '~> 1.11'
26
+ end
27
+
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class Foo
4
+ extend RainCaptcha::Configuration
5
+ end
6
+
7
+ class ConfigurationTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ Foo.reset
11
+ end
12
+
13
+ def test_protocol
14
+ assert_equal "http", Foo.protocol
15
+ end
16
+
17
+ def test_protocol_while_using_ssl
18
+ Foo.use_ssl = true
19
+ assert_equal "https", Foo.protocol
20
+ end
21
+
22
+ def test_endpoint
23
+ assert_equal 'http://raincaptcha.driversworld.us/captcha/', Foo.endpoint
24
+ end
25
+
26
+ def test_configure_user_ssl
27
+ Foo.configure do |c|
28
+ c.use_ssl = true
29
+ end
30
+
31
+ assert_equal 'https://raincaptcha.driversworld.us/captcha/', Foo.endpoint
32
+ end
33
+
34
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+ require 'webmock/test_unit'
3
+
4
+ class RainCaptchaTest < Test::Unit::TestCase
5
+
6
+ def test_url
7
+ assert_equal "http://raincaptcha.driversworld.us/captcha/?key=example_org%3A127.0.0.1", RainCaptcha.url("example_org:127.0.0.1")
8
+ assert_equal "http://raincaptcha.driversworld.us/captcha/?key=username%40example.org", RainCaptcha.url("username@example.org")
9
+ end
10
+
11
+ def test_verify_url
12
+ assert_equal "http://raincaptcha.driversworld.us/captcha/test/?key=example_org%3A127.0.0.1&code=abcde", RainCaptcha.verify_url("example_org:127.0.0.1","abcde")
13
+ assert_equal "http://raincaptcha.driversworld.us/captcha/test/?key=username%40example.org&code=fghij", RainCaptcha.verify_url("username@example.org","fghij")
14
+ end
15
+
16
+ def test_verify_uri_returns_a_uri
17
+ assert RainCaptcha.verify_uri("foo","bar").is_a? URI
18
+ end
19
+
20
+ def test_verify_true
21
+ stub_request(:get, "http://raincaptcha.driversworld.us/captcha/test/?code=bar&key=foo").
22
+ to_return(:status => 200, :body => "true")
23
+ assert RainCaptcha.verify("foo","bar")
24
+ end
25
+
26
+ def test_verify_false
27
+ stub_request(:get, "http://raincaptcha.driversworld.us/captcha/test/?code=bar&key=foo").
28
+ to_return(:status => 200, :body => "false")
29
+ assert !RainCaptcha.verify("foo","bar")
30
+ end
31
+
32
+ def test_verify_unsuccesful
33
+ stub_request(:get, "http://raincaptcha.driversworld.us/captcha/test/?code=bar&key=foo").
34
+ to_return(:status => 500)
35
+
36
+ assert_raise Net::HTTPFatalError do
37
+ RainCaptcha.verify("foo","bar")
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+
3
+ require 'rain_captcha'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rain_captcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jay Strybis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: test-unit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.5'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.5'
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.11'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ description: Anti-spam protection for your website
63
+ email:
64
+ - jay.strybis@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - LICENSE.md
70
+ - rain_captcha.gemspec
71
+ - Rakefile
72
+ - README.md
73
+ - lib/rain_captcha/configuration.rb
74
+ - lib/rain_captcha/version.rb
75
+ - lib/rain_captcha.rb
76
+ - test/rain_captcha/configuration_test.rb
77
+ - test/rain_captcha_test.rb
78
+ - test/test_helper.rb
79
+ homepage: https://github.com/unreal/rain_captcha
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.6
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.25
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Rails plugin for RainCaptcha service
104
+ test_files:
105
+ - test/rain_captcha/configuration_test.rb
106
+ - test/rain_captcha_test.rb
107
+ - test/test_helper.rb
108
+ has_rdoc: