adscaptcha 0.0.41
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/adscaptcha.gemspec +17 -0
- data/lib/adscaptcha.rb +44 -0
- data/lib/adscaptcha/client_helper.rb +26 -0
- data/lib/adscaptcha/configuration.rb +33 -0
- data/lib/adscaptcha/rails.rb +5 -0
- data/lib/adscaptcha/verify.rb +55 -0
- data/lib/adscaptcha/version.rb +3 -0
- data/test/lib/adscaptcha/configuration.rb +0 -0
- data/test/lib/adscaptcha/version_test.rb +6 -0
- data/test/test_helper.rb +3 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jimmy Cuervo
|
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.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Adscaptcha
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'adscaptcha'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install adscaptcha
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/adscaptcha.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/adscaptcha/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jimmy Cuervo"]
|
6
|
+
gem.email = ["richard.gonzales@gmail.com"]
|
7
|
+
gem.description = %q{Helpers for AdsCaptcha API}
|
8
|
+
gem.summary = %q{This plugin adds helpers for the AdsCaptcha API}
|
9
|
+
gem.homepage = "https://github.com/jcuervo/AdsCaptcha"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "adscaptcha"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Adscaptcha::VERSION
|
17
|
+
end
|
data/lib/adscaptcha.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'adscaptcha/configuration'
|
2
|
+
require 'adscaptcha/verify'
|
3
|
+
require 'adscaptcha/client_helper'
|
4
|
+
require "adscaptcha/version"
|
5
|
+
|
6
|
+
module Adscaptcha
|
7
|
+
ADSCAPTCHA_API = 'api.adscaptcha.com'
|
8
|
+
|
9
|
+
def self.configuration
|
10
|
+
@configuration ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
# Allows easy setting of multiple configuration options. See Configuration
|
14
|
+
# for all available options.
|
15
|
+
#--
|
16
|
+
# The temp assignment is only used to get a nicer rdoc. Feel free to remove
|
17
|
+
# this hack.
|
18
|
+
#++
|
19
|
+
def self.configure
|
20
|
+
config = configuration
|
21
|
+
yield(config)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.with_configuration(config)
|
25
|
+
original_config = {}
|
26
|
+
|
27
|
+
config.each do |key, value|
|
28
|
+
original_config[key] = configuration.send(key)
|
29
|
+
configuration.send("#{key}=", value)
|
30
|
+
end
|
31
|
+
|
32
|
+
result = yield if block_given?
|
33
|
+
|
34
|
+
original_config.each { |key, value| configuration.send("#{key}=", value) }
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
class AdscaptchaError < StandardError
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
if defined?(Rails)
|
43
|
+
require 'adscaptcha/rails'
|
44
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Adscaptcha
|
2
|
+
module ClientHelper
|
3
|
+
def adscaptcha_tags(options = {})
|
4
|
+
captcha_id = options[:captcha_id] ||= Adscaptcha.configuration.captcha_id
|
5
|
+
key = options[:public_key] ||= Adscaptcha.configuration.public_key
|
6
|
+
|
7
|
+
raise AdscaptchaError, "No public key specified." unless key
|
8
|
+
error = options[:error] ||= ((defined? flash) ? flash[:adscaptcha_error] : "")
|
9
|
+
|
10
|
+
dummy = Random.new.rand(1..9999999999)
|
11
|
+
urlGet = "http://#{Adscaptcha.configuration.ads_captcha_api}/Get.aspx"
|
12
|
+
urlNoScript = "http://#{Adscaptcha.configuration.ads_captcha_api}/NoScript.aspx"
|
13
|
+
params = "?CaptchaId=#{captcha_id}&PublicKey=#{key}&Dummy=#{dummy}"
|
14
|
+
|
15
|
+
html = "<script src='#{urlGet}#{params}' type='text/javascript'></script>"
|
16
|
+
html << "<noscript>"
|
17
|
+
html << "<iframe src='#{urlNoScript}#{params}' width='300' height='100' frameborder='0'></iframe>"
|
18
|
+
html << "<table>"
|
19
|
+
html << "<tr><td>Type challenge here:</td><td><input type='text' name='adscaptcha_response_field' value='' /></td></tr>"
|
20
|
+
html << "<tr><td>Paste code here:</td><td><input type='text' name='adscaptcha_challenge_field' value='' /></td></tr>"
|
21
|
+
html << "</table>"
|
22
|
+
html << "</noscript>"
|
23
|
+
return (html.respond_to?(:html_safe) && html.html_safe) || html
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Adscaptcha
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :captcha_id,
|
4
|
+
:public_key,
|
5
|
+
:private_key,
|
6
|
+
:ads_captcha_api
|
7
|
+
# attr_accessor :nonssl_api_server_url,
|
8
|
+
# :ssl_api_server_url,
|
9
|
+
# :verify_url,
|
10
|
+
# :skip_verify_env,
|
11
|
+
# :private_key,
|
12
|
+
# :public_key,
|
13
|
+
# :proxy,
|
14
|
+
# :handle_timeouts_gracefully
|
15
|
+
|
16
|
+
def initialize #:nodoc:
|
17
|
+
# @nonssl_api_server_url = RECAPTCHA_API_SERVER_URL
|
18
|
+
# @ssl_api_server_url = RECAPTCHA_API_SECURE_SERVER_URL
|
19
|
+
# @verify_url = RECAPTCHA_VERIFY_URL
|
20
|
+
# @skip_verify_env = SKIP_VERIFY_ENV
|
21
|
+
# @handle_timeouts_gracefully = HANDLE_TIMEOUTS_GRACEFULLY
|
22
|
+
@ads_captcha_api = ADSCAPTCHA_API
|
23
|
+
|
24
|
+
@private_key = ENV['ADSCAPTCHA_PRIVATE_KEY']
|
25
|
+
@public_key = ENV['ADSCAPTCHA_PUBLIC_KEY']
|
26
|
+
@captcha_id = ENV['ADSCAPTCHA_ID']
|
27
|
+
end
|
28
|
+
|
29
|
+
# def api_server_url(ssl = false) #:nodoc:
|
30
|
+
# ssl ? ssl_api_server_url : nonssl_api_server_url
|
31
|
+
# end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "uri"
|
2
|
+
module Adscaptcha
|
3
|
+
module Verify
|
4
|
+
def verify_adscaptcha(options = {})
|
5
|
+
validate_url = "http://#{Adscaptcha.configuration.ads_captcha_api}/Validate.aspx"
|
6
|
+
private_key = options[:private_key] || Adscaptcha.configuration.private_key
|
7
|
+
captcha_id = options[:captcha_id] || Adscaptcha.configuration.captcha_id
|
8
|
+
|
9
|
+
model = options[:model]
|
10
|
+
attribute = options[:attribute] || :base
|
11
|
+
|
12
|
+
begin
|
13
|
+
response = nil
|
14
|
+
|
15
|
+
Timeout::timeout(options[:timeout] || 3) do
|
16
|
+
uri = URI(validate_url)
|
17
|
+
req = Net::HTTP::Post.new(uri.path)
|
18
|
+
|
19
|
+
req.set_form_data({
|
20
|
+
"CaptchaId" => captcha_id,
|
21
|
+
"PrivateKey" => private_key,
|
22
|
+
"ChallengeCode" => params[:adscaptcha_challenge_field],
|
23
|
+
"UserResponse" => params[:adscaptcha_response_field],
|
24
|
+
"RemoteAddress" => request.remote_ip
|
25
|
+
})
|
26
|
+
|
27
|
+
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
28
|
+
http.request(req)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
unless response.body == 'true'
|
33
|
+
flash[:adscaptcha_error] = "Captcha error. Please try again."
|
34
|
+
if model
|
35
|
+
message = "Captcha error. Please try again."
|
36
|
+
model.errors.add attribute, options[:message] || message
|
37
|
+
end
|
38
|
+
return false
|
39
|
+
else
|
40
|
+
flash.delete(:adscaptcha_error)
|
41
|
+
return true
|
42
|
+
end
|
43
|
+
rescue Timeout::Error
|
44
|
+
flash[:adscaptcha_error] = "Could not validate captcha now. Please try again."
|
45
|
+
if model
|
46
|
+
message = "Could not validate captcha now. Please try again."
|
47
|
+
model.errors.add attribute, options[:message] || message
|
48
|
+
return false
|
49
|
+
end
|
50
|
+
rescue Exception => e
|
51
|
+
raise AdscaptchaError, e.message, e.backtrace
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
File without changes
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: adscaptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 77
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 41
|
10
|
+
version: 0.0.41
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jimmy Cuervo
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-09-25 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Helpers for AdsCaptcha API
|
23
|
+
email:
|
24
|
+
- richard.gonzales@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- adscaptcha.gemspec
|
38
|
+
- lib/adscaptcha.rb
|
39
|
+
- lib/adscaptcha/client_helper.rb
|
40
|
+
- lib/adscaptcha/configuration.rb
|
41
|
+
- lib/adscaptcha/rails.rb
|
42
|
+
- lib/adscaptcha/verify.rb
|
43
|
+
- lib/adscaptcha/version.rb
|
44
|
+
- test/lib/adscaptcha/configuration.rb
|
45
|
+
- test/lib/adscaptcha/version_test.rb
|
46
|
+
- test/test_helper.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: https://github.com/jcuervo/AdsCaptcha
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.7
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: This plugin adds helpers for the AdsCaptcha API
|
81
|
+
test_files:
|
82
|
+
- test/lib/adscaptcha/configuration.rb
|
83
|
+
- test/lib/adscaptcha/version_test.rb
|
84
|
+
- test/test_helper.rb
|