yandex_captcha 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +23 -0
- data/README.md +103 -0
- data/Rakefile +13 -0
- data/app/assets/images/yandex_captcha/loading.gif +0 -0
- data/app/assets/javascripts/yandex_captcha/captcha.coffee.erb +4 -0
- data/app/assets/stylesheets/yandex_captcha/captcha.css +3 -0
- data/app/controllers/yandex_captcha/application_controller.rb +6 -0
- data/app/controllers/yandex_captcha/captcha_controller.rb +8 -0
- data/app/views/yandex_captcha/_captcha.html.erb +16 -0
- data/app/views/yandex_captcha/_captcha_ajax.html.erb +8 -0
- data/config/routes.rb +3 -0
- data/lib/yandex_captcha/configuration.rb +46 -0
- data/lib/yandex_captcha/engine.rb +19 -0
- data/lib/yandex_captcha/helpers/rails.rb +18 -0
- data/lib/yandex_captcha/helpers/sinatra.rb +17 -0
- data/lib/yandex_captcha/rails.rb +3 -0
- data/lib/yandex_captcha/sinatra.rb +19 -0
- data/lib/yandex_captcha/verify.rb +109 -0
- data/lib/yandex_captcha/version.rb +3 -0
- data/lib/yandex_captcha.rb +53 -0
- data/test/test_helper.rb +9 -0
- data/test/yandex_cleanweb_test.rb +109 -0
- data/yandex_captcha.gemspec +26 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ae23241bb45bcf89f041f848941187d5200b81f0
|
4
|
+
data.tar.gz: 7fa96c268f0025bc5e9dbc06b4db2cca060278a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd7ebb30538e31051761bba8432f30c25ea9c57188ee238a9ba49434948feee5870093ae951db45f7534914e4a7c26158f516898fff28634357091a4deafde2a
|
7
|
+
data.tar.gz: 3c38f97fdab8cafb79b7a9745cba9fb50570faabd8e5a6aff7f952abfb0493726c75ba284f968b8c14da193ef7579c0a34ee968148d7c6d6e3c8064e4f8d52de
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.2
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2014 Alexander Merkulov
|
2
|
+
Copyright (c) 2012 Kir Shatrov
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# Yandex Captcha
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/merqlove/yandex-captcha.png)](https://codeclimate.com/github/merqlove/yandex-captcha)
|
4
|
+
[![Build Status](https://travis-ci.org/merqlove/yandex-captcha.svg)](https://travis-ci.org/merqlove/yandex-captcha)
|
5
|
+
|
6
|
+
Ruby wrapper for [Yandex Cleanweb](http://api.yandex.ru/cleanweb/) spam detector.
|
7
|
+
|
8
|
+
Unfortunatelly, this gem *is not capable with MRI 1.8.7* because of MRI 1.8.7 doesn't have `URI.encode_www_form` method.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'yandex_captcha', '~> 0.4.0'
|
15
|
+
|
16
|
+
Or:
|
17
|
+
|
18
|
+
gem 'yandex_captcha', github: 'merqlove/yandex-captcha'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install yandex_captcha
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Get the api key: [http://api.yandex.ru/cleanweb/getkey.xml](http://api.yandex.ru/cleanweb/getkey.xml)
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# Rails routes
|
34
|
+
mount YandexCaptcha::Engine, at: '/yandex_captcha/'
|
35
|
+
|
36
|
+
# Sinatra
|
37
|
+
register YandexCaptcha::Sinatra
|
38
|
+
|
39
|
+
# Configuration
|
40
|
+
YandexCaptcha.configure do |config|
|
41
|
+
config.api_key = "your_key"
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
In Views:
|
46
|
+
|
47
|
+
```erb
|
48
|
+
<%= captcha_tags %>
|
49
|
+
<%= captcha_tags ajax:true %>
|
50
|
+
<%= captcha_tags noscript:true %>
|
51
|
+
```
|
52
|
+
|
53
|
+
In Controllers:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
if YandexCaptcha::Verify.valid_captcha?(params[:captcha_response_id], params[:captcha_response_field])
|
57
|
+
# some
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
Other examples:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# Methods
|
65
|
+
YandexCaptcha::Verify.spam?("just phrase")
|
66
|
+
=> false
|
67
|
+
|
68
|
+
YandexCaptcha::Verify.spam?(body_plain: "my text", ip: "80.80.40.3")
|
69
|
+
=> false
|
70
|
+
|
71
|
+
YandexCaptcha::Verify.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
|
72
|
+
=> { id: "request id", links: [ ['http://spam.com', true] ] }
|
73
|
+
```
|
74
|
+
|
75
|
+
More complex example:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
|
79
|
+
user_input = "download free porn <a>...</a>"
|
80
|
+
if spam_check = YandexCaptcha::Verify.spam?(user_input, ip: current_user.ip)
|
81
|
+
captcha = YandexCaptcha::Verify.get_captcha(spam_check[:id])
|
82
|
+
|
83
|
+
# now you can show captcha[:url] to user
|
84
|
+
# but remember to write captcha[:captcha] to session
|
85
|
+
|
86
|
+
# to check is captcha enterred by user is valid:
|
87
|
+
captcha_valid = YandexCaptcha::Verify.valid_captcha?(result[:id], captcha[:captcha], user_captcha)
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
If you use Yandex Captcha in Rails app, we recommend to set up the api key in `config/initializers/yandex_captcha.rb`
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
1. Fork it
|
96
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
97
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
98
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
99
|
+
5. Create new Pull Request
|
100
|
+
|
101
|
+
---
|
102
|
+
|
103
|
+
Special respect to Evrone which gem `yandex-cleanweb` include most of code in `lib/yandex_captcha/verify.rb` and some examples on this page.
|
data/Rakefile
ADDED
Binary file
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<% if noscript %>
|
2
|
+
<noscript>
|
3
|
+
<% end %>
|
4
|
+
<div id="captcha_widget">
|
5
|
+
<% if error %>
|
6
|
+
<div id="captcha_error"><%= error %></div>
|
7
|
+
<% end %>
|
8
|
+
<div id="captcha_image">
|
9
|
+
<img src="<%= captcha[:url] %>" />
|
10
|
+
</div>
|
11
|
+
<input type="text" id="captcha_response_field" name="captcha_response_field" placeholder="Введите цифры" />
|
12
|
+
<input type="hidden" name="captcha_response_id" value="<%= captcha[:captcha] %>" />
|
13
|
+
</div>
|
14
|
+
<% if noscript %>
|
15
|
+
</noscript>
|
16
|
+
<% end %>
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<div id="captcha_widget">
|
2
|
+
<div id="captcha_image">
|
3
|
+
<img src="<%= asset_path("yandex_captcha/loading.gif") %>" />
|
4
|
+
</div>
|
5
|
+
<input type="text" id="captcha_response_field" name="captcha_response_field" placeholder="Введите цифры" />
|
6
|
+
<input type="hidden" name="captcha_response_id" value="" />
|
7
|
+
</div>
|
8
|
+
<%= javascript_include_tag "yandex_captcha/captcha" %>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module YandexCaptcha
|
2
|
+
# This class enables detailed configuration of the Yandex CleanWeb services.
|
3
|
+
#
|
4
|
+
# By calling
|
5
|
+
#
|
6
|
+
# YandexCaptcha.configuration # => instance of YandexCaptcha::Configuration
|
7
|
+
#
|
8
|
+
# or
|
9
|
+
# YandexCaptcha.configure do |config|
|
10
|
+
# config # => instance of YandexCaptcha::Configuration
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# you are able to perform configuration updates.
|
14
|
+
#
|
15
|
+
# Your are able to customize all attributes listed below. All values have
|
16
|
+
# sensitive default and will very likely not need to be changed.
|
17
|
+
#
|
18
|
+
# Please note that the public and private key for the Yandex CleanWeb API Access
|
19
|
+
# have no useful default value. The keys may be set via the Shell enviroment
|
20
|
+
# or using this configuration. Settings within this configuration always take
|
21
|
+
# precedence.
|
22
|
+
#
|
23
|
+
# Setting the keys with this Configuration
|
24
|
+
#
|
25
|
+
# YandexCaptcha.configure do |config|
|
26
|
+
# config.api_key = 'cw.1.1.gsdjdgskjhsdgjkgsdjsdjgkskhsgjkgsjhdkgsdghskd.sdgjhgsdsgdkjgdshkgds'
|
27
|
+
# config.captcha_type = 'elite'
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
class Configuration
|
31
|
+
attr_accessor :api_server_url,
|
32
|
+
:api_key,
|
33
|
+
:skip_verify_env,
|
34
|
+
:handle_timeouts_gracefully,
|
35
|
+
:captcha_type
|
36
|
+
|
37
|
+
def initialize #:nodoc:
|
38
|
+
@api_server_url = API_URL
|
39
|
+
@captcha_type = CAPTCHA_TYPE
|
40
|
+
@api_key = ENV['CAPTCHA_KEY']
|
41
|
+
@skip_verify_env = SKIP_VERIFY_ENV
|
42
|
+
@handle_timeouts_gracefully = HANDLE_TIMEOUTS_GRACEFULLY
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module YandexCaptcha
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
isolate_namespace YandexCaptcha
|
4
|
+
engine_name 'yandex_captcha'
|
5
|
+
|
6
|
+
if Rails.version >= '3.1'
|
7
|
+
initializer :assets do |app|
|
8
|
+
app.config.assets.precompile += %w( yandex_captcha/captcha.js yandex_captcha/captcha.css yandex_captcha/loading.gif )
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer "setup config" do
|
13
|
+
begin
|
14
|
+
ActionView::Base.send(:include, ::YandexCaptcha::Helpers::Rails)
|
15
|
+
ActionController::Base.send(:include, ::YandexCaptcha::Verify)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module YandexCaptcha
|
2
|
+
module Helpers
|
3
|
+
module Rails
|
4
|
+
|
5
|
+
def captcha_tags(options = {})
|
6
|
+
|
7
|
+
error = options[:error] ||= ((defined? flash) ? flash[:captcha_error] : "")
|
8
|
+
if options[:ajax]
|
9
|
+
render "yandex_captcha/captcha_ajax"
|
10
|
+
else
|
11
|
+
captcha = YandexCaptcha::Verify.get_captcha
|
12
|
+
render "yandex_captcha/captcha", locals: { captcha: captcha, error: error, noscript: options[:noscript] } if captcha
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module YandexCaptcha
|
2
|
+
module Helpers
|
3
|
+
module Sinatra
|
4
|
+
|
5
|
+
def captcha_tags(options = {})
|
6
|
+
template = settings.captcha_ajax_template.to_s.to_sym
|
7
|
+
if options[:ajax]
|
8
|
+
render(current_engine, template, {layout: false}).to_s.html_safe
|
9
|
+
else
|
10
|
+
captcha = YandexCaptcha::Verify.get_captcha
|
11
|
+
render(current_engine, template, {layout: false}, { captcha: captcha, noscript: options[:noscript] }).to_s.html_safe if captcha
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'yandex_captcha/helpers/sinatra'
|
3
|
+
|
4
|
+
module YandexCaptcha
|
5
|
+
module Sinatra
|
6
|
+
def self.registered(app)
|
7
|
+
app.set :captcha_ajax_template, "yandex_captcha/captcha_ajax" unless app.settings.captcha_ajax_template
|
8
|
+
app.set :captcha_template, "yandex_captcha/captcha" unless app.settings.captcha_template
|
9
|
+
app.set :captcha_url, "/get_captcha" unless app.settings.captcha_url
|
10
|
+
|
11
|
+
app.get app.settings.captcha_url do
|
12
|
+
content_type :json
|
13
|
+
YandexCaptcha::Verify.get_captcha.to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
app.helpers Helpers::Sinatra
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "nokogiri"
|
3
|
+
require "net/http"
|
4
|
+
|
5
|
+
module YandexCaptcha
|
6
|
+
module Verify
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def spam?(*options)
|
10
|
+
response = api_check_spam(options)
|
11
|
+
doc = Nokogiri::XML(response)
|
12
|
+
|
13
|
+
spam_result = doc.xpath('//check-spam-result')
|
14
|
+
request_id_tag = spam_result.xpath('id')
|
15
|
+
spam_flag_tag = spam_result.xpath('text')
|
16
|
+
|
17
|
+
raise BadResponseException if request_id_tag.size.zero?
|
18
|
+
|
19
|
+
request_id = request_id_tag.first
|
20
|
+
spam_flag = spam_flag_tag.first.attributes["spam-flag"]
|
21
|
+
|
22
|
+
spam_check request_id.content, spam_result, spam_flag.content
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_captcha(request_id=nil)
|
26
|
+
response = api_get_captcha(request_id)
|
27
|
+
doc = Nokogiri::XML(response)
|
28
|
+
|
29
|
+
url = doc.xpath('//get-captcha-result/url').text
|
30
|
+
captcha_id = doc.xpath('//get-captcha-result/captcha').text
|
31
|
+
|
32
|
+
{ url: url, captcha: captcha_id }
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid_captcha?(captcha_id=nil, value=nil, request_id=nil)
|
36
|
+
response = api_check_captcha(request_id, captcha_id, value)
|
37
|
+
doc = Nokogiri::XML(response)
|
38
|
+
doc.xpath('//check-captcha-result/ok').any?
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def api_check_captcha(request_id, captcha_id, value)
|
44
|
+
check_captcha_url = "#{API_URL}/check-captcha"
|
45
|
+
params = {
|
46
|
+
key: prepare_api_key,
|
47
|
+
id: request_id,
|
48
|
+
captcha: captcha_id,
|
49
|
+
value: value
|
50
|
+
}
|
51
|
+
|
52
|
+
uri = URI.parse(check_captcha_url)
|
53
|
+
uri.query = URI.encode_www_form(params)
|
54
|
+
|
55
|
+
Net::HTTP.get(uri)
|
56
|
+
end
|
57
|
+
|
58
|
+
def api_get_captcha(request_id)
|
59
|
+
get_captcha_url = "#{API_URL}/get-captcha"
|
60
|
+
params = { key: prepare_api_key, id: request_id, type: YandexCaptcha.configuration.captcha_type }
|
61
|
+
|
62
|
+
uri = URI.parse(get_captcha_url)
|
63
|
+
uri.query = URI.encode_www_form(params)
|
64
|
+
|
65
|
+
Net::HTTP.get(uri)
|
66
|
+
end
|
67
|
+
|
68
|
+
def api_check_spam(options)
|
69
|
+
cleanweb_options = { key: prepare_api_key }
|
70
|
+
run_options = options[0]
|
71
|
+
|
72
|
+
if run_options.is_a?(String) # quick check
|
73
|
+
cleanweb_options[:body_plain] = run_options
|
74
|
+
else
|
75
|
+
options = run_options
|
76
|
+
cleanweb_options.merge!(Hash[options.map{ |k,v| [k.to_s.gsub("_","-"), v] }])
|
77
|
+
end
|
78
|
+
|
79
|
+
check_spam_url = "#{API_URL}/check-spam"
|
80
|
+
uri = URI.parse(check_spam_url)
|
81
|
+
response = Net::HTTP.post_form(uri, cleanweb_options)
|
82
|
+
response.body
|
83
|
+
end
|
84
|
+
|
85
|
+
def spam_check(request_id, spam_result, spam_flag)
|
86
|
+
return false unless request_id or spam_result or spam_flag
|
87
|
+
|
88
|
+
if spam_flag == 'yes'
|
89
|
+
links = spam_result.xpath('links')
|
90
|
+
links_childrens = links.first.children
|
91
|
+
|
92
|
+
links_childrens.map do |el|
|
93
|
+
[el.attributes["url"], el.attributes["spam_flag"] == 'yes']
|
94
|
+
end
|
95
|
+
|
96
|
+
{ id: request_id, links: links_childrens }
|
97
|
+
else
|
98
|
+
false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def prepare_api_key
|
103
|
+
raise NoApiKeyException if YandexCaptcha.configuration.api_key.nil? || YandexCaptcha.configuration.api_key.empty?
|
104
|
+
|
105
|
+
YandexCaptcha.configuration.api_key
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'yandex_captcha/configuration'
|
2
|
+
require 'yandex_captcha/verify'
|
3
|
+
|
4
|
+
module YandexCaptcha
|
5
|
+
class NoApiKeyException < Exception; end
|
6
|
+
class BadResponseException < Exception; end
|
7
|
+
|
8
|
+
API_URL = 'http://cleanweb-api.yandex.ru/1.0'
|
9
|
+
CAPTCHA_TYPE = 'std'
|
10
|
+
HANDLE_TIMEOUTS_GRACEFULLY = true
|
11
|
+
SKIP_VERIFY_ENV = ['test', 'cucumber']
|
12
|
+
|
13
|
+
# Gives access to the current Configuration.
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
# Allows easy setting of multiple configuration options. See Configuration
|
19
|
+
# for all available options.
|
20
|
+
#--
|
21
|
+
# The temp assignment is only used to get a nicer rdoc. Feel free to remove
|
22
|
+
# this hack.
|
23
|
+
#++
|
24
|
+
def self.configure
|
25
|
+
config = configuration
|
26
|
+
yield(config)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.with_configuration(config)
|
30
|
+
original_config = {}
|
31
|
+
|
32
|
+
config.each do |key, value|
|
33
|
+
original_config[key] = configuration.send(key)
|
34
|
+
configuration.send("#{key}=", value)
|
35
|
+
end
|
36
|
+
|
37
|
+
result = yield if block_given?
|
38
|
+
|
39
|
+
original_config.each { |key, value| configuration.send("#{key}=", value) }
|
40
|
+
result
|
41
|
+
end
|
42
|
+
|
43
|
+
class YandexCleanwebError < StandardError
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
if defined?(Rails)
|
48
|
+
require 'yandex_captcha/rails'
|
49
|
+
end
|
50
|
+
|
51
|
+
if defined?(Sinatra) and Sinatra.respond_to? :register
|
52
|
+
require 'yandex_captcha/sinatra'
|
53
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "test_helper"
|
3
|
+
|
4
|
+
describe YandexCaptcha do
|
5
|
+
|
6
|
+
context "without api key" do
|
7
|
+
before do
|
8
|
+
YandexCaptcha.configure do |config|
|
9
|
+
config.api_key = nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#spam?" do
|
14
|
+
it "raise an error" do
|
15
|
+
-> {
|
16
|
+
YandexCaptcha::Verify.spam?("anything")
|
17
|
+
}.must_raise YandexCaptcha::NoApiKeyException
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#get_captcha" do
|
22
|
+
it "raise an error" do
|
23
|
+
-> {
|
24
|
+
YandexCaptcha::Verify.get_captcha("anything")
|
25
|
+
}.must_raise YandexCaptcha::NoApiKeyException
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#valid_captcha?" do
|
30
|
+
it "raise an error" do
|
31
|
+
-> {
|
32
|
+
YandexCaptcha::Verify.valid_captcha?("anything", "anything", 123)
|
33
|
+
}.must_raise YandexCaptcha::NoApiKeyException
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with empty api key" do
|
40
|
+
before do
|
41
|
+
YandexCaptcha.configure do |config|
|
42
|
+
config.api_key = ""
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "raise an error" do
|
47
|
+
-> {
|
48
|
+
YandexCaptcha::Verify.spam?("anything")
|
49
|
+
}.must_raise YandexCaptcha::NoApiKeyException
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "with api key" do
|
54
|
+
|
55
|
+
before do
|
56
|
+
YandexCaptcha.configure do |config|
|
57
|
+
config.api_key = "cw.1.1.20121227T080449Z.51de1ee126e5ced6.f4f417fb55727520d7e39b00cf5393d4b1ca5e78"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#spam?" do
|
62
|
+
|
63
|
+
describe "simple check" do
|
64
|
+
it "works" do
|
65
|
+
YandexCaptcha::Verify.spam?("фраза").must_equal false
|
66
|
+
YandexCaptcha::Verify.spam?("недорого увеличение пениса проститутки").must_equal false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "advanced mode" do
|
71
|
+
it "works" do
|
72
|
+
YandexCaptcha::Verify.spam?(body_plain: "my text", ip: "80.80.40.3").must_equal false
|
73
|
+
end
|
74
|
+
|
75
|
+
it "with some html" do
|
76
|
+
result = YandexCaptcha::Verify.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
|
77
|
+
|
78
|
+
result[:id].wont_be_empty
|
79
|
+
result[:links].must_be_empty
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#get_captcha + #valid_captcha?" do
|
85
|
+
|
86
|
+
it "works for not valid captchas" do
|
87
|
+
result = YandexCaptcha::Verify.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
|
88
|
+
captcha = YandexCaptcha::Verify.get_captcha(result[:id])
|
89
|
+
|
90
|
+
captcha[:url].wont_be_empty
|
91
|
+
captcha[:captcha].wont_be_empty
|
92
|
+
|
93
|
+
valid = YandexCaptcha::Verify.valid_captcha?(result[:id], captcha[:captcha], "1234")
|
94
|
+
valid.must_equal false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "raises BadResponseException in case of empty result" do
|
99
|
+
before do
|
100
|
+
FakeWeb.clean_registry
|
101
|
+
end
|
102
|
+
|
103
|
+
it "check for spam" do
|
104
|
+
FakeWeb.register_uri(:post, "http://cleanweb-api.yandex.ru/1.0/check-spam", body: "")
|
105
|
+
proc { YandexCaptcha::Verify.spam?(body_plain: "any text") }.must_raise(YandexCaptcha::BadResponseException)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yandex_captcha/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "yandex_captcha"
|
8
|
+
gem.version = YandexCaptcha::VERSION
|
9
|
+
gem.authors = ["Alexander Merkulov","Kir Shatrov"]
|
10
|
+
gem.email = ["sasha@merqlove.ru","shatrov@me.com"]
|
11
|
+
gem.description = %q{Ruby wrapper for Yandex.Cleanweb}
|
12
|
+
gem.summary = %q{Ruby wrapper for Yandex.Cleanweb spam detector}
|
13
|
+
gem.homepage = "https://github.com/merqlove/yandex-captcha"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'nokogiri'
|
21
|
+
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
gem.add_development_dependency "minitest", "~> 3.0"
|
24
|
+
gem.add_development_dependency "minitest-spec-context"
|
25
|
+
gem.add_development_dependency "fakeweb"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yandex_captcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander Merkulov
|
8
|
+
- Kir Shatrov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-06-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: minitest-spec-context
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: fakeweb
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: Ruby wrapper for Yandex.Cleanweb
|
85
|
+
email:
|
86
|
+
- sasha@merqlove.ru
|
87
|
+
- shatrov@me.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".ruby-version"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- app/assets/images/yandex_captcha/loading.gif
|
100
|
+
- app/assets/javascripts/yandex_captcha/captcha.coffee.erb
|
101
|
+
- app/assets/stylesheets/yandex_captcha/captcha.css
|
102
|
+
- app/controllers/yandex_captcha/application_controller.rb
|
103
|
+
- app/controllers/yandex_captcha/captcha_controller.rb
|
104
|
+
- app/views/yandex_captcha/_captcha.html.erb
|
105
|
+
- app/views/yandex_captcha/_captcha_ajax.html.erb
|
106
|
+
- config/routes.rb
|
107
|
+
- lib/yandex_captcha.rb
|
108
|
+
- lib/yandex_captcha/configuration.rb
|
109
|
+
- lib/yandex_captcha/engine.rb
|
110
|
+
- lib/yandex_captcha/helpers/rails.rb
|
111
|
+
- lib/yandex_captcha/helpers/sinatra.rb
|
112
|
+
- lib/yandex_captcha/rails.rb
|
113
|
+
- lib/yandex_captcha/sinatra.rb
|
114
|
+
- lib/yandex_captcha/verify.rb
|
115
|
+
- lib/yandex_captcha/version.rb
|
116
|
+
- test/test_helper.rb
|
117
|
+
- test/yandex_cleanweb_test.rb
|
118
|
+
- yandex_captcha.gemspec
|
119
|
+
homepage: https://github.com/merqlove/yandex-captcha
|
120
|
+
licenses: []
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.2.2
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Ruby wrapper for Yandex.Cleanweb spam detector
|
142
|
+
test_files:
|
143
|
+
- test/test_helper.rb
|
144
|
+
- test/yandex_cleanweb_test.rb
|