yandex_cleanweb 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/yandex_cleanweb/version.rb +1 -1
- data/lib/yandex_cleanweb.rb +12 -9
- data/test/test_helper.rb +1 -0
- data/test/yandex_cleanweb_test.rb +14 -3
- data/yandex_cleanweb.gemspec +1 -0
- metadata +18 -2
data/README.md
CHANGED
data/lib/yandex_cleanweb.rb
CHANGED
@@ -6,8 +6,9 @@ require "net/http"
|
|
6
6
|
|
7
7
|
module YandexCleanweb
|
8
8
|
class NoApiKeyException < Exception; end
|
9
|
+
class BadResponseException < Exception; end
|
9
10
|
|
10
|
-
API_URL = 'http://cleanweb-api.yandex.ru/1.0
|
11
|
+
API_URL = 'http://cleanweb-api.yandex.ru/1.0'
|
11
12
|
|
12
13
|
class << self
|
13
14
|
attr_accessor :api_key
|
@@ -19,6 +20,8 @@ module YandexCleanweb
|
|
19
20
|
request_id_tag = doc.xpath('//check-spam-result/id')
|
20
21
|
spam_flag_tag = doc.xpath('//check-spam-result/text')
|
21
22
|
|
23
|
+
raise BadResponseException if request_id_tag.size.zero?
|
24
|
+
|
22
25
|
request_id = request_id_tag[0].content
|
23
26
|
spam_flag = spam_flag_tag[0].attributes["spam-flag"].content
|
24
27
|
|
@@ -29,7 +32,7 @@ module YandexCleanweb
|
|
29
32
|
[el.attributes["url"], el.attributes["spam_flag"] == 'yes']
|
30
33
|
end
|
31
34
|
|
32
|
-
{ :
|
35
|
+
{ id: request_id, links: links }
|
33
36
|
else
|
34
37
|
false
|
35
38
|
end
|
@@ -42,7 +45,7 @@ module YandexCleanweb
|
|
42
45
|
url = doc.xpath('//get-captcha-result/url').text
|
43
46
|
captcha_id = doc.xpath('//get-captcha-result/captcha').text
|
44
47
|
|
45
|
-
{ :
|
48
|
+
{ url: url, captcha: captcha_id }
|
46
49
|
end
|
47
50
|
|
48
51
|
def valid_captcha?(request_id, captcha_id, value)
|
@@ -56,10 +59,10 @@ module YandexCleanweb
|
|
56
59
|
def api_check_captcha(request_id, captcha_id, value)
|
57
60
|
check_captcha_url = "#{API_URL}/check-captcha"
|
58
61
|
params = {
|
59
|
-
:
|
60
|
-
:
|
61
|
-
:
|
62
|
-
:
|
62
|
+
key: prepare_api_key,
|
63
|
+
id: request_id,
|
64
|
+
captcha: captcha_id,
|
65
|
+
value: value
|
63
66
|
}
|
64
67
|
|
65
68
|
uri = URI.parse(check_captcha_url)
|
@@ -70,7 +73,7 @@ module YandexCleanweb
|
|
70
73
|
|
71
74
|
def api_get_captcha(request_id)
|
72
75
|
get_captcha_url = "#{API_URL}/get-captcha"
|
73
|
-
params = { :
|
76
|
+
params = { key: prepare_api_key, id: request_id }
|
74
77
|
|
75
78
|
uri = URI.parse(get_captcha_url)
|
76
79
|
uri.query = URI.encode_www_form(params)
|
@@ -79,7 +82,7 @@ module YandexCleanweb
|
|
79
82
|
end
|
80
83
|
|
81
84
|
def api_check_spam(options)
|
82
|
-
cleanweb_options = { :
|
85
|
+
cleanweb_options = { key: prepare_api_key }
|
83
86
|
|
84
87
|
if options[0].is_a?(String) # quick check
|
85
88
|
cleanweb_options[:body_plain] = options[0]
|
data/test/test_helper.rb
CHANGED
@@ -63,11 +63,11 @@ describe YandexCleanweb do
|
|
63
63
|
|
64
64
|
describe "advanced mode" do
|
65
65
|
it "works" do
|
66
|
-
YandexCleanweb.spam?(:
|
66
|
+
YandexCleanweb.spam?(body_plain: "my text", ip: "80.80.40.3").must_equal false
|
67
67
|
end
|
68
68
|
|
69
69
|
it "with some html" do
|
70
|
-
result = YandexCleanweb.spam?(:
|
70
|
+
result = YandexCleanweb.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
|
71
71
|
|
72
72
|
result[:id].wont_be_empty
|
73
73
|
result[:links].must_be_empty
|
@@ -78,7 +78,7 @@ describe YandexCleanweb do
|
|
78
78
|
describe "#get_captcha + #valid_captcha?" do
|
79
79
|
|
80
80
|
it "works for not valid captchas" do
|
81
|
-
result = YandexCleanweb.spam?(:
|
81
|
+
result = YandexCleanweb.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
|
82
82
|
captcha = YandexCleanweb.get_captcha(result[:id])
|
83
83
|
|
84
84
|
captcha[:url].wont_be_empty
|
@@ -88,5 +88,16 @@ describe YandexCleanweb do
|
|
88
88
|
valid.must_equal false
|
89
89
|
end
|
90
90
|
end
|
91
|
+
|
92
|
+
describe "raises BadResponseException in case of empty result" do
|
93
|
+
before do
|
94
|
+
FakeWeb.clean_registry
|
95
|
+
end
|
96
|
+
|
97
|
+
it do
|
98
|
+
FakeWeb.register_uri(:post, "http://cleanweb-api.yandex.ru/1.0/check-spam", body: "")
|
99
|
+
proc { YandexCleanweb.spam?(body_plain: "any text") }.must_raise(YandexCleanweb::BadResponseException)
|
100
|
+
end
|
101
|
+
end
|
91
102
|
end
|
92
103
|
end
|
data/yandex_cleanweb.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yandex_cleanweb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -75,6 +75,22 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: fakeweb
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
78
94
|
description: Ruby wrapper for Yandex.Cleanweb
|
79
95
|
email:
|
80
96
|
- shatrov@me.com
|